You are here

class DomainEntityPermissions in Domain Access Entity 8

Dynamic permissions class for Domain Entity.

Hierarchy

Expanded class hierarchy of DomainEntityPermissions

File

src/DomainEntityPermissions.php, line 10

Namespace

Drupal\domain_entity
View source
class DomainEntityPermissions {
  use StringTranslationTrait;

  /**
   * Define permissions.
   */
  public function permissions() {
    $permissions = [];
    $mapper = \Drupal::service('domain_entity.mapper');
    $bundle_info = \Drupal::service('entity_type.bundle.info');
    foreach ($mapper
      ->getEnabledEntityTypes() as $type_id => $entity_type) {
      $bundles = $bundle_info
        ->getBundleInfo($type_id);
      if (!empty($bundles)) {
        foreach ($bundles as $bundle_id => $bundle) {
          $permissions += $this
            ->bundlePermissions($bundle_id, $bundle['label'], $entity_type
            ->id(), $entity_type
            ->getBundleLabel());
        }
      }
    }
    return $permissions;
  }

  /**
   * Helper method to generate bundle permission list.
   *
   * @param int $id
   *   Bundle ID.
   * @param string $label
   *   Bundle label.
   * @param int $entityTypeId
   *   Entity type ID.
   * @param string $entityTypeLabel
   *   Entity type label.
   *
   * @see DomainAccessPermissions
   *
   * @return array
   *   An array of permission names and descriptions.
   */
  private function bundlePermissions($id, $label, $entityTypeId, $entityTypeLabel) {

    // Build standard list of bundle permissions for this type.
    $perms = [
      "create {$id} {$entityTypeId} content on assigned domains" => [
        'title' => $this
          ->t('%entity_type_name: %type_name: Create new content on assigned domains', [
          '%type_name' => $label,
          '%entity_type_name' => $entityTypeLabel,
        ]),
      ],
      "update {$id} {$entityTypeId} content on assigned domains" => [
        'title' => $this
          ->t('%entity_type_name: %type_name: Edit any content on assigned domains', [
          '%type_name' => $label,
          '%entity_type_name' => $entityTypeLabel,
        ]),
      ],
      "delete {$id} {$entityTypeId} content on assigned domains" => [
        'title' => $this
          ->t('%entity_type_name: %type_name: Delete any content on assigned domains', [
          '%type_name' => $label,
          '%entity_type_name' => $entityTypeLabel,
        ]),
      ],
    ];
    return $perms;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DomainEntityPermissions::bundlePermissions private function Helper method to generate bundle permission list.
DomainEntityPermissions::permissions public function Define 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.