You are here

class ActionsPermissions in Views Bulk Operations (VBO) 8

Same name and namespace in other branches
  1. 8.3 modules/actions_permissions/src/ActionsPermissions.php \Drupal\actions_permissions\ActionsPermissions
  2. 8.2 modules/actions_permissions/src/ActionsPermissions.php \Drupal\actions_permissions\ActionsPermissions
  3. 4.0.x modules/actions_permissions/src/ActionsPermissions.php \Drupal\actions_permissions\ActionsPermissions

Create permissions for existing actions.

Hierarchy

Expanded class hierarchy of ActionsPermissions

File

modules/actions_permissions/src/ActionsPermissions.php, line 14

Namespace

Drupal\actions_permissions
View source
class ActionsPermissions implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * VBO Action manager service.
   *
   * @var \Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionManager
   */
  protected $actionManager;

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

  /**
   * Constructor.
   *
   * @param \Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionManager $actionManager
   *   The action manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager.
   */
  public function __construct(ViewsBulkOperationsActionManager $actionManager, EntityTypeManagerInterface $entityTypeManager) {
    $this->actionManager = $actionManager;
    $this->entityTypeManager = $entityTypeManager;
  }

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

  /**
   * Get permissions for Actions.
   *
   * @return array
   *   Permissions array.
   */
  public function permissions() {
    $permissions = [];
    $entity_type_definitions = $this->entityTypeManager
      ->getDefinitions();

    // Get definitions that will not be altered by actions_permissions.
    foreach ($this->actionManager
      ->getDefinitions([
      'skip_actions_permissions' => TRUE,
      'nocache' => TRUE,
    ]) as $definition) {

      // Skip actions that define their own requirements.
      if (!empty($definition['requirements'])) {
        continue;
      }
      $id = 'execute ' . $definition['id'];
      $entity_type = NULL;
      if (empty($definition['type'])) {
        $entity_type = $this
          ->t('all entity types');
        $id .= ' all';
      }
      elseif (isset($entity_type_definitions[$definition['type']])) {
        $entity_type = $entity_type_definitions[$definition['type']]
          ->getLabel();
        $id .= ' ' . $definition['type'];
      }
      if (isset($entity_type)) {
        $permissions[$id] = [
          'title' => $this
            ->t('Execute the %action action on %type.', [
            '%action' => $definition['label'],
            '%type' => $entity_type,
          ]),
        ];
      }
    }

    // Rebuild VBO action definitions cache with
    // included action_permissions modifications.
    $this->actionManager
      ->getDefinitions([
      'nocache' => TRUE,
    ]);
    return $permissions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActionsPermissions::$actionManager protected property VBO Action manager service.
ActionsPermissions::$entityTypeManager protected property The entity type manager.
ActionsPermissions::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ActionsPermissions::permissions public function Get permissions for Actions.
ActionsPermissions::__construct public function Constructor.
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.