You are here

public function WebformSubmissionFieldFilter::operatorForm in Webform Views Integration 8.5

Options form subform for setting the operator.

This may be overridden by child classes, and it must define $form['operator'];

Overrides FilterPluginBase::operatorForm

See also

buildOptionsForm()

File

src/Plugin/views/filter/WebformSubmissionFieldFilter.php, line 65

Class

WebformSubmissionFieldFilter
Filter based on value of a webform submission.

Namespace

Drupal\webform_views\Plugin\views\filter

Code

public function operatorForm(&$form, FormStateInterface $form_state) {
  parent::operatorForm($form, $form_state);
  if (isset($form['operator'])) {
    $do_ajax = TRUE;

    // We must guess the future #parents for this operator here.
    if (!$this
      ->isExposed()) {
      $parents = [
        'options',
        'operator',
      ];
    }
    elseif (!$this
      ->isAGroup()) {

      // We cannot determine which row we are building, so we prefer to hold
      // off ajax.
      $do_ajax = FALSE;
      $parents = [];
    }
    elseif ($this->options['expose']['use_operator']) {
      $do_ajax = FALSE;
      $parents = [
        $this->options['expose']['operator_id'],
      ];
    }
    else {

      // We shouldn't really fall into here. If we do, it means there is yet
      // another unknown use case of this method.
      $do_ajax = FALSE;
      $parents = [];
    }
    $operator = NestedArray::getValue($form_state
      ->getUserInput(), $parents);
    if (!$operator || is_array($operator) || !isset($this
      ->operators()[$operator])) {
      $operator = $this->operator;
    }
    if ($do_ajax) {
      $process = $this
        ->getFormElementProperty($form['operator'], '#process', []);

      // We need to run before ajax process stuff since we dynamically insert
      // the wrapper value for ajax.
      array_unshift($process, [
        self::class,
        'processOperatorForm',
      ]);
      $form['operator']['#ajax'] = [
        'callback' => [
          self::class,
          'ajaxValueForm',
        ],
        'wrapper' => 'this-will-be-set-up-in-process',
      ];

      // When just adding the filter the ajax URL is wrong. So we hard code it
      // here in that case.
      if ($this->routeMatch
        ->getRouteName() == 'views_ui.form_add_handler') {
        $ajax_url = Url::fromRoute('views_ui.form_handler', [
          'js' => $this->routeMatch
            ->getRawParameter('js'),
          'view' => $this->routeMatch
            ->getRawParameter('view'),
          'display_id' => $this->routeMatch
            ->getRawParameter('display_id'),
          'type' => $this->routeMatch
            ->getRawParameter('type'),
          'id' => $this->options['id'],
        ]);
        $form['operator']['#ajax']['url'] = $ajax_url;
      }
      $form['operator']['#process'] = $process;
    }
    else {

      // We are in the scenario where Ajax will break. So we fall to some
      // inoffensive (with relaxed validation) operator.
      // See https://www.drupal.org/node/2804457 and
      // https://www.drupal.org/node/2842525 for the bugs that force us to do
      // it this way.
      $operator = 'contains';
    }
    $form_state
      ->set([
      'webform_views',
      'filter_operator',
    ], $operator);
  }
}