You are here

NodeOptionPremiumPermissions.php in Node Option Premium 8

Namespace

Drupal\nopremium

File

src/NodeOptionPremiumPermissions.php
View source
<?php

namespace Drupal\nopremium;

use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\Entity\NodeType;

/**
 * Provides dynamic permissions for nodes of different types for promoted.
 */
class NodeOptionPremiumPermissions {
  use StringTranslationTrait;

  /**
   * Returns an array of node type permissions.
   *
   * @return array
   *   The node type permissions.
   *
   * @see \Drupal\user\PermissionHandlerInterface::getPermissions()
   */
  public function nodeTypePermissions() {
    $perms = [];

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

  /**
   * 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 [
      "view full {$type_id} premium content" => [
        'title' => $this
          ->t('%type_name: View full premium content', $type_params),
      ],
      "override {$type_id} premium content" => [
        'title' => $this
          ->t('%type_name: Override premium option', $type_params),
      ],
    ];
  }

}

Classes

Namesort descending Description
NodeOptionPremiumPermissions Provides dynamic permissions for nodes of different types for promoted.