View source
<?php
namespace Drupal\google_analytics_reports\Plugin\views\filter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class GoogleAnalyticsString extends GoogleAnalyticsBase {
use StringTranslationTrait;
public function defineOptions() {
$options = parent::defineOptions();
$options['expose']['contains']['required'] = [
'default' => FALSE,
];
return $options;
}
public function operators() {
$operators = [
'=' => [
'title' => $this
->t('Is equal to'),
'short' => $this
->t('='),
'method' => 'opEqual',
'values' => 1,
],
'!=' => [
'title' => $this
->t('Is not equal to'),
'short' => $this
->t('!='),
'method' => 'opInequal',
'values' => 1,
],
'contains' => [
'title' => $this
->t('Contains'),
'short' => $this
->t('contains'),
'method' => 'opContains',
'values' => 1,
],
'not' => [
'title' => $this
->t('Does not contain'),
'short' => $this
->t('!has'),
'method' => 'opNot',
'values' => 1,
],
'regular_expression' => [
'title' => $this
->t('Contains a match for the regular expression'),
'short' => $this
->t('regex'),
'method' => 'opRegex',
'values' => 1,
],
'not_regular_expression' => [
'title' => $this
->t('Does not match regular expression'),
'short' => $this
->t('!regex'),
'method' => 'opNotRegex',
'values' => 1,
],
];
return $operators;
}
public function valueForm(&$form, FormStateInterface $form_state) {
parent::valueForm($form, $form_state);
$values = $form_state
->getValues();
$which = 'all';
if (!empty($form['operator'])) {
$source = $form['operator']['#type'] == 'radios' ? 'radio:options[operator]' : 'edit-options-operator';
}
if (!empty($values['exposed'])) {
$identifier = $this->options['expose']['identifier'];
if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
$which = in_array($this->operator, $this
->operatorValues(1)) ? 'value' : 'none';
}
else {
$source = 'edit-' . Html::getUniqueId($this->options['expose']['operator_id']);
}
}
if ($which == 'all' || $which == 'value') {
$form['value'] = [
'#type' => 'textfield',
'#title' => $this
->t('Value'),
'#size' => 30,
'#default_value' => $this->value,
];
if (!empty($values['exposed']) && !isset($values['input'][$identifier])) {
$values['input'][$identifier] = $this->value;
}
if ($which == 'all') {
$form['value'] += [
'#dependency' => [
$source => $this
->operatorValues(1),
],
];
}
}
if (!isset($form['value'])) {
$form['value'] = [
'#type' => 'value',
'#value' => NULL,
];
}
}
public function opEqual($field) {
$this->query
->addWhere($this->options['group'], $field, $this->value, '==');
}
public function opInequal($field) {
$this->query
->addWhere($this->options['group'], $field, $this->value, '!=');
}
public function opContains($field) {
$this->query
->addWhere($this->options['group'], $field, $this->value, '=@');
}
public function opNot($field) {
$this->query
->addWhere($this->options['group'], $field, $this->value, '!@');
}
public function opRegex($field) {
$this->query
->addWhere($this->options['group'], $field, $this->value, '=~');
}
public function opNotRegex($field) {
$this->query
->addWhere($this->options['group'], $field, $this->value, '!~');
}
public function adminSummary() {
if (!empty($this->options['exposed'])) {
return $this
->t('exposed');
}
$options = $this
->operatorOptions('short');
$output = '';
if (!empty($options[$this->operator])) {
$output = Html::escape($options[$this->operator]);
}
if (in_array($this->operator, $this
->operatorValues(1))) {
$output .= ' ' . Html::escape($this->value);
}
return $output;
}
public function operatorValues($values = 1) {
$options = [];
foreach ($this
->operators() as $id => $info) {
if (isset($info['values']) && $info['values'] == $values) {
$options[] = $id;
}
}
return $options;
}
}