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