class MenuItemContentDependencyCollector in Dependency Calculation 8
Hierarchy
- class \Drupal\depcalc\EventSubscriber\DependencyCollector\BaseDependencyCollector implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
- class \Drupal\depcalc\EventSubscriber\DependencyCollector\MenuItemContentDependencyCollector
Expanded class hierarchy of MenuItemContentDependencyCollector
1 file declares its use of MenuItemContentDependencyCollector
- MenuDependencyTest.php in tests/
src/ Kernel/ EventSubscriber/ DependencyCollector/ MenuDependencyTest.php
1 string reference to 'MenuItemContentDependencyCollector'
1 service uses MenuItemContentDependencyCollector
File
- src/
EventSubscriber/ DependencyCollector/ MenuItemContentDependencyCollector.php, line 9
Namespace
Drupal\depcalc\EventSubscriber\DependencyCollectorView source
class MenuItemContentDependencyCollector extends BaseDependencyCollector {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[DependencyCalculatorEvents::CALCULATE_DEPENDENCIES][] = [
'onCalculateDependencies',
];
return $events;
}
public function onCalculateDependencies(CalculateEntityDependenciesEvent $event) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$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);
/** @var \Drupal\Core\Entity\EntityInterface $parent_menu */
$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);
// Child menu_link_content entity's dependencies already calculated.
// Adding parent is sufficient.
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BaseDependencyCollector:: |
protected | function | Gets the dependency calculator. | |
BaseDependencyCollector:: |
protected | function | Properly adds dependencies and their modules to a wrapper object. | |
MenuItemContentDependencyCollector:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
MenuItemContentDependencyCollector:: |
public | function |