You are here

class CommentPermissions in Comment Permissions 8

Provide dynamic permissions for comments of different types.

Hierarchy

Expanded class hierarchy of CommentPermissions

File

src/CommentPermissions.php, line 11

Namespace

Drupal\comment_perm
View source
class CommentPermissions {
  use StringTranslationTrait;

  /**
   * Returns an array of comment type permissions.
   *
   * @return array
   *   The comment type permissions
   *   @see \Drupal\user\PermissionHandlerInterface::getPermissions()
   */
  public function commentTypePermissions() {
    $perm = array();

    // Generate comment permissions for all comment types.
    foreach (CommentType::loadMultiple() as $type) {
      $perm += $this
        ->buildPermissions($type);
    }
    return $perm;
  }

  /**
   * Returns a list of node permissions for a given node type.
   *
   * @param \Drupal\comment\Entity\CommentType $type
   *   The comment type.
   *
   * @return array
   *   An associative array of permission names and descriptions.
   */
  protected function buildPermissions(CommentType $type) {
    $type_id = $type
      ->id();
    $type_params = array(
      '%type_name' => $type
        ->label(),
    );
    return array(
      "administer {$type_id} comments" => array(
        'title' => $this
          ->t('%type_name: Administer comments and comment settings', $type_params),
      ),
      "administer {$type_id} comment type" => array(
        'title' => $this
          ->t('%type_name: Administer comment type and settings', $type_params),
        'restrict access' => TRUE,
      ),
      "access {$type_id} comments" => array(
        'title' => $this
          ->t('%type_name: View comments', $type_params),
      ),
      "post {$type_id} comments" => array(
        'title' => $this
          ->t('%type_name: Post comments', $type_params),
      ),
      "skip {$type_id} comment approval" => array(
        'title' => $this
          ->t('%type_name: Skip comment approval', $type_params),
      ),
      "edit {$type_id} own comments" => array(
        'title' => $this
          ->t('%type_name: Edit own comments', $type_params),
      ),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommentPermissions::buildPermissions protected function Returns a list of node permissions for a given node type.
CommentPermissions::commentTypePermissions public function Returns an array of comment type permissions.
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.