diff --git a/core/lib/Drupal/Core/Access/DependentAccessInterface.php b/core/lib/Drupal/Core/Access/DependentAccessInterface.php index 7611ecfcd0..64ae185309 100644 --- a/core/lib/Drupal/Core/Access/DependentAccessInterface.php +++ b/core/lib/Drupal/Core/Access/DependentAccessInterface.php @@ -25,19 +25,19 @@ /** * Sets the access dependency. * - * @param \Drupal\Core\Access\AccessibleInterface[] $access_dependencies - * The access dependencies. + * @param \Drupal\Core\Access\AccessibleInterface $access_dependency + * The object upon which access depends. * * @return $this */ - public function setAccessDependencies(array $access_dependencies); + public function setAccessDependency(AccessibleInterface $access_dependency); /** - * Gets the access dependencies. + * Gets the access dependency. * - * @return \Drupal\Core\Access\AccessibleInterface[] + * @return \Drupal\Core\Access\AccessibleInterface|null * The access dependency or NULL if none has been set. */ - public function getAccessDependencies(); + public function getAccessDependency(); } diff --git a/core/lib/Drupal/Core/Access/DependentAccessTrait.php b/core/lib/Drupal/Core/Access/DependentAccessTrait.php index 1f6a5b57ce..9429cf9565 100644 --- a/core/lib/Drupal/Core/Access/DependentAccessTrait.php +++ b/core/lib/Drupal/Core/Access/DependentAccessTrait.php @@ -8,25 +8,25 @@ trait DependentAccessTrait { /** - * The access dependencies. + * The access dependency. * - * @var \Drupal\Core\Access\AccessibleInterface[] + * @var \Drupal\Core\Access\AccessibleInterface */ - protected $accessDependencies = []; + protected $accessDependency; /** * {@inheritdoc} */ - public function setAccessDependencies(array $access_dependencies) { - $this->accessDependencies = $access_dependencies; + public function setAccessDependency(AccessibleInterface $access_dependency) { + $this->accessDependency = $access_dependency; return $this; } /** * {@inheritdoc} */ - public function getAccessDependencies() { - return $this->accessDependencies; + public function getAccessDependency() { + return $this->accessDependency; } } diff --git a/core/modules/block_content/src/BlockContentAccessControlHandler.php b/core/modules/block_content/src/BlockContentAccessControlHandler.php index 23e12bffed..84d6fe157b 100644 --- a/core/modules/block_content/src/BlockContentAccessControlHandler.php +++ b/core/modules/block_content/src/BlockContentAccessControlHandler.php @@ -2,7 +2,7 @@ namespace Drupal\block_content; -use Drupal\block_content\Event\BlockContentGetDependenciesEvent; +use Drupal\block_content\Event\BlockContentGetDependencyEvent; use Drupal\Core\Access\DependentAccessInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\EntityHandlerInterface; @@ -66,21 +66,21 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter if (!$entity instanceof DependentAccessInterface) { throw new \LogicException("Non-reusable block entities must implement \Drupal\Core\Access\DependentAccessInterface for access control."); } - $dependencies = $entity->getAccessDependencies(); - if (empty($dependencies)) { - // If access dependencies have not been set let modules set them. - $event = new BlockContentGetDependenciesEvent($entity); - $this->eventDispatcher->dispatch(BlockContentEvents::BLOCK_CONTENT_GET_DEPENDENCIES, $event); - $dependencies = $entity->getAccessDependencies(); - if (empty($dependencies)) { + $dependency = $entity->getAccessDependency(); + if (empty($dependency)) { + // If an access dependency has not been set let modules set one. + $event = new BlockContentGetDependencyEvent($entity); + $this->eventDispatcher->dispatch(BlockContentEvents::BLOCK_CONTENT_GET_DEPENDENCY, $event); + $dependency = $event->getAccessDependency(); + if (empty($dependency)) { return AccessResult::forbidden("Non-reusable blocks must set an access dependency for access control."); } } - foreach ($dependencies as $dependency) { - if (empty($dependency->in_preview)) { - $access = $access->andIf($dependency->access($operation, $account, TRUE)); - } + if (!$dependency instanceof EntityInterface || empty($dependency->in_preview)) { + /** @var \Drupal\Core\Entity\EntityInterface $dependency */ + $access = $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 index 2f5de4c161..2e3f7eaed8 100644 --- a/core/modules/block_content/src/BlockContentEvents.php +++ b/core/modules/block_content/src/BlockContentEvents.php @@ -5,24 +5,24 @@ /** * Defines events for the block_content module. * - * @see \Drupal\block_content\Event\BlockContentGetDependenciesEvent + * @see \Drupal\block_content\Event\BlockContentGetDependencyEvent */ final class BlockContentEvents { /** - * Name of the event when getting the dependencies of a non-reusable block. + * Name of the event when getting the dependency of a non-reusable block. * - * This event allows modules to set the dependencies of non-reusable block if - * \Drupal\block_content\Entity\BlockContent::setAccessDependencies has not - * been called. + * This event allows modules to set a dependency of non-reusable block if + * \Drupal\Core\Access\AccessDependentTrait::getAccessDependency has not been + * called. * * @Event * - * @see \Drupal\block_content\Event\BlockContentGetDependenciesEvent + * @see \Drupal\block_content\Event\BlockContentGetDependencyEvent * @see \Drupal\block_content\BlockContentAccessControlHandler::checkAccess() * * @var string */ - const BLOCK_CONTENT_GET_DEPENDENCIES = 'block_content.get_dependencies'; + const BLOCK_CONTENT_GET_DEPENDENCY = 'block_content.get_dependency'; } diff --git a/core/modules/block_content/src/BlockContentInterface.php b/core/modules/block_content/src/BlockContentInterface.php index d926c4800f..8f1a47c111 100644 --- a/core/modules/block_content/src/BlockContentInterface.php +++ b/core/modules/block_content/src/BlockContentInterface.php @@ -69,7 +69,7 @@ public function setReusable(); * * @return $this */ - public function setNonreusable(); + public function setNotReusable(); /** * Sets the theme value. diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index c90349dd22..b419705daf 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -319,7 +319,7 @@ public function setReusable() { /** * {@inheritdoc} */ - public function setNonreusable() { + public function setNotReusable() { return $this->set('reusable', FALSE); } diff --git a/core/modules/block_content/src/Event/BlockContentGetDependenciesEvent.php b/core/modules/block_content/src/Event/BlockContentGetDependenciesEvent.php deleted file mode 100644 index fcfcf2ccf4..0000000000 --- a/core/modules/block_content/src/Event/BlockContentGetDependenciesEvent.php +++ /dev/null @@ -1,40 +0,0 @@ -blockContent = $blockContent; - } - - /** - * Gets the block content entity. - * - * @return \Drupal\block_content\BlockContentInterface - * The block content entity. - */ - public function getBlockContent() { - return $this->blockContent; - } - -} diff --git a/core/modules/block_content/src/Event/BlockContentGetDependencyEvent.php b/core/modules/block_content/src/Event/BlockContentGetDependencyEvent.php new file mode 100644 index 0000000000..977c8593fc --- /dev/null +++ b/core/modules/block_content/src/Event/BlockContentGetDependencyEvent.php @@ -0,0 +1,68 @@ +blockContent = $blockContent; + } + + /** + * Gets the block content entity. + * + * @return \Drupal\block_content\BlockContentInterface + * The block content entity. + */ + public function getBlockContentEntity() { + return $this->blockContent; + } + + /** + * Gets the access dependency. + * + * @return \Drupal\Core\Access\AccessibleInterface + * The access dependency. + */ + public function getAccessDependency() { + return $this->accessDependency; + } + + /** + * Sets the access dependency. + * + * @param \Drupal\Core\Access\AccessibleInterface $access_dependency + * The access dependency. + */ + public function setAccessDependency(AccessibleInterface $access_dependency) { + $this->accessDependency = $access_dependency; + } + +} diff --git a/core/modules/block_content/tests/src/Kernel/BlockContentAccessHandlerTest.php b/core/modules/block_content/tests/src/Kernel/BlockContentAccessHandlerTest.php index 142dd79500..47cf416622 100644 --- a/core/modules/block_content/tests/src/Kernel/BlockContentAccessHandlerTest.php +++ b/core/modules/block_content/tests/src/Kernel/BlockContentAccessHandlerTest.php @@ -96,7 +96,7 @@ protected function setUp() { */ public function testAccess($operation, $published, $reusable, $permissions, $parent_access, $expected_access) { $published ? $this->blockEntity->setPublished() : $this->blockEntity->setUnpublished(); - $reusable ? $this->blockEntity->setReusable() : $this->blockEntity->setNonreusable(); + $reusable ? $this->blockEntity->setReusable() : $this->blockEntity->setNotReusable(); $user = User::create([ 'name' => 'Someone', @@ -132,7 +132,7 @@ public function testAccess($operation, $published, $reusable, $permissions, $par ->willReturn($expected_parent_result) ->shouldBeCalled(); - $this->blockEntity->setAccessDependencies([$parent_entity->reveal()]); + $this->blockEntity->setAccessDependency($parent_entity->reveal()); } $this->blockEntity->save(); diff --git a/core/modules/block_content/tests/src/Kernel/BlockContentDeriverTest.php b/core/modules/block_content/tests/src/Kernel/BlockContentDeriverTest.php index ddb20ffbd2..de5714e667 100644 --- a/core/modules/block_content/tests/src/Kernel/BlockContentDeriverTest.php +++ b/core/modules/block_content/tests/src/Kernel/BlockContentDeriverTest.php @@ -55,7 +55,7 @@ public function testReusableBlocksOnlyAreDerived() { $this->assertTrue($block_manager->hasDefinition($plugin_id)); // Set the block not to be reusable. - $block_content->setNonreusable(); + $block_content->setNotReusable(); $block_content->save(); // Ensure the non-reusable block content is not provided a derivative block