You are here

public function ClientLocationOriginFilter::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 ManualOriginDefault::buildOptionsForm

File

src/Plugin/GeofieldProximitySource/ClientLocationOriginFilter.php, line 28

Class

ClientLocationOriginFilter
Defines 'Geofield Client Location Origin' plugin.

Namespace

Drupal\geofield\Plugin\GeofieldProximitySource

Code

public function buildOptionsForm(array &$form, FormStateInterface $form_state, array $options_parents, $is_exposed = FALSE) {
  $form['#attributes'] = [
    'class' => [
      'proximity-origin-client',
    ],
  ];
  $form["origin"] = [
    '#title' => $this
      ->t('Client Coordinates'),
    '#type' => 'geofield_latlon',
    '#description' => $this
      ->t('Value in decimal degrees. Use dot (.) as decimal separator.'),
    '#default_value' => [
      'lat' => '',
      'lon' => '',
    ],
    '#attributes' => [
      'class' => [
        'visually-hidden',
      ],
    ],
  ];

  // If it is a proximity filter context and IS NOT exposed, render origin
  // summary option.
  if ($this->viewHandler->configuration['id'] == 'geofield_proximity_filter' && !$is_exposed) {
    $form['origin_summary_flag'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show the Client Origin coordinates as summary in the Exposed Form'),
      '#default_value' => isset($this->configuration['origin_summary_flag']) ? $this->configuration['origin_summary_flag'] : TRUE,
    ];
  }

  // If it IS exposed load the geolocation library.
  if ($is_exposed) {
    $form['origin']['#attached']['library'][] = 'geofield/geolocation';

    // And 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-summary">@lat</span>', [
            '@lat' => $this
              ->t('undefined'),
          ]),
          '@lon' => new FormattableMarkup('<span class="geofield-lon-summary">@lon</span>', [
            '@lon' => $this
              ->t('undefined'),
          ]),
        ]),
      ];
    }
  }
}