You are here

public function CommonMap::buildOptionsForm in Geolocation Field 8

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

Provide a form to edit options for this plugin.

Overrides StylePluginBase::buildOptionsForm

File

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

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) {
  parent::buildOptionsForm($form, $form_state);
  $labels = $this->displayHandler
    ->getFieldLabels();
  $fieldMap = \Drupal::service('entity_field.manager')
    ->getFieldMap();
  $geo_options = [];
  $title_options = [];
  $icon_options = [];
  $id_options = [];
  $fields = $this->displayHandler
    ->getOption('fields');
  foreach ($fields as $field_name => $field) {
    if ($field['plugin_id'] == 'geolocation_field') {
      $geo_options[$field_name] = $labels[$field_name];
    }
    if ($field['plugin_id'] == 'field' && !empty($field['entity_type']) && !empty($field['entity_field'])) {
      if (!empty($fieldMap[$field['entity_type']][$field['entity_field']]['type']) && $fieldMap[$field['entity_type']][$field['entity_field']]['type'] == 'geolocation') {
        $geo_options[$field_name] = $labels[$field_name];
      }
    }
    if (!empty($field['type']) && $field['type'] == 'image') {
      $icon_options[$field_name] = $labels[$field_name];
    }
    if (!empty($field['type']) && $field['type'] == 'string') {
      $title_options[$field_name] = $labels[$field_name];
    }
    if (!empty($field['type']) && $field['type'] == 'number_integer') {
      $id_options[$field_name] = $labels[$field_name];
    }
  }
  $form['geolocation_field'] = [
    '#title' => $this
      ->t('Geolocation source field'),
    '#type' => 'select',
    '#default_value' => $this->options['geolocation_field'],
    '#description' => $this
      ->t("The source of geodata for each entity."),
    '#options' => $geo_options,
    '#required' => TRUE,
  ];
  $form['title_field'] = [
    '#title' => $this
      ->t('Title source field'),
    '#type' => 'select',
    '#default_value' => $this->options['title_field'],
    '#description' => $this
      ->t("The source of the title for each entity. Field type must be 'string'."),
    '#options' => $title_options,
    '#empty_value' => 'none',
  ];
  $map_update_target_options = $this
    ->getMapUpdateOptions();

  /*
   * Dynamic map handling.
   */
  if (!empty($map_update_target_options['map_update_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['map_update_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 ($display_instance
          ->getPluginId() == 'page') {
          $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("Non-page displays will only update themselves. Most likely a page view should be updated instead."),
          '#options' => $update_targets,
          '#states' => [
            'visible' => [
              ':input[name="style_options[dynamic_map][enabled]"]' => [
                'checked' => TRUE,
              ],
            ],
          ],
        ];
      }
    }
  }

  /*
   * Centre handling.
   */
  $options = [
    'fit_bounds' => $this
      ->t('Automatically fit map bounds to results. Disregards any set center or zoom.'),
    'first_row' => $this
      ->t('Use first row as centre.'),
    'fixed_value' => $this
      ->t('Provide fixed latitude and longitude.'),
    'client_location' => $this
      ->t('Ask client for location via HTML5 geolocation API.'),
  ];
  $options += $map_update_target_options['map_update_options'];
  $form['centre'] = [
    '#type' => 'table',
    '#prefix' => $this
      ->t('<h3>Centre options</h3>Please note: Each option will, if it can be applied, supersede any following option.'),
    '#header' => [
      $this
        ->t('Enable'),
      $this
        ->t('Option'),
      $this
        ->t('settings'),
      [
        'data' => $this
          ->t('Settings'),
        'colspan' => '1',
      ],
    ],
    '#attributes' => [
      'id' => 'geolocation-centre-options',
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'geolocation-centre-option-weight',
      ],
    ],
  ];
  foreach ($options as $id => $label) {
    $weight = isset($this->options['centre'][$id]['weight']) ? $this->options['centre'][$id]['weight'] : 0;
    $form['centre'][$id]['#weight'] = $weight;
    $form['centre'][$id]['enable'] = [
      '#type' => 'checkbox',
      '#default_value' => isset($this->options['centre'][$id]['enable']) ? $this->options['centre'][$id]['enable'] : TRUE,
    ];
    $form['centre'][$id]['option'] = [
      '#markup' => $label,
    ];

    // Add tabledrag supprt.
    $form['centre'][$id]['#attributes']['class'][] = 'draggable';
    $form['centre'][$id]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for @option', [
        '@option' => $label,
      ]),
      '#title_display' => 'invisible',
      '#size' => 4,
      '#default_value' => $weight,
      '#attributes' => [
        'class' => [
          'geolocation-centre-option-weight',
        ],
      ],
    ];
  }
  $form['centre']['client_location']['settings'] = [
    '#type' => 'container',
    'update_map' => [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Additionally feed clients location back to view via dynamic map settings?'),
      '#default_value' => isset($this->options['centre']['client_location']['settings']['update_map']) ? $this->options['centre']['client_location']['settings']['update_map'] : FALSE,
    ],
    '#states' => [
      'visible' => [
        ':input[name="style_options[centre][client_location][enable]"]' => [
          'checked' => TRUE,
        ],
        ':input[name="style_options[dynamic_map][enabled]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['centre']['fixed_value']['settings'] = [
    '#type' => 'container',
    'latitude' => [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Latitude'),
      '#default_value' => isset($this->options['centre']['fixed_value']['settings']['latitude']) ? $this->options['centre']['fixed_value']['settings']['latitude'] : '',
      '#size' => 60,
      '#maxlength' => 128,
    ],
    'longitude' => [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Longitude'),
      '#default_value' => isset($this->options['centre']['fixed_value']['settings']['longitude']) ? $this->options['centre']['fixed_value']['settings']['longitude'] : '',
      '#size' => 60,
      '#maxlength' => 128,
    ],
    '#states' => [
      'visible' => [
        ':input[name="style_options[centre][fixed_value][enable]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  uasort($form['centre'], 'Drupal\\Component\\Utility\\SortArray::sortByWeightProperty');

  /*
   * Advanced settings
   */
  $form['advanced_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Advanced settings'),
  ];
  $form['show_raw_locations'] = [
    '#group' => 'style_options][advanced_settings',
    '#title' => $this
      ->t('Show raw locations'),
    '#description' => $this
      ->t('By default, the location data for the map will be hidden when the map loads.'),
    '#type' => 'checkbox',
    '#default_value' => $this->options['show_raw_locations'],
  ];
  $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'],
  ];
  if ($icon_options) {
    $form['icon_field'] = [
      '#group' => 'style_options][advanced_settings',
      '#title' => $this
        ->t('Icon source field'),
      '#type' => 'select',
      '#default_value' => $this->options['icon_field'],
      '#description' => $this
        ->t("Optional image (field) to use as icon."),
      '#options' => $icon_options,
      '#empty_value' => 'none',
      '#process' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'processGroup',
        ],
        [
          '\\Drupal\\Core\\Render\\Element\\Select',
          'processSelect',
        ],
      ],
      '#pre_render' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'preRenderGroup',
        ],
      ],
    ];
  }
  if ($id_options) {
    $form['marker_scroll_to_result'] = [
      '#group' => 'style_options][advanced_settings',
      '#title' => $this
        ->t('On clicking marker scroll to result instead of opening marker bubble'),
      '#type' => 'checkbox',
      '#default_value' => $this->options['marker_scroll_to_result'],
    ];
    $form['id_field'] = [
      '#group' => 'style_options][advanced_settings',
      '#title' => $this
        ->t('ID source field'),
      '#type' => 'select',
      '#default_value' => $this->options['id_field'],
      '#description' => $this
        ->t("Unique location ID used as scrolling target. If not targeting the raw location output, either add a class structured as 'geolocation-location-id-[ID]' or an attribute 'data-location-id=\"[ID]\"' to the target element."),
      '#options' => $id_options,
      '#empty_value' => 'none',
      '#states' => [
        'visible' => [
          ':input[name="style_options[marker_scroll_to_result]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#process' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'processGroup',
        ],
        [
          '\\Drupal\\Core\\Render\\Element\\Select',
          'processSelect',
        ],
      ],
      '#pre_render' => [
        [
          '\\Drupal\\Core\\Render\\Element\\RenderElement',
          'preRenderGroup',
        ],
      ],
    ];
  }
  $form['marker_row_number'] = [
    '#group' => 'style_options][advanced_settings',
    '#title' => $this
      ->t('Show views result row number in marker'),
    '#type' => 'checkbox',
    '#default_value' => $this->options['marker_row_number'],
  ];
  $form['context_popup_content'] = [
    '#group' => 'style_options][advanced_settings',
    '#type' => 'textarea',
    '#title' => $this
      ->t('Context popup content'),
    '#description' => $this
      ->t('A right click on the map will open a context popup with this content. Tokens supported. Additionally "@lat, @lng" will be replaced dynamically.'),
    '#default_value' => $this->options['context_popup_content'],
  ];

  /*
   * Marker Clusterer
   */
  $form['marker_clusterer_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Marker Clusterer'),
  ];
  $form['marker_clusterer'] = [
    '#group' => 'style_options][marker_clusterer_settings',
    '#title' => $this
      ->t('Cluster markers'),
    '#type' => 'checkbox',
    '#description' => $this
      ->t('Various <a href=":url">examples</a> are available.', [
      ':url' => 'https://developers.google.com/maps/documentation/javascript/marker-clustering',
    ]),
    '#default_value' => $this->options['marker_clusterer'],
  ];
  $form['marker_clusterer_image_path'] = [
    '#group' => 'style_options][marker_clusterer_settings',
    '#title' => $this
      ->t('Cluster image path'),
    '#type' => 'textfield',
    '#default_value' => $this->options['marker_clusterer_image_path'],
    '#description' => $this
      ->t("Set the marker image path. If omitted, the default image path %url will be used.", [
      '%url' => 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m',
    ]),
    '#states' => [
      'visible' => [
        ':input[name="style_options[marker_clusterer]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['marker_clusterer_styles'] = [
    '#group' => 'style_options][marker_clusterer_settings',
    '#title' => $this
      ->t('Styles of the Cluster'),
    '#type' => 'textarea',
    '#default_value' => $this->options['marker_clusterer_styles'],
    '#description' => $this
      ->t('Set custom Cluster styles in JSON Format. Custom Styles have to be set for all 5 Cluster Images. See the <a href=":reference">reference</a> for details.', [
      ':reference' => 'https://googlemaps.github.io/js-marker-clusterer/docs/reference.html',
    ]),
    '#states' => [
      'visible' => [
        ':input[name="style_options[marker_clusterer]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  /*
   * Google Maps settings
   */
  $form += $this
    ->getGoogleMapsSettingsForm($this->options, 'style_options][');
}