You are here

class ProjectPermissions in Drupal PM (Project Management) 4.x

Provides dynamic permissions for Project of different types.

Hierarchy

Expanded class hierarchy of ProjectPermissions

File

modules/pm_project/src/ProjectPermissions.php, line 13

Namespace

Drupal\pm_project
View source
class ProjectPermissions {
  use StringTranslationTrait;

  /**
   * Returns an array of node type permissions.
   *
   * @return array
   *   The Project by bundle permissions.
   *   @see \Drupal\user\PermissionHandlerInterface::getPermissions()
   */
  public function generatePermissions() {
    $perms = [];
    foreach (Project::loadMultiple() as $type) {
      $perms += $this
        ->buildPermissions($type);
    }
    return $perms;
  }

  /**
   * Returns a list of node permissions for a given node type.
   *
   * @param \Drupal\pm_project\Entity\Project $type
   *   The Project type.
   *
   * @return array
   *   An associative array of permission names and descriptions.
   */
  protected function buildPermissions(Project $type) {
    $type_id = $type
      ->id();
    $type_params = [
      '%type_name' => $type
        ->label(),
    ];
    return [
      "{$type_id} create entities" => [
        'title' => $this
          ->t('Create new %type_name entities', $type_params),
      ],
      "{$type_id} edit own entities" => [
        'title' => $this
          ->t('Edit own %type_name entities', $type_params),
      ],
      "{$type_id} edit any entities" => [
        'title' => $this
          ->t('Edit any %type_name entities', $type_params),
      ],
      "{$type_id} delete own entities" => [
        'title' => $this
          ->t('Delete own %type_name entities', $type_params),
      ],
      "{$type_id} delete any entities" => [
        'title' => $this
          ->t('Delete any %type_name entities', $type_params),
      ],
      "{$type_id} view revisions" => [
        'title' => $this
          ->t('View %type_name revisions', $type_params),
        'description' => t('To view a revision, you also need permission to view the entity item.'),
      ],
      "{$type_id} revert revisions" => [
        'title' => $this
          ->t('Revert %type_name revisions', $type_params),
        'description' => t('To revert a revision, you also need permission to edit the entity item.'),
      ],
      "{$type_id} delete revisions" => [
        'title' => $this
          ->t('Delete %type_name revisions', $type_params),
        'description' => $this
          ->t('To delete a revision, you also need permission to delete the entity item.'),
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProjectPermissions::buildPermissions protected function Returns a list of node permissions for a given node type.
ProjectPermissions::generatePermissions public function Returns an array of node type permissions.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.