access_policy-1.0.x-dev/src/Plugin/Derivative/DynamicLocalTasks.php
src/Plugin/Derivative/DynamicLocalTasks.php
<?php namespace Drupal\access_policy\Plugin\Derivative; use Drupal\access_policy\AccessPolicyInformation; use Drupal\Component\Plugin\Derivative\DeriverBase; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\TranslationInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Generates access-related local tasks. */ class DynamicLocalTasks extends DeriverBase implements ContainerDeriverInterface { use StringTranslationTrait; /** * The base plugin ID. * * @var string */ protected $basePluginId; /** * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * The access policy information service. * * @var \Drupal\access_policy\AccessPolicyInformation */ protected $accessPolicyInfo; /** * Creates a AccessPolicy local task object. * * @param string $base_plugin_id * The base plugin ID. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The translation manager. * @param \Drupal\access_policy\AccessPolicyInformation $access_policy_information * The access policy information service. */ public function __construct($base_plugin_id, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation, AccessPolicyInformation $access_policy_information) { $this->entityTypeManager = $entity_type_manager; $this->stringTranslation = $string_translation; $this->basePluginId = $base_plugin_id; $this->accessPolicyInfo = $access_policy_information; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, $base_plugin_id) { return new static( $base_plugin_id, $container->get('entity_type.manager'), $container->get('string_translation'), $container->get('access_policy.information'), ); } /** * {@inheritdoc} */ public function getDerivativeDefinitions($base_plugin_definition) { $this->derivatives = []; // Add the Access tab to entities. $supported_entity_types = array_filter($this->entityTypeManager->getDefinitions(), function (EntityTypeInterface $type) { return $this->accessPolicyInfo->isAccessControlledEntityType($type); }); foreach ($supported_entity_types as $entity_type_id => $entity_type) { $this->derivatives["$entity_type_id.access_tab"] = [ 'route_name' => "entity.$entity_type_id.access_policy_form", 'title' => $this->t('Access'), 'base_route' => "entity.$entity_type_id.canonical", 'weight' => 1, ] + $base_plugin_definition; } return $this->derivatives; } }