|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\block_diff_ui; |
| 4 | + |
| 5 | +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; |
| 6 | +use Drupal\Core\Entity\EntityInterface; |
| 7 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 8 | +use Drupal\Core\StringTranslation\StringTranslationTrait; |
| 9 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * Provides dynamic permissions for block content of different types. |
| 13 | + */ |
| 14 | +class BlockContentPermissions implements ContainerInjectionInterface { |
| 15 | + |
| 16 | + use StringTranslationTrait; |
| 17 | + |
| 18 | + /** |
| 19 | + * The entity type manager service. |
| 20 | + * |
| 21 | + * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
| 22 | + */ |
| 23 | + protected $entityTypeManager; |
| 24 | + |
| 25 | + /** |
| 26 | + * BlockContentPermissions constructor. |
| 27 | + * |
| 28 | + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
| 29 | + * The entity type manager service. |
| 30 | + */ |
| 31 | + public function __construct(EntityTypeManagerInterface $entity_type_manager) { |
| 32 | + $this->entityTypeManager = $entity_type_manager; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * {@inheritdoc} |
| 37 | + */ |
| 38 | + public static function create(ContainerInterface $container) { |
| 39 | + return new static($container->get('entity_type.manager')); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Returns an array of block content type permissions. |
| 44 | + * |
| 45 | + * @return array |
| 46 | + * The block content type permissions. |
| 47 | + * @see \Drupal\user\PermissionHandlerInterface::getPermissions() |
| 48 | + */ |
| 49 | + public function blockTypePermissions() { |
| 50 | + $perms = []; |
| 51 | + // Generate block content permissions for all block content types. |
| 52 | + $block_types = $this->entityTypeManager |
| 53 | + ->getStorage('block_content_type')->loadMultiple(); |
| 54 | + foreach ($block_types as $type) { |
| 55 | + $perms += $this->buildPermissions($type); |
| 56 | + } |
| 57 | + return $perms; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns a list of block content permissions for a given block type. |
| 62 | + * |
| 63 | + * @param EntityInterface $type |
| 64 | + * The block type. |
| 65 | + * |
| 66 | + * @return array |
| 67 | + * An associative array of permission names and descriptions. |
| 68 | + */ |
| 69 | + protected function buildPermissions(EntityInterface $type) { |
| 70 | + $type_id = $type->id(); |
| 71 | + $type_params = ['%type_name' => $type->label()]; |
| 72 | + |
| 73 | + return [ |
| 74 | + "view $type_id revisions" => [ |
| 75 | + 'title' => $this->t('%type_name: View revisions', $type_params), |
| 76 | + 'description' => t('To view a revision, you also need permission to view the block content.'), |
| 77 | + ], |
| 78 | + "revert $type_id revisions" => [ |
| 79 | + 'title' => $this->t('%type_name: Revert revisions', $type_params), |
| 80 | + 'description' => t('To revert a revision, you also need permission to edit the block content.'), |
| 81 | + ], |
| 82 | + "delete $type_id revisions" => [ |
| 83 | + 'title' => $this->t('%type_name: Delete revisions', $type_params), |
| 84 | + 'description' => $this->t('To delete a revision, you also need permission to delete the block content.'), |
| 85 | + ], |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments