You are here

public function ManualOriginDefault::buildOptionsForm in Geofield 8

Builds the specific form elements for the geofield proximity plugin.

Parameters

array $form: The form element to build.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

array $options_parents: The values parents.

bool $is_exposed: The check/differentiate if it is part of an exposed form.

Overrides GeofieldProximitySourceBase::buildOptionsForm

1 method overrides ManualOriginDefault::buildOptionsForm()
ClientLocationOriginFilter::buildOptionsForm in src/Plugin/GeofieldProximitySource/ClientLocationOriginFilter.php
Builds the specific form elements for the geofield proximity plugin.

File

src/Plugin/GeofieldProximitySource/ManualOriginDefault.php, line 47

Class

ManualOriginDefault
Defines 'Geofield Manual Origin' plugin.

Namespace

Drupal\geofield\Plugin\GeofieldProximitySource

Code

public function buildOptionsForm(array &$form, FormStateInterface $form_state, array $options_parents, $is_exposed = FALSE) {
  $lat = isset($this->configuration['origin']['lat']) ? $this->configuration['origin']['lat'] : $this->origin['lat'];
  $lon = isset($this->configuration['origin']['lon']) ? $this->configuration['origin']['lon'] : $this->origin['lon'];
  $form["origin"] = [
    '#title' => $this
      ->t('Origin Coordinates'),
    '#type' => 'geofield_latlon',
    '#description' => $this
      ->t('Value in decimal degrees. Use dot (.) as decimal separator.'),
    '#default_value' => [
      'lat' => $lat,
      'lon' => $lon,
    ],
  ];

  // If it is a proximity filter context and IS NOT exposed, render origin
  // hidden and origin_summary options.
  if ($this->viewHandler->configuration['id'] == 'geofield_proximity_filter' && !$is_exposed) {
    $form['origin_hidden_flag'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Hide the Origin Input elements from the Exposed Form'),
      '#default_value' => isset($this->configuration['origin_hidden_flag']) ? $this->configuration['origin_hidden_flag'] : FALSE,
      '#states' => [
        'visible' => [
          ':input[name="options[expose_button][checkbox][checkbox]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['origin_summary_flag'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show (anyway) the Origin coordinates as summary in the Exposed Form'),
      '#default_value' => isset($this->configuration['origin_summary_flag']) ? $this->configuration['origin_summary_flag'] : TRUE,
      '#states' => [
        'visible' => [
          ':input[name="options[source_configuration][origin_hidden_flag]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }

  // If it IS exposed, eventually Hide the Origin components..
  if ($is_exposed && (isset($this->configuration['origin_hidden_flag']) && $this->configuration['origin_hidden_flag'])) {
    $form["origin"]['#attributes']['class'][] = 'visually-hidden';

    // Eventually Render the Origin Summary.
    if (isset($this->configuration['origin_summary_flag']) && $this->configuration['origin_summary_flag']) {
      $form['origin_summary'] = [
        "#type" => 'html_tag',
        "#tag" => 'div',
        '#value' => $this
          ->t('from Latitude: @lat and Longitude: @lon.', [
          '@lat' => new FormattableMarkup('<span class="geofield-lat"> @lat</span>', [
            '@lat' => $lat,
          ]),
          '@lon' => new FormattableMarkup('<span class="geofield-lon"> @lon</span>', [
            '@lon' => $lon,
          ]),
        ]),
        '#attributes' => [
          'class' => [
            'proximity-origin-summary',
          ],
        ],
      ];
    }
  }
}