View source
<?php
namespace Drupal\entity_hierarchy\Plugin\views\argument;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_hierarchy\Storage\NestedSetNodeKeyFactory;
use Drupal\entity_hierarchy\Storage\NestedSetStorageFactory;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class EntityHierarchyArgumentPluginBase extends ArgumentPluginBase {
protected $nestedSetStorageFactory;
protected $entityTypeManager;
protected $nodeKeyFactory;
protected $nestedSetPrefix = '';
public function __construct(array $configuration, $plugin_id, $plugin_definition, NestedSetStorageFactory $nestedSetStorageFactory, EntityTypeManagerInterface $entityTypeManager, NestedSetNodeKeyFactory $nodeKeyFactory, Connection $database) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->nestedSetStorageFactory = $nestedSetStorageFactory;
$this->entityTypeManager = $entityTypeManager;
$this->nodeKeyFactory = $nodeKeyFactory;
$this->nestedSetPrefix = $database
->tablePrefix();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_hierarchy.nested_set_storage_factory'), $container
->get('entity_type.manager'), $container
->get('entity_hierarchy.nested_set_node_factory'), $container
->get('database'));
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$options = range(1, 50);
$form['depth'] = [
'#type' => 'select',
'#empty_option' => $this
->t('No restriction'),
'#empty_value' => 0,
'#options' => array_combine($options, $options),
'#title' => $this
->t('Depth'),
'#default_value' => $this->options['depth'],
'#description' => $this
->t('Filter to children that are at most this many levels deeper than their parent. E.g. for immediate children, select 1.'),
];
parent::buildOptionsForm($form, $form_state);
}
protected function getTreeStorage() {
return $this->nestedSetStorageFactory
->fromTableName($this->nestedSetPrefix . $this->table);
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['depth'] = [
'default' => 0,
];
return $options;
}
protected function loadEntity() {
return $this->entityTypeManager
->getStorage($this
->getEntityType())
->load($this->argument);
}
}