You are here

public function TermParentCollector::onCalculateDependencies in Dependency Calculation 8

Add parent terms to dependency calculation.

Parameters

\Drupal\depcalc\Event\CalculateEntityDependenciesEvent $event: The CalculateEntityDependenciesEvent event.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/EventSubscriber/DependencyCollector/TermParentCollector.php, line 47

Class

TermParentCollector
Class TermParentCollector

Namespace

Drupal\depcalc\EventSubscriber\DependencyCollector

Code

public function onCalculateDependencies(CalculateEntityDependenciesEvent $event) {
  if ($event
    ->getWrapper()
    ->getEntityTypeId() == 'taxonomy_term') {

    /** @var \Drupal\taxonomy\TermInterface $term */
    $term = $event
      ->getEntity();

    /** @var \Drupal\taxonomy\TermStorage $storage */
    $storage = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term');
    $parents = $storage
      ->loadParents($term
      ->id());
    foreach ($parents as $parent) {
      if (!$event
        ->getStack()
        ->hasDependency($parent
        ->uuid())) {
        $parent_wrapper = new DependentEntityWrapper($parent);
        $local_dependencies = [];
        $this
          ->mergeDependencies($parent_wrapper, $event
          ->getStack(), $this
          ->getCalculator()
          ->calculateDependencies($parent_wrapper, $event
          ->getStack(), $local_dependencies));
        $event
          ->addDependency($parent_wrapper);

        // Child term's dependencies already calculated. Adding parent is sufficient.
      }
    }
  }
}