Skip to content

Commit

Permalink
added menu local task alter to restrict edit profile for securefile user
Browse files Browse the repository at this point in the history
  • Loading branch information
yeniatencio committed Dec 10, 2024
1 parent 7c31c61 commit 453097a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions modules/tide_media/tide_media.module
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
Expand All @@ -16,6 +17,7 @@ use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\file\FileInterface;
use Drupal\views\ViewExecutable;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
* Implements hook_views_pre_view().
Expand Down Expand Up @@ -152,6 +154,17 @@ function tide_media_form_media_form_alter(&$form, FormStateInterface $form_state
* Implements template_preprocess_field().
*/
function tide_media_preprocess_field(&$variables) {
$route_name = \Drupal::routeMatch()->getRouteName();
$current_user = \Drupal::currentUser();
$roles = $current_user->getRoles();

if ($route_name === 'entity.user.edit_form' && in_array('secure_file_user', $roles)) {
$url = \Drupal\Core\Url::fromRoute('entity.user.canonical', ['user' => $current_user->id()]);
$response = new RedirectResponse($url->toString());
$response->send();
exit();
}

if ($variables['field_name'] == 'field_media_file') {
$element = $variables['element'];
if ($element['#entity_type'] == 'media' && $element['#bundle'] == 'document' && $element['#view_mode'] == 'embedded') {
Expand Down Expand Up @@ -464,3 +477,19 @@ function tide_media_file_presave(FileInterface $file) {
$file->setFilename($filename_new);
}
}

/**
* Implements hook_menu_local_tasks_alter().
*/
function tide_media_menu_local_tasks_alter(&$data, $route_name, RefinableCacheableDependencyInterface &$cacheability) {
$current_user = \Drupal::currentUser();
$roles = $current_user->getRoles();

if (in_array('secure_file_user', $roles)) {
foreach ($data['tabs'][0] as $key => $items) {
if ($key === 'entity.user.edit_form') {
unset($data['tabs'][0][$key]);
}
}
}
}

0 comments on commit 453097a

Please sign in to comment.