View source
<?php
namespace Drupal\webform_views\Plugin\views\filter;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\views\Plugin\views\filter\NumericFilter;
use Drupal\webform_views\Plugin\views\WebformSubmissionCastToNumberTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformSubmissionNumericFilter extends NumericFilter {
use WebformSubmissionCastToNumberTrait;
protected $entityTypeManager;
protected $elementInfoManager;
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'), $container
->get('plugin.manager.element_info'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ElementInfoManagerInterface $element_info_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->elementInfoManager = $element_info_manager;
}
public function valueForm(&$form, FormStateInterface $form_state) {
parent::valueForm($form, $form_state);
$webform = $this->entityTypeManager
->getStorage('webform')
->load($this->definition['webform_id']);
$possible_locations = [];
$possible_locations[] = [
'value',
];
$possible_locations[] = [
'value',
'value',
];
$possible_locations[] = [
'value',
'min',
];
$possible_locations[] = [
'value',
'max',
];
foreach ($possible_locations as $possible_location) {
$key_exists = FALSE;
$element =& NestedArray::getValue($form, $possible_location, $key_exists);
if ($key_exists && isset($element['#type']) && $element['#type'] != 'value') {
$default_value = end($possible_location);
$default_value = isset($this->value[$default_value]) ? $this->value[$default_value] : NULL;
$element = [
'#title' => $element['#title'],
'#default_value' => $default_value,
'#required' => FALSE,
'#states' => $element['#states'],
] + $webform
->getElementInitialized($this->definition['webform_submission_field']);
}
}
}
public function opSimple($field) {
if ($this->definition['explicit_cast']) {
$this->query
->addWhereExpression($this->options['group'], $this
->castToDataType($field) . ' ' . $this->operator . ' ' . $this
->castToDataType(':value'), [
':value' => $this->value['value'],
]);
}
else {
parent::opSimple($field);
}
}
public function opBetween($field) {
if ($this->definition['explicit_cast']) {
$operator = $this->operator == 'between' ? 'BETWEEN' : 'NOT BETWEEN';
$this->query
->addWhereExpression($this->options['group'], $this
->castToDataType($field) . ' ' . $operator . ' ' . $this
->castToDataType(':min') . ' AND ' . $this
->castToDataType(':max'), [
':min' => $this->value['min'],
':max' => $this->value['max'],
]);
}
else {
parent::opBetween($field);
}
}
public function acceptExposedInput($input) {
if (parent::acceptExposedInput($input)) {
if (empty($this->options['exposed'])) {
return TRUE;
}
if (!empty($this->options['expose']['identifier'])) {
$value = $input[$this->options['expose']['identifier']];
return (bool) $value;
}
}
return FALSE;
}
function operators() {
$operators = parent::operators();
unset($operators['regular_expression']);
return $operators;
}
protected function getFormElementProperty($element, $property, $default) {
if (isset($element[$property])) {
return $element[$property];
}
return $this->elementInfoManager
->getInfoProperty($element['#type'], $property, $default);
}
}