You are here

class AccessUnpublishedPermissions in Access unpublished 8

Calculates the permissions for access_unpublished.

@package Drupal\access_unpublished

Hierarchy

Expanded class hierarchy of AccessUnpublishedPermissions

File

src/AccessUnpublishedPermissions.php, line 16

Namespace

Drupal\access_unpublished
View source
class AccessUnpublishedPermissions implements ContainerInjectionInterface {
  use StringTranslationTrait;

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

  /**
   * Constructs a new AccessUnpublishedPermissions instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
   *   The translation service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $translation) {
    $this->entityTypeManager = $entity_type_manager;
    $this->stringTranslation = $translation;
  }

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

  /**
   * Permissions callback.
   *
   * @return array
   *   Returns permissions for all nodes.
   */
  public function permissions() {
    $permissions = [];
    $definitions = $this->entityTypeManager
      ->getDefinitions();
    foreach ($definitions as $definition) {
      if (AccessUnpublished::applicableEntityType($definition)) {
        $permission = 'access_unpublished ' . $definition
          ->id();
        if ($definition
          ->get('bundle_entity_type')) {
          $bundles = $this->entityTypeManager
            ->getStorage($definition
            ->getBundleEntityType())
            ->loadMultiple();
          foreach ($bundles as $bundle) {
            $permissions[$permission . ' ' . $bundle
              ->id()] = [
              'title' => $this
                ->t('Access unpublished %bundle @type', [
                '%bundle' => strtolower($bundle
                  ->label()),
                '@type' => $definition
                  ->getPluralLabel(),
              ]),
            ];
          }
        }
        else {
          $permissions[$permission] = [
            'title' => $this
              ->t('Access unpublished @type', [
              '@type' => $definition
                ->getPluralLabel(),
            ]),
          ];
        }
      }
    }
    return $permissions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AccessUnpublishedPermissions::$entityTypeManager protected property The entity type manager.
AccessUnpublishedPermissions::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
AccessUnpublishedPermissions::permissions public function Permissions callback.
AccessUnpublishedPermissions::__construct public function Constructs a new AccessUnpublishedPermissions instance.
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.