Skip to content
Snippets Groups Projects

Issue #3002175: Custom blocks created in inline forms should not be reusable

Files
7
<?php
namespace Drupal\inline_entity_form\EventSubscriber;
use Drupal\block_content\BlockContentEvents;
use Drupal\block_content\BlockContentInterface;
use Drupal\block_content\Event\BlockContentGetDependencyEvent;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\ContextProvider\NodeRouteContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Override Layout Builder SetInlineBlockDependencyWithContextTranslation.
*
* Handles handle nested blocks.
*
* This is copied from the version in layout_builder_at, except for skipping
* the test isBlockRevisionUsedInEntity().
*/
class SetInlineBlockDependencyNested implements EventSubscriberInterface {
/**
* The currently active route match object.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected RouteMatchInterface $routeMatch;
/**
* The node route context service.
*
* @var \Drupal\node\ContextProvider\NodeRouteContext
*/
protected NodeRouteContext $nodeRouteContext;
/**
* Constructs event subscriber.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The currently active route match object.
* @param \Drupal\node\ContextProvider\NodeRouteContext $node_route_context
* The node route context service.
*/
public function __construct(RouteMatchInterface $route_match, NodeRouteContext $node_route_context) {
$this->routeMatch = $route_match;
$this->nodeRouteContext = $node_route_context;
}
/**
* Handles the BlockContentEvents::INLINE_BLOCK_GET_DEPENDENCY event.
*
* @param \Drupal\block_content\Event\BlockContentGetDependencyEvent $event
* The event.
*
* @throws \Drupal\Component\Plugin\Exception\ContextException
*/
public function onGetDependency(BlockContentGetDependencyEvent $event) {
if ($dependency = $this->getInlineBlockDependency($event->getBlockContentEntity())) {
$event->setAccessDependency($dependency);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
BlockContentEvents::BLOCK_CONTENT_GET_DEPENDENCY => [
'onGetDependency',
-100,
],
];
}
/**
* Get the access dependency of an inline block.
*
* @param \Drupal\block_content\BlockContentInterface $block_content
* The block content entity.
*
* @return \Drupal\Core\Entity\EntityInterface|null
* Returns the entity dependency.
*
* @throws \Drupal\Component\Plugin\Exception\ContextException
*/
protected function getInlineBlockDependency(BlockContentInterface $block_content) {
// @todo Find a better way to get child blocks dependencies.
// Try to extract the node route context.
$contexts = $this->nodeRouteContext->getRuntimeContexts([]);
/** @var \Drupal\Core\Plugin\Context\Context $nodeContext */
$nodeContext = $contexts['node'];
$entity = $nodeContext->getContextValue();
// If we couldn't retrieve the node from the route then we try to get the
// section storage.
if (!$entity) {
$entity = $this->routeMatch->getParameter('section_storage');
}
return $entity;
}
}
Loading