comparison core/modules/layout_builder/src/LayoutBuilderOverridesPermissions.php @ 5:12f9dff5fda9 tip

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