You are here

class MenuItemContentDependencyCollector in Dependency Calculation 8

Hierarchy

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'
depcalc.services.yml in ./depcalc.services.yml
depcalc.services.yml
1 service uses MenuItemContentDependencyCollector
menu_item_content.parent.calculator in ./depcalc.services.yml
Drupal\depcalc\EventSubscriber\DependencyCollector\MenuItemContentDependencyCollector

File

src/EventSubscriber/DependencyCollector/MenuItemContentDependencyCollector.php, line 9

Namespace

Drupal\depcalc\EventSubscriber\DependencyCollector
View 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

Namesort descending Modifiers Type Description Overrides
BaseDependencyCollector::getCalculator protected function Gets the dependency calculator.
BaseDependencyCollector::mergeDependencies protected function Properly adds dependencies and their modules to a wrapper object.
MenuItemContentDependencyCollector::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
MenuItemContentDependencyCollector::onCalculateDependencies public function