You are here

public function GeofieldProximityFilter::valueForm in Geofield 8

Provide a simple textfield for equality

Overrides NumericFilter::valueForm

File

src/Plugin/views/filter/GeofieldProximityFilter.php, line 348

Class

GeofieldProximityFilter
Field handler to filter Geofields by proximity.

Namespace

Drupal\geofield\Plugin\views\filter

Code

public function valueForm(&$form, FormStateInterface $form_state) {
  parent::valueForm($form, $form_state);
  $form['value'] = [
    '#type' => 'container',
    '#tree' => TRUE,
  ];
  $units_description = '';
  $user_input = $form_state
    ->getUserInput();

  // 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';
  $source = !empty($form['operator']) ? ':input[name="options[operator]"]' : '';
  if ($exposed = $form_state
    ->get('exposed')) {
    $identifier = $this->options['expose']['identifier'];
    if (!isset($user_input[$identifier]) || !is_array($user_input[$identifier])) {
      $user_input[$identifier] = [];
    }
    $units_description = $this
      ->t('Units: @units', [
      '@units' => isset($user_input['options']['units']) ? $this->geofieldRadiusOptions[$user_input['options']['units']] : $this->geofieldRadiusOptions[$this->options['units']],
    ]);
    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 = ':input[name="' . $this->options['expose']['operator_id'] . '"]';
    }
  }
  if ($which == 'all' || $which == 'value') {
    $form['value']['value'] = [
      '#type' => 'number',
      '#min' => 0,
      '#step' => 0.1,
      '#title' => $exposed && !isset($form['field_geofield_proximity_op']) ? $this
        ->t('Distance') . ' ' . $this->operator : $this
        ->t('Distance'),
      '#size' => 30,
      '#default_value' => $this->value['value'],
      '#description' => $exposed && isset($units_description) ? $units_description : '',
    ];
    if (!empty($this->options['expose']['placeholder'])) {
      $form['value']['value']['#attributes']['placeholder'] = $this->options['expose']['placeholder'];
    }
    if ($exposed && isset($identifier) && !isset($user_input[$identifier]['value'])) {
      $user_input[$identifier]['value'] = $this->value['value'];
      $form_state
        ->setUserInput($user_input);
    }
  }
  if ($which == 'all') {

    // Setup #states for all operators with one value.
    foreach ($this
      ->operatorValues(1) as $operator) {
      $form['value']['value']['#states']['visible'][] = [
        $source => [
          'value' => $operator,
        ],
      ];
    }
  }
  if ($which == 'all' || $which == 'minmax') {
    $form['value']['min'] = [
      '#type' => 'number',
      '#min' => 0,
      '#step' => 0.1,
      '#title' => !$exposed ? $this
        ->t('Min') : $this
        ->t('From'),
      '#size' => 30,
      '#default_value' => $this->value['min'],
      '#description' => $exposed ? $units_description : '',
    ];
    if (!empty($this->options['expose']['min_placeholder'])) {
      $form['value']['min']['#attributes']['placeholder'] = $this->options['expose']['min_placeholder'];
    }
    $form['value']['max'] = [
      '#type' => 'number',
      '#min' => 0,
      '#step' => 0.1,
      '#title' => !$exposed ? $this
        ->t('And max') : $this
        ->t('And'),
      '#size' => 30,
      '#default_value' => $this->value['max'],
      '#description' => $exposed ? $units_description : '',
    ];
    if (!empty($this->options['expose']['max_placeholder'])) {
      $form['value']['max']['#attributes']['placeholder'] = $this->options['expose']['max_placeholder'];
    }
    if ($which == 'all') {
      $states = [];

      // Setup #states for all operators with two values.
      foreach ($this
        ->operatorValues(2) as $operator) {
        $states['#states']['visible'][] = [
          $source => [
            'value' => $operator,
          ],
        ];
      }
      $form['value']['min'] += $states;
      $form['value']['max'] += $states;
    }
    if ($exposed && isset($identifier) && isset($identifier) && !isset($user_input[$identifier]['min'])) {
      $user_input[$identifier]['min'] = $this->value['min'];
    }
    if ($exposed && isset($identifier) && !isset($user_input[$identifier]['max'])) {
      $user_input[$identifier]['max'] = $this->value['max'];
    }
    if (!isset($form['value'])) {

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

  // Build the specific Geofield Proximity Form Elements.
  if ($exposed && isset($identifier)) {
    $form['value']['#type'] = 'fieldset';
    $form['value']['source_configuration'] = [
      '#type' => 'container',
    ];
    try {
      $source_plugin_id = $this->options['source'];
      $source_plugin_configuration = isset($identifier) && isset($user_input[$identifier]['origin']) ? $user_input[$identifier] : $this->options['source_configuration'];

      /** @var \Drupal\geofield\Plugin\GeofieldProximitySourceInterface $source_plugin */
      $this->sourcePlugin = $this->proximitySourceManager
        ->createInstance($source_plugin_id, $source_plugin_configuration);
      $this->sourcePlugin
        ->setViewHandler($this);
      $proximity_origin = $this->sourcePlugin
        ->getOrigin();
      $this->sourcePlugin
        ->buildOptionsForm($form['value']['source_configuration'], $form_state, [
        'source_configuration',
      ], $exposed);

      // Write the Proximity Filter exposed summary.
      if ($this->options['source_configuration']['exposed_summary']) {
        $form['value']['exposed_summary'] = $this
          ->exposedSummary();
      }
      if (!isset($user_input[$identifier]['origin']) && !empty($proximity_origin)) {
        $user_input[$identifier]['origin'] = [
          'lat' => $proximity_origin['lat'],
          'lon' => $proximity_origin['lon'],
        ];
        $form_state
          ->setUserInput($user_input);
      }
    } catch (\Exception $e) {
      watchdog_exception('geofield', $e);
      $form_state
        ->setErrorByName($form['value']['source_configuration'], $this
        ->t("The Proximity Source couldn't be set due to: @error", [
        '@error' => $e,
      ]));
    }
  }
}