You are here

public function GoogleAnalyticsNumeric::valueForm in Google Analytics Reports 8.3

Provide a simple textfield for equality.

Overrides FilterPluginBase::valueForm

File

src/Plugin/views/filter/GoogleAnalyticsNumeric.php, line 85

Class

GoogleAnalyticsNumeric
Simple filter to handle numerics for Google Analytics.

Namespace

Drupal\google_analytics_reports\Plugin\views\filter

Code

public function valueForm(&$form, FormStateInterface $form_state) {
  parent::valueForm($form, $form_state);
  $values = $form_state
    ->getValues();
  $form['value']['#tree'] = TRUE;

  // We have to make some choices when creating this as an exposed
  // filter form. For example, if the operator is locked and thus
  // not rendered, we can't render dependencies; instead we only
  // render the form items we need.
  $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'])) {

      // Exposed and locked.
      $which = in_array($this->operator, $this
        ->operatorValues(2)) ? 'minmax' : 'value';
    }
    else {
      $source = 'edit-' . Html::getUniqueId($this->options['expose']['operator_id']);
    }
  }
  if ($which == 'all') {
    $form['value']['value'] = [
      '#type' => 'textfield',
      '#title' => empty($values['exposed']) ? $this
        ->t('Value') : '',
      '#size' => 30,
      '#default_value' => $this->value['value'],
      '#dependency' => [
        $source => $this
          ->operatorValues(1),
      ],
    ];
    if (!empty($values['exposed']) && !isset($values['input'][$identifier]['value'])) {
      $values['input'][$identifier]['value'] = $this->value['value'];
    }
  }
  elseif ($which == 'value') {

    // When exposed we drop the value-value and just do value if
    // the operator is locked.
    $form['value'] = [
      '#type' => 'textfield',
      '#title' => empty($values['exposed']) ? $this
        ->t('Value') : '',
      '#size' => 30,
      '#default_value' => $this->value['value'],
    ];
    if (!empty($values['exposed']) && !isset($values['input'][$identifier])) {
      $values['input'][$identifier] = $this->value['value'];
    }
  }
  if ($which == 'all' || $which == 'minmax') {
    $form['value']['min'] = [
      '#type' => 'textfield',
      '#title' => empty($values['exposed']) ? $this
        ->t('Min') : '',
      '#size' => 30,
      '#default_value' => $this->value['min'],
    ];
    $form['value']['max'] = [
      '#type' => 'textfield',
      '#title' => empty($values['exposed']) ? $this
        ->t('And max') : $this
        ->t('And'),
      '#size' => 30,
      '#default_value' => $this->value['max'],
    ];
    if ($which == 'all') {
      $dependency = [
        '#dependency' => [
          $source => $this
            ->operatorValues(2),
        ],
      ];
      $form['value']['min'] += $dependency;
      $form['value']['max'] += $dependency;
    }
    if (!empty($values['exposed']) && !isset($values['input'][$identifier]['min'])) {
      $values['input'][$identifier]['min'] = $this->value['min'];
    }
    if (!empty($values['exposed']) && !isset($values['input'][$identifier]['max'])) {
      $values['input'][$identifier]['max'] = $this->value['max'];
    }
    if (!isset($form['value'])) {

      // Ensure there is something in the 'value'.
      $form['value'] = [
        '#type' => 'value',
        '#value' => NULL,
      ];
    }
  }
}