MenuItemContentDependencyCollector.php in Dependency Calculation 8
File
src/EventSubscriber/DependencyCollector/MenuItemContentDependencyCollector.php
View source
<?php
namespace Drupal\depcalc\EventSubscriber\DependencyCollector;
use Drupal\depcalc\DependencyCalculatorEvents;
use Drupal\depcalc\DependentEntityWrapper;
use Drupal\depcalc\Event\CalculateEntityDependenciesEvent;
class MenuItemContentDependencyCollector extends BaseDependencyCollector {
public static function getSubscribedEvents() {
$events[DependencyCalculatorEvents::CALCULATE_DEPENDENCIES][] = [
'onCalculateDependencies',
];
return $events;
}
public function onCalculateDependencies(CalculateEntityDependenciesEvent $event) {
$entity = $event
->getEntity();
if ($entity
->getEntityTypeId() !== 'menu_link_content') {
return;
}
$event
->getWrapper()
->addModuleDependencies([
'menu_link_content',
]);
$menu = $entity
->get('menu_name')
->first()
->getValue()['value'];
$menu = \Drupal::entityTypeManager()
->getStorage('menu')
->load($menu);
if ($menu && !$event
->getStack()
->hasDependency($menu
->uuid())) {
$menu_wrapper = new DependentEntityWrapper($menu);
$local_dependencies = [];
$this
->mergeDependencies($menu_wrapper, $event
->getStack(), $this
->getCalculator()
->calculateDependencies($menu_wrapper, $event
->getStack(), $local_dependencies));
$event
->addDependency($menu_wrapper);
}
$parent = $entity
->get('parent')
->first() ? $entity
->get('parent')
->first()
->getValue()['value'] : '';
if (!$parent) {
return;
}
[
$parent_type,
$uuid,
] = explode(':', $parent);
$parent_menu = \Drupal::service('entity.repository')
->loadEntityByUuid($parent_type, $uuid);
if ($parent_menu && !$event
->getStack()
->hasDependency($parent_menu
->uuid())) {
$parent_wrapper = new DependentEntityWrapper($parent_menu);
$local_dependencies = [];
$this
->mergeDependencies($parent_wrapper, $event
->getStack(), $this
->getCalculator()
->calculateDependencies($parent_wrapper, $event
->getStack(), $local_dependencies));
$event
->addDependency($parent_wrapper);
}
}
}