class TermParentCollector in Dependency Calculation 8
Class TermParentCollector
Hierarchy
- class \Drupal\depcalc\EventSubscriber\DependencyCollector\BaseDependencyCollector implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
- class \Drupal\depcalc\EventSubscriber\DependencyCollector\TermParentCollector
Expanded class hierarchy of TermParentCollector
1 string reference to 'TermParentCollector'
1 service uses TermParentCollector
File
- src/
EventSubscriber/ DependencyCollector/ TermParentCollector.php, line 13
Namespace
Drupal\depcalc\EventSubscriber\DependencyCollectorView 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
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. | |
TermParentCollector:: |
protected | property | ||
TermParentCollector:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
TermParentCollector:: |
public | function | Add parent terms to dependency calculation. | |
TermParentCollector:: |
public | function | TermParentCollector constructor. |