Created
July 6, 2016 02:34
-
-
Save acbramley/1127c4698a9ae86885e03716f47e3281 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\djr_views\Plugin\views\filter; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\taxonomy\Entity\Term; | |
use Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid; | |
use Drupal\views\Plugin\views\filter\InOperator; | |
/** | |
* Filter by term id. | |
* | |
* @ingroup views_filter_handlers | |
* | |
* @ViewsFilter("djr_views_term_uuid") | |
*/ | |
class TermUuid extends TaxonomyIndexTid { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function valueForm(&$form, FormStateInterface $form_state) { | |
parent::valueForm($form, $form_state); | |
// Alter the form options so the key (value that is saved) is the uuid. | |
$vocabulary = $this->vocabularyStorage->load($this->options['vid']); | |
$options = array(); | |
$query = \Drupal::entityQuery('taxonomy_term') | |
// @todo Sorting on vocabulary properties - | |
// https://www.drupal.org/node/1821274. | |
->sort('weight') | |
->sort('name') | |
->addTag('term_access'); | |
if ($this->options['limit']) { | |
$query->condition('vid', $vocabulary->id()); | |
} | |
$result = $query->execute(); | |
/** @var Term[] $terms */ | |
$terms = Term::loadMultiple($result); | |
foreach ($terms as $term) { | |
$options[$term->uuid()] = \Drupal::entityManager()->getTranslationFromContext($term)->label(); | |
} | |
$form['value']['#options'] = $options; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function query() { | |
// Convert uuid to tid at query time. | |
$uuids = $this->value; | |
$new_values = []; | |
/** @var \Drupal\Core\Entity\EntityRepositoryInterface $repository */ | |
$repository = \Drupal::service('entity.repository'); | |
foreach ($uuids as $uuid) { | |
$term = $repository->loadEntityByUuid('taxonomy_term', $uuid); | |
$new_values[$term->id()] = $term->id(); | |
} | |
$this->value = $new_values; | |
parent::query(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function adminSummary() { | |
// Set up $this->valueOptions for the parent summary. | |
$this->valueOptions = array(); | |
if ($this->value) { | |
$this->value = array_filter($this->value); | |
/** @var \Drupal\Core\Entity\EntityRepositoryInterface $repository */ | |
$repository = \Drupal::service('entity.repository'); | |
foreach ($this->value as $uuid) { | |
$term = $repository->loadEntityByUuid('taxonomy_term', $uuid); | |
$this->valueOptions[$term->uuid()] = \Drupal::entityManager()->getTranslationFromContext($term)->label(); | |
} | |
} | |
// Skip TaxonomyIndexTid::adminSummary cause it overwrites our stuff. | |
return InOperator::adminSummary(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment