contacts_events-8.x-1.x-dev/src/Plugin/views/filter/Event.php
src/Plugin/views/filter/Event.php
<?php
namespace Drupal\contacts_events\Plugin\views\filter;
use Drupal\Core\Entity\Element\EntityAutocomplete;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\InOperator;
/**
* Filter handler for events.
*
* @ingroup views_filter_handlers
*
* @ViewsFilter("contacts_events_order_event")
*/
class Event extends InOperator {
/**
* {@inheritdoc}
*/
protected function valueForm(&$form, FormStateInterface $form_state) {
$events = $this->value ? Event::loadMultiple($this->value) : [];
$default_value = EntityAutocomplete::getEntityLabels($events);
$form['value'] = [
'#type' => 'entity_autocomplete',
'#title' => $this->t('Events'),
'#description' => $this->t('Start typing the name of the event or its ID.'),
'#target_type' => 'contacts_event',
'#tags' => FALSE,
'#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);
}
}
/**
* {@inheritdoc}
*/
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'];
}
}
/**
* {@inheritdoc}
*/
public function getValueOptions() {
return $this->valueOptions;
}
}
