GoogleAnalyticsBase.php in Google Analytics Reports 8.3
File
src/Plugin/views/filter/GoogleAnalyticsBase.php
View source
<?php
namespace Drupal\google_analytics_reports\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Number;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class GoogleAnalyticsBase extends FilterPluginBase {
use StringTranslationTrait;
protected $alwaysMultiple = TRUE;
public $isCustom = NULL;
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->isCustom = google_analytics_reports_is_custom($this->realField);
}
public function defineOptions() {
$options = parent::defineOptions();
if ($this->isCustom) {
$options['custom_field_number'] = [
'default' => 1,
];
}
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
if ($this->isCustom) {
$form['custom_field_number'] = [
'#type' => 'textfield',
'#title' => $this
->t('Custom field number'),
'#default_value' => isset($this->options['custom_field_number']) ? $this->options['custom_field_number'] : 1,
'#size' => 2,
'#maxlength' => 2,
'#required' => TRUE,
'#element_validate' => [
Number::class,
'validateNumber',
],
];
}
}
public function operatorOptions($which = 'title') {
$options = [];
foreach ($this
->operators() as $id => $info) {
$options[$id] = $info[$which];
}
return $options;
}
public function query() {
if ($this->isCustom) {
$this->realField = google_analytics_reports_custom_to_variable_field($this->realField, $this->options['custom_field_number']);
}
$info = $this
->operators();
if (!empty($info[$this->operator]['method'])) {
$this
->{$info[$this->operator]['method']}($this->realField);
}
}
public function operators() {
return [];
}
}