KeywordId.php in Bibliography & Citation 2.0.x
File
modules/bibcite_entity/src/Plugin/views/filter/KeywordId.php
View source
<?php
namespace Drupal\bibcite_entity\Plugin\views\filter;
use Drupal\Core\Entity\Element\EntityAutocomplete;
use Drupal\Core\Form\FormStateInterface;
use Drupal\bibcite_entity\Entity\Keyword;
use Drupal\views\Plugin\views\filter\ManyToOne;
class KeywordId extends ManyToOne {
protected $alwaysMultiple = TRUE;
private $validatedExposedInput;
protected function valueForm(&$form, FormStateInterface $form_state) {
$keywords = $this->value ? Keyword::loadMultiple($this->value) : [];
$default_value = EntityAutocomplete::getEntityLabels($keywords);
$form['value'] = [
'#type' => 'entity_autocomplete',
'#title' => $this
->t('Keywords'),
'#description' => $this
->t('Enter a comma separated list of keywords.'),
'#target_type' => 'bibcite_keyword',
'#tags' => TRUE,
'#default_value' => $default_value,
'#process_default_value' => $this
->isExposed(),
];
$user_input = $form_state
->getUserInput();
if ($form_state
->get('exposed') && !isset($user_input[$this->options['expose']['identifier']])) {
$user_input[$this->options['expose']['identifier']] = $default_value;
$form_state
->setUserInput($user_input);
}
}
protected function valueValidate($form, FormStateInterface $form_state) {
$ids = [];
if ($values = $form_state
->getValue([
'options',
'value',
])) {
foreach ($values as $value) {
$ids[] = $value['target_id'];
}
sort($ids);
}
$form_state
->setValue([
'options',
'value',
], $ids);
}
public function acceptExposedInput($input) {
$rc = parent::acceptExposedInput($input);
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;
}
if (empty($this->options['expose']['identifier'])) {
return;
}
$identifier = $this->options['expose']['identifier'];
$input = $form_state
->getValue($identifier);
if ($this->options['is_grouped'] && isset($this->options['group_info']['group_items'][$input])) {
$this->operator = $this->options['group_info']['group_items'][$input]['operator'];
$input = $this->options['group_info']['group_items'][$input]['value'];
}
$ids = [];
$values = $form_state
->getValue($identifier);
if ($values && (!$this->options['is_grouped'] || $this->options['is_grouped'] && $input != 'All')) {
foreach ($values as $value) {
$ids[] = $value['target_id'];
}
}
if ($ids) {
$this->validatedExposedInput = $ids;
}
}
protected function valueSubmit($form, FormStateInterface $form_state) {
}
public function getValueOptions() {
return $this->valueOptions;
}
public function adminSummary() {
$this->valueOptions = [];
if ($this->value) {
$result = \Drupal::entityTypeManager()
->getStorage('bibcite_keyword')
->loadByProperties([
'id' => $this->value,
]);
foreach ($result as $keyword) {
if ($keyword
->id()) {
$this->valueOptions[$keyword
->id()] = $keyword
->getName();
}
}
}
return parent::adminSummary();
}
}