entity_hierarchy-8.x-2.24/src/Plugin/views/argument/HierarchyIsParentOfEntity.php
src/Plugin/views/argument/HierarchyIsParentOfEntity.php
<?php namespace Drupal\entity_hierarchy\Plugin\views\argument; use Drupal\Core\Form\FormStateInterface; use Drupal\entity_hierarchy\Storage\Record; use Drupal\views\Plugin\views\query\Sql; /** * Argument to limit to parent of an entity. * * @ingroup views_argument_handlers * * @ViewsArgument("entity_hierarchy_argument_is_parent_of_entity") */ class HierarchyIsParentOfEntity extends EntityHierarchyArgumentPluginBase { /** * Set up the query for this argument. * * The argument sent may be found at $this->argument. */ public function query($group_by = FALSE) { $this->ensureMyTable(); // Load the actual entity. $filtered = FALSE; if ($entity = $this->loadEntity()) { $queryBuilder = $this->queryBuilderFactory->get($this->configuration['entity_hierarchy_field'], $entity->getEntityTypeId()); $ancestors = $queryBuilder->findAncestors($entity)->filter(fn (Record $node) => $node->getId() !== (int) $entity->id()); if ($this->options['depth']) { $depth = -1 * ($queryBuilder->findDepth($entity) - (int) $this->options['depth']); $ancestors = $ancestors->filter(fn (Record $node) => $node->getDepth() >= $depth); } $parents = $ancestors->map(fn (Record $node) => $node->getId()); assert($this->query instanceof Sql); if (!$parents) { $this->query->addWhereExpression(0, '1 <> 1'); return; } $this->query->addWhere(0, "$this->tableAlias.$this->realField", $parents, 'IN'); $filtered = TRUE; } // The entity doesn't exist, or isn't in the tree and hence has no parents. if (!$filtered) { // Add a killswitch. $this->query->addWhereExpression(0, '1 <> 1'); } } /** * {@inheritdoc} */ public function buildOptionsForm(&$form, FormStateInterface $form_state): void { parent::buildOptionsForm($form, $form_state); $form['depth']['#description'] = $this->t('Filter to parent that are at most this many levels higher than their parent. E.g. for immediate parent, select 1.'); } }