TaxonomyPermissions.php 2.0 KB

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