You are here

trait PermissionProviderTrait in Group 2.0.x

Trait for group relation permission providers.

This trait takes care of common logic for permission providers. Please make sure your handler service asks for the entity_type.manager service and sets to the $this->entityTypeManager property in its constructor.

Hierarchy

6 files declare their use of PermissionProviderTrait
BarAdminPermissionProvider.php in tests/modules/group_test_plugin_alter/src/Plugin/Group/RelationHandler/BarAdminPermissionProvider.php
BazAdminPermissionProvider.php in tests/modules/group_test_plugin_alter/src/Plugin/Group/RelationHandler/BazAdminPermissionProvider.php
FooAdminPermissionProvider.php in tests/modules/group_test_plugin_alter/src/Plugin/Group/RelationHandler/FooAdminPermissionProvider.php
FullEntityPermissionProvider.php in tests/modules/group_test_plugin/src/Plugin/Group/RelationHandler/FullEntityPermissionProvider.php
GroupNodePermissionProvider.php in modules/gnode/src/Plugin/Group/RelationHandler/GroupNodePermissionProvider.php

... See full list

File

src/Plugin/Group/RelationHandler/PermissionProviderTrait.php, line 15

Namespace

Drupal\group\Plugin\Group\RelationHandler
View source
trait PermissionProviderTrait {
  use RelationHandlerTrait {
    init as traitInit;
  }

  /**
   * The entity type the plugin handler is for.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface
   */
  protected $entityType;

  /**
   * Whether the target entity type implements the EntityOwnerInterface.
   *
   * @var bool
   */
  protected $implementsOwnerInterface;

  /**
   * Whether the target entity type implements the EntityPublishedInterface.
   *
   * @var bool
   */
  protected $implementsPublishedInterface;

  /**
   * Whether the plugin defines permissions for the target entity type.
   *
   * @var bool
   */
  protected $definesEntityPermissions;

  /**
   * {@inheritdoc}
   */
  public function init($plugin_id, array $definition) {
    $this
      ->traitInit($plugin_id, $definition);
    $this->entityType = $this
      ->entityTypeManager()
      ->getDefinition($definition['entity_type_id']);
    $this->implementsOwnerInterface = $this->entityType
      ->entityClassImplements(EntityOwnerInterface::class);
    $this->implementsPublishedInterface = $this->entityType
      ->entityClassImplements(EntityPublishedInterface::class);
    $this->definesEntityPermissions = !empty($definition['entity_access']);
  }

  /**
   * {@inheritdoc}
   */
  public function getAdminPermission() {
    if (!isset($this->parent)) {
      throw new \LogicException('Using PermissionProviderTrait without assigning a parent or overwriting the methods.');
    }
    return $this->parent
      ->getAdminPermission();
  }

  /**
   * {@inheritdoc}
   */
  public function getPermission($operation, $target, $scope = 'any') {
    if (!isset($this->parent)) {
      throw new \LogicException('Using PermissionProviderTrait without assigning a parent or overwriting the methods.');
    }
    return $this->parent
      ->getPermission($operation, $target, $scope);
  }

  /**
   * {@inheritdoc}
   */
  public function buildPermissions() {
    if (!isset($this->parent)) {
      throw new \LogicException('Using PermissionProviderTrait without assigning a parent or overwriting the methods.');
    }
    return $this->parent
      ->buildPermissions();
  }

  /**
   * Builds a permission with common translation arguments predefined.
   *
   * @param string $title
   *   The permission title.
   * @param string $description
   *   (optional) The permission description.
   *
   * @return array
   *   The permission with a default translatable markup replacement for both
   *   %plugin_name and %entity_type.
   */
  protected function buildPermission($title, $description = NULL) {
    $t_args = [
      '%plugin_name' => $this->definition['label'],
      '%entity_type' => $this->entityType
        ->getSingularLabel(),
    ];
    $permission['title'] = $title;
    $permission['title_args'] = $t_args;
    if (isset($description)) {
      $permission['description'] = $description;
      $permission['description_args'] = $t_args;
    }
    return $permission;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PermissionProviderTrait::$definesEntityPermissions protected property Whether the plugin defines permissions for the target entity type.
PermissionProviderTrait::$entityType protected property The entity type the plugin handler is for.
PermissionProviderTrait::$implementsOwnerInterface protected property Whether the target entity type implements the EntityOwnerInterface.
PermissionProviderTrait::$implementsPublishedInterface protected property Whether the target entity type implements the EntityPublishedInterface.
PermissionProviderTrait::buildPermission protected function Builds a permission with common translation arguments predefined.
PermissionProviderTrait::buildPermissions public function 4
PermissionProviderTrait::getAdminPermission public function 4
PermissionProviderTrait::getPermission public function 4
PermissionProviderTrait::init public function
RelationHandlerTrait::$definition protected property The plugin definition.
RelationHandlerTrait::$entityTypeManager protected property The entity type manager.
RelationHandlerTrait::$groupRelationManager protected property The group relation manager.
RelationHandlerTrait::$parent protected property The parent relation handler in the decorator chain.
RelationHandlerTrait::$pluginId protected property The plugin ID as read from the definition.
RelationHandlerTrait::entityTypeManager protected function Gets the entity type manager service.
RelationHandlerTrait::groupRelationManager protected function Gets the group relation manager service.
RelationHandlerTrait::init public function Aliased as: traitInit