You are here

class NodePermissions in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/node/src/NodePermissions.php \Drupal\node\NodePermissions
  2. 10 core/modules/node/src/NodePermissions.php \Drupal\node\NodePermissions

Provides dynamic permissions for nodes of different types.

Hierarchy

Expanded class hierarchy of NodePermissions

File

core/modules/node/src/NodePermissions.php, line 12

Namespace

Drupal\node
View source
class NodePermissions {
  use BundlePermissionHandlerTrait;
  use StringTranslationTrait;

  /**
   * Returns an array of node type permissions.
   *
   * @return array
   *   The node type permissions.
   *   @see \Drupal\user\PermissionHandlerInterface::getPermissions()
   */
  public function nodeTypePermissions() {
    return $this
      ->generatePermissions(NodeType::loadMultiple(), [
      $this,
      'buildPermissions',
    ]);
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
BundlePermissionHandlerTrait::generatePermissions protected function Builds a permissions array for the supplied bundles.
NodePermissions::buildPermissions protected function Returns a list of node permissions for a given node type.
NodePermissions::nodeTypePermissions 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.