1<?php 2 3namespace Drupal\entity_test; 4 5use Drupal\Core\StringTranslation\StringTranslationTrait; 6use Drupal\entity_test\Entity\EntityTestBundle; 7 8/** 9 * Provides dynamic permissions for entity test. 10 */ 11class EntityTestPermissions { 12 13 use StringTranslationTrait; 14 15 /** 16 * Returns an array of entity_test_bundle permissions. 17 * 18 * @return array 19 * An array of entity_test_bundle permissions. 20 * @see \Drupal\user\PermissionHandlerInterface::getPermissions() 21 */ 22 public function entityTestBundlePermissions() { 23 $perms = []; 24 // Generate permissions for all EntityTestBundle bundles. 25 foreach (EntityTestBundle::loadMultiple() as $type) { 26 $perms += $this->buildPermissions($type); 27 } 28 29 return $perms; 30 } 31 32 /** 33 * Returns a list of entity test permissions for a given entity test bundle. 34 * 35 * @param \Drupal\entity_test\Entity\EntityTestBundle $type 36 * The entity test bundle. 37 * 38 * @return array 39 * An associative array of permission names and descriptions. 40 */ 41 protected function buildPermissions(EntityTestBundle $type) { 42 $type_id = $type->id(); 43 $type_params = ['%type_name' => $type->label()]; 44 45 return [ 46 "create $type_id entity_test_with_bundle entities" => [ 47 'title' => $this->t('%type_name: Create new entity', $type_params), 48 ], 49 ]; 50 } 51 52} 53