diff --git a/core/modules/block_content/block_content.post_update.php b/core/modules/block_content/block_content.post_update.php index 6b323f99f8..3ad95643bd 100644 --- a/core/modules/block_content/block_content.post_update.php +++ b/core/modules/block_content/block_content.post_update.php @@ -8,7 +8,7 @@ use Drupal\Core\Config\Entity\ConfigEntityUpdater; /** - * Adds 'reusable filter to Custom Block views. + * Adds a 'reusable' filter to Custom Block views. */ function block_content_post_update_add_views_reusable_filter(&$sandbox = NULL) { $data_table = \Drupal::entityTypeManager() diff --git a/core/modules/block_content/src/BlockContentAccessControlHandler.php b/core/modules/block_content/src/BlockContentAccessControlHandler.php index 55bebc03b5..c7138c0238 100644 --- a/core/modules/block_content/src/BlockContentAccessControlHandler.php +++ b/core/modules/block_content/src/BlockContentAccessControlHandler.php @@ -2,18 +2,53 @@ namespace Drupal\block_content; +use Drupal\block_content\Event\BlockContentGetDependencyEvent; use Drupal\Core\Access\AccessDependentInterface; use Drupal\Core\Access\AccessResult; +use Drupal\Core\Entity\EntityHandlerInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityAccessControlHandler; +use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Session\AccountInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Defines the access control handler for the custom block entity type. * * @see \Drupal\block_content\Entity\BlockContent */ -class BlockContentAccessControlHandler extends EntityAccessControlHandler { +class BlockContentAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface { + + /** + * The event dispatcher. + * + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + */ + protected $eventDispatcher; + + /** + * BlockContentAccessControlHandler constructor. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. + * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher + * The event dispatcher. + */ + public function __construct(EntityTypeInterface $entity_type, EventDispatcherInterface $dispatcher) { + parent::__construct($entity_type); + $this->eventDispatcher = $dispatcher; + } + + /** + * {@inheritdoc} + */ + public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { + return new static( + $entity_type, + $container->get('event_dispatcher') + ); + } /** * {@inheritdoc} @@ -33,9 +68,19 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter } $dependency = $entity->getAccessDependency(); if (empty($dependency)) { - return AccessResult::forbidden("Non-reusable blocks must set an access dependency for access control."); + // If an access dependency has not been set let modules set one. + $event = new BlockContentGetDependencyEvent($entity); + $this->eventDispatcher->dispatch(BlockContentEvents::INLINE_BLOCK_GET_DEPENDENCY, $event); + $dependency = $entity->getAccessDependency(); + if (empty($dependency)) { + return AccessResult::forbidden("Non-reusable blocks must set an access dependency for access control."); + } + } + if (empty($dependency->in_preview)) { + /** @var \Drupal\Core\Entity\EntityInterface $dependency */ + $access = $access->andIf($dependency->access($operation, $account, TRUE)); } - $access->andIf($dependency->access($operation, $account, TRUE)); + } return $access; } diff --git a/core/modules/block_content/src/BlockContentEvents.php b/core/modules/block_content/src/BlockContentEvents.php new file mode 100644 index 0000000000..865c07aa98 --- /dev/null +++ b/core/modules/block_content/src/BlockContentEvents.php @@ -0,0 +1,28 @@ +blockContent = $blockContent; + } + + /** + * Gets the block content entity. + * + * @return \Drupal\block_content\BlockContentInterface + * The block content entity. + */ + public function getBlockContent() { + return $this->blockContent; + } + +}