You are here

class RabbitHolePermissionGenerator in Rabbit Hole 2.x

Same name and namespace in other branches
  1. 8 src/RabbitHolePermissionGenerator.php \Drupal\rabbit_hole\RabbitHolePermissionGenerator

Generates permission for each supported entity type.

Hierarchy

Expanded class hierarchy of RabbitHolePermissionGenerator

File

src/RabbitHolePermissionGenerator.php, line 15

Namespace

Drupal\rabbit_hole
View source
class RabbitHolePermissionGenerator implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Entity plugin manager.
   *
   * @var \Drupal\rabbit_hole\Plugin\RabbitHoleEntityPluginManager
   */
  protected $rhEntityPluginManager;

  /**
   * Constructor.
   */
  public function __construct(EntityTypeManagerInterface $etm, RabbitHoleEntityPluginManager $entity_plugin_manager, TranslationInterface $translation) {
    $this->entityTypeManager = $etm;
    $this->rhEntityPluginManager = $entity_plugin_manager;
    $this->stringTranslation = $translation;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('plugin.manager.rabbit_hole_entity_plugin'), $container
      ->get('string_translation'));
  }

  /**
   * Return an array of per-entity rabbit hole permissions.
   *
   * @return array
   *   An array of permissions.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function permissions() {
    $permissions = [];
    foreach ($this->rhEntityPluginManager
      ->getDefinitions() as $def) {
      $entity_type = $this->entityTypeManager
        ->getStorage($def['entityType'])
        ->getEntityType();
      $permissions += [
        'rabbit hole administer ' . $def['entityType'] => [
          'title' => $this
            ->t('Administer Rabbit Hole settings for %entity_type', [
            '%entity_type' => $entity_type
              ->getLabel(),
          ]),
        ],
        'rabbit hole bypass ' . $def['entityType'] => [
          'title' => $this
            ->t('Bypass Rabbit Hole action for %entity_type', [
            '%entity_type' => $entity_type
              ->getLabel(),
          ]),
        ],
      ];
    }
    return $permissions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RabbitHolePermissionGenerator::$entityTypeManager protected property The entity type manager.
RabbitHolePermissionGenerator::$rhEntityPluginManager protected property Entity plugin manager.
RabbitHolePermissionGenerator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
RabbitHolePermissionGenerator::permissions public function Return an array of per-entity rabbit hole permissions.
RabbitHolePermissionGenerator::__construct public function Constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.