Chris@18
|
1 <?php
|
Chris@18
|
2
|
Chris@18
|
3 namespace Drupal\layout_builder;
|
Chris@18
|
4
|
Chris@18
|
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
Chris@18
|
6 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
|
Chris@18
|
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@18
|
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
|
Chris@18
|
9 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@18
|
10
|
Chris@18
|
11 /**
|
Chris@18
|
12 * Provides dynamic permissions for Layout Builder overrides.
|
Chris@18
|
13 *
|
Chris@18
|
14 * @see \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage::access()
|
Chris@18
|
15 *
|
Chris@18
|
16 * @internal
|
Chris@18
|
17 * Dynamic permission callbacks are internal.
|
Chris@18
|
18 */
|
Chris@18
|
19 class LayoutBuilderOverridesPermissions implements ContainerInjectionInterface {
|
Chris@18
|
20
|
Chris@18
|
21 use StringTranslationTrait;
|
Chris@18
|
22
|
Chris@18
|
23 /**
|
Chris@18
|
24 * The entity type manager.
|
Chris@18
|
25 *
|
Chris@18
|
26 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
Chris@18
|
27 */
|
Chris@18
|
28 protected $entityTypeManager;
|
Chris@18
|
29
|
Chris@18
|
30 /**
|
Chris@18
|
31 * The entity type bundle info service.
|
Chris@18
|
32 *
|
Chris@18
|
33 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
|
Chris@18
|
34 */
|
Chris@18
|
35 protected $bundleInfo;
|
Chris@18
|
36
|
Chris@18
|
37 /**
|
Chris@18
|
38 * LayoutBuilderOverridesPermissions constructor.
|
Chris@18
|
39 *
|
Chris@18
|
40 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@18
|
41 * The entity type manager.
|
Chris@18
|
42 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info
|
Chris@18
|
43 * The bundle info service.
|
Chris@18
|
44 */
|
Chris@18
|
45 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info) {
|
Chris@18
|
46 $this->entityTypeManager = $entity_type_manager;
|
Chris@18
|
47 $this->bundleInfo = $bundle_info;
|
Chris@18
|
48 }
|
Chris@18
|
49
|
Chris@18
|
50 /**
|
Chris@18
|
51 * {@inheritdoc}
|
Chris@18
|
52 */
|
Chris@18
|
53 public static function create(ContainerInterface $container) {
|
Chris@18
|
54 return new static(
|
Chris@18
|
55 $container->get('entity_type.manager'),
|
Chris@18
|
56 $container->get('entity_type.bundle.info')
|
Chris@18
|
57 );
|
Chris@18
|
58 }
|
Chris@18
|
59
|
Chris@18
|
60 /**
|
Chris@18
|
61 * Returns an array of permissions.
|
Chris@18
|
62 *
|
Chris@18
|
63 * @return string[][]
|
Chris@18
|
64 * An array whose keys are permission names and whose corresponding values
|
Chris@18
|
65 * are defined in \Drupal\user\PermissionHandlerInterface::getPermissions().
|
Chris@18
|
66 */
|
Chris@18
|
67 public function permissions() {
|
Chris@18
|
68 $permissions = [];
|
Chris@18
|
69
|
Chris@18
|
70 /** @var \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface[] $entity_displays */
|
Chris@18
|
71 $entity_displays = $this->entityTypeManager->getStorage('entity_view_display')->loadByProperties(['third_party_settings.layout_builder.allow_custom' => TRUE]);
|
Chris@18
|
72 foreach ($entity_displays as $entity_display) {
|
Chris@18
|
73 $entity_type_id = $entity_display->getTargetEntityTypeId();
|
Chris@18
|
74 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
|
Chris@18
|
75 $bundle = $entity_display->getTargetBundle();
|
Chris@18
|
76 $args = [
|
Chris@18
|
77 '%entity_type' => $entity_type->getCollectionLabel(),
|
Chris@18
|
78 '@entity_type_singular' => $entity_type->getSingularLabel(),
|
Chris@18
|
79 '@entity_type_plural' => $entity_type->getPluralLabel(),
|
Chris@18
|
80 '%bundle' => $this->bundleInfo->getBundleInfo($entity_type_id)[$bundle]['label'],
|
Chris@18
|
81 ];
|
Chris@18
|
82 if ($entity_type->hasKey('bundle')) {
|
Chris@18
|
83 $permissions["configure all $bundle $entity_type_id layout overrides"] = [
|
Chris@18
|
84 'title' => $this->t('%entity_type - %bundle: Configure all layout overrides', $args),
|
Chris@18
|
85 'warning' => $this->t('Warning: Allows configuring the layout even if the user cannot edit the @entity_type_singular itself.', $args),
|
Chris@18
|
86 ];
|
Chris@18
|
87 $permissions["configure editable $bundle $entity_type_id layout overrides"] = [
|
Chris@18
|
88 'title' => $this->t('%entity_type - %bundle: Configure layout overrides for @entity_type_plural that the user can edit', $args),
|
Chris@18
|
89 ];
|
Chris@18
|
90 }
|
Chris@18
|
91 else {
|
Chris@18
|
92 $permissions["configure all $bundle $entity_type_id layout overrides"] = [
|
Chris@18
|
93 'title' => $this->t('%entity_type: Configure all layout overrides', $args),
|
Chris@18
|
94 'warning' => $this->t('Warning: Allows configuring the layout even if the user cannot edit the @entity_type_singular itself.', $args),
|
Chris@18
|
95 ];
|
Chris@18
|
96 $permissions["configure editable $bundle $entity_type_id layout overrides"] = [
|
Chris@18
|
97 'title' => $this->t('%entity_type: Configure layout overrides for @entity_type_plural that the user can edit', $args),
|
Chris@18
|
98 ];
|
Chris@18
|
99 }
|
Chris@18
|
100 }
|
Chris@18
|
101 return $permissions;
|
Chris@18
|
102 }
|
Chris@18
|
103
|
Chris@18
|
104 }
|