You are here

function webform_access_menu_local_tasks_alter in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_access/webform_access.module \webform_access_menu_local_tasks_alter()

Implements hook_menu_local_tasks_alter().

Add webform access group to local task cacheability.

See also

\Drupal\Core\Menu\Plugin\Block\LocalTasksBlock::build

File

modules/webform_access/webform_access.module, line 118
Provides webform access controls for webform nodes.

Code

function webform_access_menu_local_tasks_alter(&$data, $route_name) {

  // Change config entities 'Translate *' tab to be just label 'Translate'.
  $webform_entities = [
    'webform_access_group',
    'webform_access_type',
  ];
  foreach ($webform_entities as $webform_entity) {
    if (isset($data['tabs'][0]["config_translation.local_tasks:entity.{$webform_entity}.config_translation_overview"]['#link']['title'])) {
      $data['tabs'][0]["config_translation.local_tasks:entity.{$webform_entity}.config_translation_overview"]['#link']['title'] = t('Translate');
    }
  }
  $route_name = \Drupal::routeMatch()
    ->getRouteName();
  if ($route_name !== 'entity.node.canonical' && strpos($route_name, 'entity.node.webform.') !== 0) {
    return;
  }

  /** @var \Drupal\webform\WebformRequestInterface $request_handler */
  $request_handler = \Drupal::service('webform.request');
  $account = \Drupal::currentUser();
  $webform = $request_handler
    ->getCurrentWebform();
  $source_entity = $request_handler
    ->getCurrentSourceEntity();
  if (!$webform || $source_entity) {
    return;
  }

  /** @var \Drupal\webform_access\WebformAccessGroupStorageInterface $webform_access_group */
  $webform_access_group_storage = \Drupal::entityTypeManager()
    ->getStorage('webform_access_group');
  $webform_access_groups = $webform_access_group_storage
    ->loadByEntities($webform, $source_entity, $account);
  if (empty($webform_access_groups)) {
    return;
  }

  /** @var \Drupal\Core\Cache\CacheableMetadata $cacheability */
  $cacheability = $data['cacheability'];
  foreach ($webform_access_groups as $webforn_access_group) {
    $cacheability
      ->addCacheableDependency($webforn_access_group);
  }
}