LayoutBuilderDependencyCollectorBase.php in Dependency Calculation 8
File
src/EventSubscriber/LayoutBuilderComponentDepencyCollector/LayoutBuilderDependencyCollectorBase.php
View source
<?php
namespace Drupal\depcalc\EventSubscriber\LayoutBuilderComponentDepencyCollector;
use Drupal\Core\Layout\LayoutPluginManagerInterface;
use Drupal\depcalc\DependencyCalculatorEvents;
use Drupal\depcalc\DependentEntityWrapper;
use Drupal\depcalc\Event\CalculateEntityDependenciesEvent;
use Drupal\depcalc\Event\SectionComponentDependenciesEvent;
use Drupal\depcalc\EventSubscriber\DependencyCollector\BaseDependencyCollector;
use Drupal\layout_builder\Section;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
abstract class LayoutBuilderDependencyCollectorBase extends BaseDependencyCollector {
protected $dispatcher;
protected $layoutPluginManager;
public function __construct(EventDispatcherInterface $dispatcher, LayoutPluginManagerInterface $layoutPluginManager = NULL) {
$this->dispatcher = $dispatcher;
$this->layoutPluginManager = $layoutPluginManager;
}
protected function addSectionDependencies(CalculateEntityDependenciesEvent $event, Section $section) {
$layout_id = $section
->getLayoutId();
$layout_plugin_definition = $this->layoutPluginManager
->getDefinition($layout_id);
$event
->setModuleDependencies([
$layout_plugin_definition
->getProvider(),
]);
}
protected function addComponentDependencies(CalculateEntityDependenciesEvent $event, array $components) {
foreach ($components as $component) {
$componentEvent = new SectionComponentDependenciesEvent($component);
$this->dispatcher
->dispatch(DependencyCalculatorEvents::SECTION_COMPONENT_DEPENDENCIES_EVENT, $componentEvent);
$this
->addSectionComponentEntityDependencies($event, $componentEvent
->getEntityDependencies());
$event
->setModuleDependencies($componentEvent
->getModuleDependencies());
}
}
protected function addSectionComponentEntityDependencies(CalculateEntityDependenciesEvent $event, array $entities) {
foreach ($entities as $entity) {
$item_entity_wrapper = new DependentEntityWrapper($entity);
$local_dependencies = [];
$this
->mergeDependencies($item_entity_wrapper, $event
->getStack(), $this
->getCalculator()
->calculateDependencies($item_entity_wrapper, $event
->getStack(), $local_dependencies));
$event
->addDependency($item_entity_wrapper);
}
}
}