You are here

TcaPermissionGenerator.php in Token Content Access 2.0.x

Same filename and directory in other branches
  1. 8 src/TcaPermissionGenerator.php

Namespace

Drupal\tca

File

src/TcaPermissionGenerator.php
View source
<?php

namespace Drupal\tca;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\tca\Plugin\TcaPluginManager;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;

/**
 * Class TcaPermissionGenerator.
 *
 * @package Drupal\tca
 */
class TcaPermissionGenerator implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * Drupal\Core\Entity\EntityTypeManager definition.
   *
   * @var Drupal\Core\Entity\EntityTypeManager
   */
  private $entityTypeManager = NULL;

  /**
   * Drupal\tca\Plugin\TcaPluginManager definition.
   *
   * @var Drupal\tca\Plugin\TcaPluginManager
   */
  private $tcaPluginManager = NULL;

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

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

  /**
   * Return an array of per-entity tca permissions.
   *
   * @return array
   *   An array of permissions
   */
  public function permissions() {
    $permissions = [];
    foreach ($this->tcaPluginManager
      ->getDefinitions() as $def) {
      $entity_type = $this->entityTypeManager
        ->getStorage($def['entityType'])
        ->getEntityType();
      $permissions += [
        'tca administer ' . $def['entityType'] => [
          'title' => $this
            ->t('Administer Token Content Access settings for %entity_type', [
            '%entity_type' => $entity_type
              ->getLabel(),
          ]),
        ],
        'tca bypass ' . $def['entityType'] => [
          'title' => $this
            ->t('Bypass Token Content Access action for %entity_type', [
            '%entity_type' => $entity_type
              ->getLabel(),
          ]),
        ],
      ];
    }
    return $permissions;
  }

}

Classes

Namesort descending Description
TcaPermissionGenerator Class TcaPermissionGenerator.