View source
<?php
namespace Drupal\media_directories\Plugin\views\filter;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermStorageInterface;
use Drupal\taxonomy\VocabularyStorageInterface;
use Drupal\views\Plugin\views\filter\ManyToOne;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MediaDirectory extends ManyToOne {
public $validatedExposedInput = [];
protected $vocabularyStorage;
protected $termStorage;
protected $configFactory;
protected $entityRepository;
public function __construct(array $configuration, $plugin_id, $plugin_definition, VocabularyStorageInterface $vocabulary_storage, TermStorageInterface $term_storage, ConfigFactoryInterface $configFactory, EntityRepositoryInterface $entity_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->vocabularyStorage = $vocabulary_storage;
$this->termStorage = $term_storage;
$this->configFactory = $configFactory;
$this->entityRepository = $entity_repository;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager')
->getStorage('taxonomy_vocabulary'), $container
->get('entity_type.manager')
->getStorage('taxonomy_term'), $container
->get('config.factory'), $container
->get('entity.repository'));
}
protected function valueForm(&$form, FormStateInterface $form_state) {
$config = $this->configFactory
->get('media_directories.settings');
$vid = $config
->get('directory_taxonomy');
if (empty($vid)) {
$settings_url = Url::fromRoute('media_directories.config_form');
$form['markup'] = [
'#markup' => '<div class="js-form-item form-item">' . $this
->t('Vocabulary is not selected. Please select it in the <a href=":url">settings</a>.', [
':url' => $settings_url
->toString(),
]) . '</div>',
];
$form['value'] = [
'#type' => 'hidden',
'#default_value' => MEDIA_DIRECTORY_ROOT,
];
return;
}
$vocabulary = $this->vocabularyStorage
->load($vid);
$tree = $this->termStorage
->loadTree($vocabulary
->id(), 0, NULL, TRUE);
$options = [];
if ($tree) {
foreach ($tree as $term) {
$choice = new \stdClass();
$choice->option = [
$term
->id() => str_repeat('−', $term->depth + 1) . ' ' . $this->entityRepository
->getTranslationFromContext($term)
->label(),
];
$options[] = $choice;
}
}
$default_value = (array) $this->value;
if ($exposed = $form_state
->get('exposed')) {
$identifier = $this->options['expose']['identifier'];
if (!empty($this->options['expose']['reduce'])) {
$options = $this
->reduceValueOptions($options);
if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
$default_value = [];
}
}
if (empty($this->options['expose']['multiple'])) {
if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
$default_value = 'All';
}
elseif (empty($default_value)) {
$keys = array_keys($options);
$default_value = array_shift($keys);
}
elseif ($default_value == [
'',
]) {
$default_value = 'All';
}
else {
$copy = $default_value;
$default_value = array_shift($copy);
}
}
}
$form['value'] = [
'#type' => 'select',
'#title' => $this
->t('Select terms from vocabulary @voc', [
'@voc' => $vocabulary
->label(),
]),
'#multiple' => TRUE,
'#options' => $options,
'#size' => min(9, count($options)),
'#default_value' => $default_value,
];
$user_input = $form_state
->getUserInput();
if ($exposed && isset($identifier) && !isset($user_input[$identifier])) {
$user_input[$identifier] = $default_value;
$form_state
->setUserInput($user_input);
}
if (!$form_state
->get('exposed')) {
$this->helper
->buildOptionsForm($form, $form_state);
$form['value']['#description'] = t('Leave blank for all. Otherwise, the first selected term will be the default instead of "Any".');
}
}
protected function exposedTranslate(&$form, $type) {
parent::exposedTranslate($form, $type);
if (isset($form['#type']) && $form['#type'] === 'select') {
$config = $this->configFactory
->get('media_directories.settings');
if ($config
->get('all_files_in_root')) {
$form['#options']['All'] = $this
->t('All directories');
}
else {
$form['#options']['All'] = $this
->t('Root directory');
}
}
}
public function query() {
$this
->ensureMyTable();
$config = $this->configFactory
->get('media_directories.settings');
if ($this->validatedExposedInput[0] === 'All') {
$this->query
->addWhereExpression(0, "{$this->tableAlias}.{$this->realField} IS NULL");
if ($config
->get('all_files_in_root')) {
$this->query
->setWhereGroup('OR', 0);
$this->query
->addWhereExpression(0, "{$this->tableAlias}.{$this->realField} IS NOT NULL");
}
}
else {
parent::query();
}
}
public function acceptExposedInput($input) {
if (empty($this->options['exposed'])) {
return TRUE;
}
if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
$this->operator = $input[$this->options['expose']['operator_id']];
}
if (!empty($this->view->is_attachment) && $this->view->display_handler
->usesExposed()) {
$this->validatedExposedInput = (array) $this->view->exposed_raw_input[$this->options['expose']['identifier']];
}
if ($this->operator == 'empty' || $this->operator == 'not empty') {
return TRUE;
}
$rc = parent::acceptExposedInput($input);
$value = $input[$this->options['expose']['identifier']];
if (!$rc && $value === 'All') {
$rc = TRUE;
$this->validatedExposedInput = [
$value,
];
}
if ($rc) {
if (isset($this->validatedExposedInput)) {
$this->value = $this->validatedExposedInput;
}
}
return $rc;
}
public function validateExposed(&$form, FormStateInterface $form_state) {
if (empty($this->options['exposed'])) {
return;
}
$identifier = $this->options['expose']['identifier'];
if ($form_state
->getValue($identifier) != 'All') {
$this->validatedExposedInput = (array) $form_state
->getValue($identifier);
}
}
protected function valueSubmit($form, FormStateInterface $form_state) {
}
public function buildExposeForm(&$form, FormStateInterface $form_state) {
parent::buildExposeForm($form, $form_state);
$form['error_message'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display error message'),
'#default_value' => !empty($this->options['error_message']),
];
}
public function adminSummary() {
$this->valueOptions = [];
if ($this->value) {
$this->value = array_filter($this->value);
$terms = Term::loadMultiple($this->value);
foreach ($terms as $term) {
$this->valueOptions[$term
->id()] = $this->entityRepository
->getTranslationFromContext($term)
->label();
}
}
return parent::adminSummary();
}
public function getCacheContexts() {
$contexts = parent::getCacheContexts();
$contexts[] = 'user';
return $contexts;
}
public function calculateDependencies() {
$config = $this->configFactory
->get('media_directories.settings');
$vid = $config
->get('directory_taxonomy');
$dependencies = parent::calculateDependencies();
$vocabulary = $vid ? $this->vocabularyStorage
->load($vid) : NULL;
if ($vocabulary) {
$dependencies[$vocabulary
->getConfigDependencyKey()][] = $vocabulary
->getConfigDependencyName();
}
foreach ($this->termStorage
->loadMultiple($this->options['value']) as $term) {
$dependencies[$term
->getConfigDependencyKey()][] = $term
->getConfigDependencyName();
}
return $dependencies;
}
}