You are here

class EntityPrintPermissions in Entity Print 8

Same name and namespace in other branches
  1. 8.2 src/EntityPrintPermissions.php \Drupal\entity_print\EntityPrintPermissions

Provides dynamic permissions for entity_print.

Hierarchy

Expanded class hierarchy of EntityPrintPermissions

File

src/EntityPrintPermissions.php, line 16

Namespace

Drupal\entity_print
View source
class EntityPrintPermissions implements ContainerInjectionInterface {
  use StringTranslationTrait;

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

  /*
   * Entity type bundle info.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface;
   */
  protected $entityTypeBundleInfo;

  /**
   * Constructs a new EntityPrintPermissions.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *    The entity manager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity type bundle info.
   * @param \Drupal\Core\StringTranslation\TranslationManager $translation_manager
   *   The translation manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, TranslationManager $translation_manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityTypeBundleInfo = $entity_type_bundle_info;
    $this->stringTranslation = $translation_manager;
  }

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

  /**
   * Returns an array of entity_print permissions.
   */
  public function getPermissions() {

    /** @var \Drupal\Core\Entity\EntityTypeInterface[] $content_entity_types */

    // Get all EntityTypes for the group "content".
    $content_entity_types = array_filter($this->entityTypeManager
      ->getDefinitions(), function ($entity_type) {
      return $entity_type
        ->getGroup() === 'content';
    });
    $permissions = [];
    foreach ($content_entity_types as $key => $content_entity_type) {
      $permissions['entity print access type ' . $content_entity_type
        ->id()] = [
        'title' => $this
          ->t('Use Entity Print for all bundles of type %entity_label', [
          '%entity_label' => $content_entity_type
            ->getLabel(),
        ]),
      ];

      // Add 1 permission for each bundle.
      $entity_type_bundles = $this->entityTypeBundleInfo
        ->getBundleInfo($content_entity_type
        ->id());
      foreach ($entity_type_bundles as $bundle_key => $entity_type_bundle) {
        $permissions['entity print access bundle ' . $bundle_key] = [
          'title' => $this
            ->t('Use entity print for bundle %entity_label - %entity_bundle_label', [
            '%entity_label' => $content_entity_type
              ->getLabel(),
            '%entity_bundle_label' => $entity_type_bundle['label'],
          ]),
        ];
      }
    }
    return $permissions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityPrintPermissions::$entityTypeBundleInfo protected property
EntityPrintPermissions::$entityTypeManager protected property The entity manager.
EntityPrintPermissions::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
EntityPrintPermissions::getPermissions public function Returns an array of entity_print permissions.
EntityPrintPermissions::__construct public function Constructs a new EntityPrintPermissions.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.