You are here

class TermParentCollector in Dependency Calculation 8

Class TermParentCollector

Hierarchy

  • class \Drupal\depcalc\EventSubscriber\DependencyCollector\BaseDependencyCollector implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of TermParentCollector

1 string reference to 'TermParentCollector'
depcalc.services.yml in ./depcalc.services.yml
depcalc.services.yml
1 service uses TermParentCollector
term.parent.calculator in ./depcalc.services.yml
Drupal\depcalc\EventSubscriber\DependencyCollector\TermParentCollector

File

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

Namespace

Drupal\depcalc\EventSubscriber\DependencyCollector
View source
class TermParentCollector extends BaseDependencyCollector {

  /**
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * TermParentCollector constructor.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[DependencyCalculatorEvents::CALCULATE_DEPENDENCIES][] = [
      'onCalculateDependencies',
    ];
    return $events;
  }

  /**
   * Add parent terms to dependency calculation.
   *
   * @param \Drupal\depcalc\Event\CalculateEntityDependenciesEvent $event
   *   The CalculateEntityDependenciesEvent event.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  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.
        }
      }
    }
  }

}

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.
TermParentCollector::$database protected property
TermParentCollector::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
TermParentCollector::onCalculateDependencies public function Add parent terms to dependency calculation.
TermParentCollector::__construct public function TermParentCollector constructor.