HierarchyIsParentOfEntity.php in Entity Reference Hierarchy 3.x
File
src/Plugin/views/argument/HierarchyIsParentOfEntity.php
View source
<?php
namespace Drupal\entity_hierarchy\Plugin\views\argument;
use Drupal\Core\Form\FormStateInterface;
class HierarchyIsParentOfEntity extends EntityHierarchyArgumentPluginBase {
public function query($group_by = FALSE) {
$this
->ensureMyTable();
$filtered = FALSE;
if ($entity = $this
->loadEntity()) {
$stub = $this->nodeKeyFactory
->fromEntity($entity);
if ($node = $this
->getTreeStorage()
->getNode($stub)) {
$filtered = TRUE;
$child_left_token = ':child_left_' . $this->tableAlias;
$expression = "{$this->tableAlias}.{$this->realField} < {$child_left_token} AND {$this->tableAlias}.right_pos > {$child_left_token}";
$arguments = [
$child_left_token => $node
->getLeft(),
];
if ($depth = $this->options['depth']) {
$depth_token = ':depth_' . $this->tableAlias;
$expression .= " AND {$this->tableAlias}.depth <= {$depth_token}}";
$arguments[$depth_token] = $node
->getDepth() - $depth;
}
$this->query
->addWhereExpression(0, $expression, $arguments);
}
}
if (!$filtered) {
$this->query
->addWhereExpression(0, '1 <> 1');
}
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
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.');
}
}