TaxonomyPermissions.php 2.3 KB

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