You are here

public function CommonMap::buildOptionsForm in Geolocation Field 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/views/style/CommonMap.php \Drupal\geolocation\Plugin\views\style\CommonMap::buildOptionsForm()

Provide a form to edit options for this plugin.

Overrides GeolocationStyleBase::buildOptionsForm

File

src/Plugin/views/style/CommonMap.php, line 248

Class

CommonMap
Allow to display several field items on a common map.

Namespace

Drupal\geolocation\Plugin\views\style

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  $map_provider_options = $this->mapProviderManager
    ->getMapProviderOptions();
  if (empty($map_provider_options)) {
    $form = [
      '#type' => 'html_tag',
      '#tag' => 'span',
      '#value' => $this
        ->t("No map provider found."),
    ];
    return;
  }
  parent::buildOptionsForm($form, $form_state);
  $map_update_target_options = $this
    ->getMapUpdateOptions();

  /*
   * Dynamic map handling.
   */
  if (!empty($map_update_target_options)) {
    $form['dynamic_map'] = [
      '#title' => $this
        ->t('Dynamic Map'),
      '#type' => 'fieldset',
    ];
    $form['dynamic_map']['enabled'] = [
      '#title' => $this
        ->t('Update view on map boundary changes. Also known as "AirBnB" style.'),
      '#type' => 'checkbox',
      '#default_value' => $this->options['dynamic_map']['enabled'],
      '#description' => $this
        ->t("If enabled, moving the map will filter results based on current map boundary. This functionality requires an exposed boundary filter. Enabling AJAX is highly recommend for best user experience. If additional views are to be updated with the map change as well, it is highly recommended to use the view containing the map as 'parent' and the additional views as attachments."),
    ];
    $form['dynamic_map']['update_handler'] = [
      '#title' => $this
        ->t('Dynamic map update handler'),
      '#type' => 'select',
      '#default_value' => $this->options['dynamic_map']['update_handler'],
      '#description' => $this
        ->t("The map has to know how to feed back the update boundary data to the view."),
      '#options' => $map_update_target_options,
      '#states' => [
        'visible' => [
          ':input[name="style_options[dynamic_map][enabled]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['dynamic_map']['hide_form'] = [
      '#title' => $this
        ->t('Hide exposed filter form element if applicable.'),
      '#type' => 'checkbox',
      '#default_value' => $this->options['dynamic_map']['hide_form'],
      '#states' => [
        'visible' => [
          ':input[name="style_options[dynamic_map][enabled]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['dynamic_map']['views_refresh_delay'] = [
      '#title' => $this
        ->t('Minimum idle time in milliseconds required to trigger views refresh'),
      '#description' => $this
        ->t('Once the view refresh is triggered, any further change of the map bounds will have no effect until the map update is finished. User interactions like scrolling in and out or dragging the map might trigger the map idle event, before the user is finished interacting. This setting adds a delay before the view is refreshed to allow further map interactions.'),
      '#type' => 'number',
      '#min' => 0,
      '#default_value' => $this->options['dynamic_map']['views_refresh_delay'],
      '#states' => [
        'visible' => [
          ':input[name="style_options[dynamic_map][enabled]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    if ($this->displayHandler
      ->getPluginId() !== 'page') {
      $update_targets = [
        $this->displayHandler->display['id'] => $this
          ->t('- This display -'),
      ];
      foreach ($this->view->displayHandlers
        ->getInstanceIds() as $instance_id) {
        $display_instance = $this->view->displayHandlers
          ->get($instance_id);
        if (in_array($display_instance
          ->getPluginId(), [
          'page',
          'block',
        ])) {
          $update_targets[$instance_id] = $display_instance->display['display_title'];
        }
      }
      if (!empty($update_targets)) {
        $form['dynamic_map']['update_target'] = [
          '#title' => $this
            ->t('Dynamic map update target'),
          '#type' => 'select',
          '#default_value' => $this->options['dynamic_map']['update_target'],
          '#description' => $this
            ->t("Targets other than page or block can only update themselves."),
          '#options' => $update_targets,
          '#states' => [
            'visible' => [
              ':input[name="style_options[dynamic_map][enabled]"]' => [
                'checked' => TRUE,
              ],
            ],
          ],
        ];
      }
    }
  }

  /*
   * Centre handling.
   */
  $form['centre'] = $this->mapCenterManager
    ->getCenterOptionsForm((array) $this->options['centre'], $this);

  /*
   * Advanced settings
   */
  $form['advanced_settings'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Advanced settings'),
  ];
  $form['even_empty'] = [
    '#group' => 'style_options][advanced_settings',
    '#title' => $this
      ->t('Display map when no locations are found'),
    '#type' => 'checkbox',
    '#default_value' => $this->options['even_empty'],
  ];
  $form['map_provider_id'] = [
    '#type' => 'select',
    '#options' => $map_provider_options,
    '#title' => $this
      ->t('Map Provider'),
    '#default_value' => $this->options['map_provider_id'],
    '#ajax' => [
      'callback' => [
        get_class($this->mapProviderManager),
        'addSettingsFormAjax',
      ],
      'wrapper' => 'map-provider-settings',
      'effect' => 'fade',
    ],
  ];
  $form['map_provider_settings'] = [
    '#type' => 'html_tag',
    '#tag' => 'span',
    '#value' => $this
      ->t("No settings available."),
  ];
  $map_provider_id = NestedArray::getValue($form_state
    ->getUserInput(), [
    'style_options',
    'map_provider_id',
  ]);
  if (empty($map_provider_id)) {
    $map_provider_id = $this->options['map_provider_id'];
  }
  if (empty($map_provider_id)) {
    $map_provider_id = key($map_provider_options);
  }
  $map_provider_settings = $this->options['map_provider_settings'] ?? [];
  if (!empty($this->options['map_provider_id']) && $map_provider_id != $this->options['map_provider_id']) {
    $map_provider_settings = [];
    if (!empty($form_state
      ->getValue([
      'style_options',
      'map_provider_settings',
    ]))) {
      $form_state
        ->setValue([
        'style_options',
        'map_provider_settings',
      ], []);
      $form_state
        ->setUserInput($form_state
        ->getValues());
    }
  }
  if (!empty($map_provider_id)) {
    $form['map_provider_settings'] = $this->mapProviderManager
      ->createInstance($map_provider_id, $map_provider_settings)
      ->getSettingsForm($map_provider_settings, [
      'style_options',
      'map_provider_settings',
    ]);
  }
  $form['map_provider_settings'] = array_replace($form['map_provider_settings'], [
    '#prefix' => '<div id="map-provider-settings">',
    '#suffix' => '</div>',
  ]);
}