Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / taxonomy / src / TaxonomyPermissions.php
1 <?php
2
3 namespace Drupal\taxonomy;
4
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\StringTranslation\StringTranslationTrait;
8 use Drupal\taxonomy\Entity\Vocabulary;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Provides dynamic permissions of the taxonomy module.
13  *
14  * @see taxonomy.permissions.yml
15  */
16 class TaxonomyPermissions implements ContainerInjectionInterface {
17
18   use StringTranslationTrait;
19
20   /**
21    * The entity manager.
22    *
23    * @var \Drupal\Core\Entity\EntityManagerInterface
24    */
25   protected $entityManager;
26
27   /**
28    * Constructs a TaxonomyPermissions instance.
29    *
30    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
31    *   The entity manager.
32    */
33   public function __construct(EntityManagerInterface $entity_manager) {
34     $this->entityManager = $entity_manager;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function create(ContainerInterface $container) {
41     return new static($container->get('entity.manager'));
42   }
43
44   /**
45    * Get taxonomy permissions.
46    *
47    * @return array
48    *   Permissions array.
49    */
50   public function permissions() {
51     $permissions = [];
52     foreach (Vocabulary::loadMultiple() as $vocabulary) {
53       $permissions += $this->buildPermissions($vocabulary);
54     }
55     return $permissions;
56   }
57
58   /**
59    * Builds a standard list of taxonomy term permissions for a given vocabulary.
60    *
61    * @param \Drupal\taxonomy\VocabularyInterface $vocabulary
62    *   The vocabulary.
63    *
64    * @return array
65    *   An array of permission names and descriptions.
66    */
67   protected function buildPermissions(VocabularyInterface $vocabulary) {
68     $id = $vocabulary->id();
69     $args = ['%vocabulary' => $vocabulary->label()];
70
71     return [
72       "create terms in $id" => ['title' => $this->t('%vocabulary: Create terms', $args)],
73       "delete terms in $id" => ['title' => $this->t('%vocabulary: Delete terms', $args)],
74       "edit terms in $id" => ['title' => $this->t('%vocabulary: Edit terms', $args)],
75     ];
76   }
77
78 }