You are here

public function StyledGoogleMapDefaultFormatter::settingsForm in Styled Google Map 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/StyledGoogleMapDefaultFormatter.php \Drupal\styled_google_map\Plugin\Field\FieldFormatter\StyledGoogleMapDefaultFormatter::settingsForm()

Returns a form to configure settings for the formatter.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the formatter. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

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

Return value

array The form elements for the formatter settings.

Overrides FormatterBase::settingsForm

File

src/Plugin/Field/FieldFormatter/StyledGoogleMapDefaultFormatter.php, line 87

Class

StyledGoogleMapDefaultFormatter
Plugin implementation of the 'styled_google_map_default' formatter.

Namespace

Drupal\styled_google_map\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $elements = parent::settingsForm($form, $form_state);
  $default_settings = StyledGoogleMapDefaultFormatter::defaultSettings();

  // Set all available setting fields for the Styled Google Map.
  $elements['width'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Width size'),
    '#default_value' => $this
      ->getSetting('width'),
    '#description' => $this
      ->t('Map width written in pixels or percentage'),
    '#required' => TRUE,
  );
  $elements['height'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Height size'),
    '#default_value' => $this
      ->getSetting('height'),
    '#description' => $this
      ->t('Map height written in pixels or percentage'),
    '#required' => TRUE,
  );
  $elements['gestureHandling'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Gesture handling'),
    '#description' => $this
      ->t('This setting controls how the API handles gestures on the map. See more <a href="@href">here</a>', [
      '@href' => 'https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions.gestureHandling',
    ]),
    '#options' => [
      'cooperative' => $this
        ->t('Scroll events with a ctrl key or ⌘ key pressed zoom the map.'),
      'greedy' => $this
        ->t('All touch gestures and scroll events pan or zoom the map.'),
      'none' => $this
        ->t('The map cannot be panned or zoomed by user gestures.'),
      'auto' => $this
        ->t('(default) Gesture handling is either cooperative or greedy'),
    ],
    '#default_value' => $this
      ->getSetting('gestureHandling'),
  ];
  $elements['style'] = array(
    '#type' => 'details',
    '#title' => $this
      ->t('Map style'),
  );
  $style_settings = $this
    ->getSetting('style');
  $elements['style']['maptype'] = array(
    '#type' => 'select',
    '#options' => array(
      'ROADMAP' => $this
        ->t('ROADMAP'),
      'SATELLITE' => $this
        ->t('SATELLITE'),
      'HYBRID' => $this
        ->t('HYBRID'),
      'TERRAIN' => $this
        ->t('TERRAIN'),
    ),
    '#title' => $this
      ->t('Map type'),
    '#default_value' => empty($style_settings['maptype']) ? $default_settings['style']['maptype'] : $style_settings['maptype'],
    '#required' => TRUE,
  );
  $elements['style']['style'] = array(
    '#type' => 'textarea',
    '#title' => $this
      ->t('JSON Style'),
    '#default_value' => empty($style_settings['style']) ? $default_settings['style']['style'] : $style_settings['style'],
    '#description' => $this
      ->t('Check out !url for custom styles. Also check out this !project to style and edit Google Map JSON styles.', array(
      '!url' => \Drupal::service('link_generator')
        ->generate($this
        ->t('Snazzy maps'), Url::fromUri('http://snazzymaps.com/')),
      '!project' => \Drupal::service('link_generator')
        ->generate($this
        ->t('Github page'), Url::fromUri('http://instrument.github.io/styled-maps-wizard/')),
    )),
  );
  $elements['style']['pin'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('URL to the marker'),
    '#default_value' => empty($style_settings['pin']) ? $default_settings['style']['pin'] : $style_settings['pin'],
    '#description' => $this
      ->t('URL to the marker image. You can use a !wrapper for the url. Ex. !example (not working until !fixed)', array(
      '!wrapper' => \Drupal::service('link_generator')
        ->generate($this
        ->t('Stream wrapper'), Url::fromUri('https://drupal.org/project/system_stream_wrapper')),
      '!example' => STYLED_GOOGLE_MAP_DEFAULT_PIN,
      '!fixed' => \Drupal::service('link_generator')
        ->generate('https://www.drupal.org/node/1308152', Url::fromUri('https://www.drupal.org/node/1308152')),
    )),
  );
  $map_center_settings = $this
    ->getSetting('map_center');
  $elements['map_center'] = array(
    '#type' => 'details',
    '#title' => $this
      ->t('Centering map'),
  );

  // Retrieve all field names from the current entity bundle.
  // Retrieve all field names from the current entity bundle.
  $field_options = array();
  $center_options = array(
    '' => $this
      ->t('Center automatically'),
  );
  $fields = $form['#fields'];
  foreach ($fields as $field) {
    $config = FieldConfig::loadByName($form['#entity_type'], $form['#bundle'], $field);
    if (!$config) {
      continue;
    }
    $type = $config
      ->get('field_type');
    $name = $config
      ->get('field_name');
    $field_options[$field] = $config
      ->getLabel();
    if ($type == 'geofield' && $this->fieldDefinition
      ->get('field_name') != $name) {
      $center_options[$field] = $config
        ->getLabel();
    }
  }
  $elements['map_center']['center_coordinates'] = array(
    '#type' => 'select',
    '#options' => $center_options,
    '#default_value' => empty($map_center_settings['center_coordinates']) ? $default_settings['map_center']['center_coordinates'] : $map_center_settings['center_coordinates'],
    '#description' => $this
      ->t('To have map centered on other point than location you need to have another GeoField in your content type structure'),
  );
  $elements['popup'] = array(
    '#type' => 'details',
    '#title' => $this
      ->t('Marker popup'),
  );
  $popup_settings = $this
    ->getSetting('popup');
  $elements['popup']['choice'] = array(
    '#type' => 'select',
    '#options' => array(
      0 => $this
        ->t('None'),
      1 => $this
        ->t('Field'),
      2 => $this
        ->t('View mode'),
    ),
    '#default_value' => empty($popup_settings['choice']) ? $default_settings['popup']['choice'] : $popup_settings['choice'],
    '#id' => 'edit-popup-choice-field',
  );

  // Retrieve view mode settings from the current entity bundle.
  $view_modes = \Drupal::service('entity_display.repository')
    ->getViewModeOptions($form['#entity_type']);
  $elements['popup']['view_mode'] = array(
    '#type' => 'select',
    '#options' => $view_modes,
    '#default_value' => empty($popup_settings['view_mode']) ? $default_settings['popup']['view_mode'] : $popup_settings['view_mode'],
    '#states' => array(
      'visible' => array(
        ':input[id="edit-popup-choice-field"]' => array(
          'value' => 2,
        ),
      ),
    ),
  );
  $elements['popup']['text'] = array(
    '#type' => 'select',
    '#options' => $field_options,
    '#default_value' => empty($popup_settings['text']) ? $default_settings['popup']['text'] : $popup_settings['text'],
    '#states' => array(
      'visible' => array(
        ':input[id="edit-popup-choice-field"]' => array(
          'value' => 1,
        ),
      ),
    ),
  );
  $elements['popup']['label'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Show field label'),
    '#default_value' => empty($popup_settings['label']) ? $default_settings['popup']['label'] : $popup_settings['label'],
    '#states' => array(
      'visible' => array(
        ':input[id="edit-popup-choice-field"]' => array(
          'value' => 1,
        ),
      ),
    ),
  );
  $elements['popup']['shadow_style'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Shadow style'),
    '#options' => array(
      0,
      1,
      2,
    ),
    '#description' => $this
      ->t('1: shadow behind, 2: shadow below, 0: no shadow'),
    '#default_value' => empty($popup_settings['shadow_style']) ? $default_settings['popup']['shadow_style'] : $popup_settings['shadow_style'],
  );
  $elements['popup']['padding'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Padding'),
    '#field_suffix' => 'px',
    '#default_value' => empty($popup_settings['padding']) ? $default_settings['popup']['padding'] : $popup_settings['padding'],
  );
  $elements['popup']['border_radius'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Border radius'),
    '#field_suffix' => 'px',
    '#default_value' => empty($popup_settings['border_radius']) ? $default_settings['popup']['border_radius'] : $popup_settings['border_radius'],
  );
  $elements['popup']['border_width'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Border width'),
    '#field_suffix' => 'px',
    '#default_value' => empty($popup_settings['border_width']) ? $default_settings['popup']['border_width'] : $popup_settings['border_width'],
  );
  $elements['popup']['border_color'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Border color'),
    '#field_suffix' => '#hex',
    '#default_value' => empty($popup_settings['border_color']) ? $default_settings['popup']['border_color'] : $popup_settings['border_color'],
  );
  $elements['popup']['background_color'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Background color'),
    '#field_suffix' => '#hex',
    '#default_value' => empty($popup_settings['background_color']) ? $default_settings['popup']['background_color'] : $popup_settings['background_color'],
  );
  $elements['popup']['min_width'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Min width'),
    '#field_suffix' => 'px (or auto)',
    '#default_value' => empty($popup_settings['min_width']) ? $default_settings['popup']['min_width'] : $popup_settings['min_width'],
  );
  $elements['popup']['max_width'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Max width'),
    '#field_suffix' => 'px (or auto)',
    '#default_value' => empty($popup_settings['max_width']) ? $default_settings['popup']['max_width'] : $popup_settings['max_width'],
  );
  $elements['popup']['min_height'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Min height'),
    '#field_suffix' => 'px (or auto)',
    '#default_value' => empty($popup_settings['min_height']) ? $default_settings['popup']['min_height'] : $popup_settings['min_height'],
  );
  $elements['popup']['max_height'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Max height'),
    '#field_suffix' => 'px (or auto)',
    '#default_value' => empty($popup_settings['max_height']) ? $default_settings['popup']['max_height'] : $popup_settings['max_height'],
  );
  $elements['popup']['arrow_style'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Arrow style'),
    '#options' => array(
      0,
      1,
      2,
    ),
    '#description' => $this
      ->t('1: left side visible, 2: right side visible, 0: both sides visible'),
    '#default_value' => empty($popup_settings['arrow_style']) ? $default_settings['popup']['arrow_style'] : $popup_settings['arrow_style'],
  );
  $elements['popup']['arrow_size'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Arrow size'),
    '#field_suffix' => 'px',
    '#default_value' => empty($popup_settings['arrow_size']) ? $default_settings['popup']['arrow_size'] : $popup_settings['arrow_size'],
  );
  $elements['popup']['arrow_position'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Arrow position'),
    '#field_suffix' => 'px',
    '#default_value' => empty($popup_settings['arrow_position']) ? $default_settings['popup']['arrow_position'] : $popup_settings['arrow_position'],
  );
  $elements['popup']['disable_auto_pan'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Auto pan'),
    '#options' => array(
      1 => $this
        ->t('Yes'),
      0 => $this
        ->t('No'),
    ),
    '#description' => $this
      ->t('Automatically center the pin on click'),
    '#default_value' => empty($popup_settings['disable_auto_pan']) ? $default_settings['popup']['disable_auto_pan'] : $popup_settings['disable_auto_pan'],
  );
  $elements['popup']['hide_close_button'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Hide close button'),
    '#options' => array(
      1 => $this
        ->t('Yes'),
      0 => $this
        ->t('No'),
    ),
    '#description' => $this
      ->t('Hide the popup close button'),
    '#default_value' => empty($popup_settings['hide_close_button']) ? $default_settings['popup']['hide_close_button'] : $popup_settings['hide_close_button'],
  );
  $elements['popup']['disable_animation'] = array(
    '#type' => 'select',
    '#title' => $this
      ->t('Disable animation'),
    '#options' => array(
      1 => $this
        ->t('Yes'),
      0 => $this
        ->t('No'),
    ),
    '#description' => $this
      ->t('Disables the popup animation'),
    '#default_value' => empty($popup_settings['disable_animation']) ? $default_settings['popup']['disable_animation'] : $popup_settings['disable_animation'],
  );
  $popup_classes_settings = !empty($popup_settings['classes']) ? $popup_settings['classes'] : $default_settings['popup']['classes'];
  $elements['popup']['classes']['content_container'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Wrapper class'),
    '#default_value' => empty($popup_classes_settings['content_container']) ? $default_settings['popup']['classes']['content_container'] : $popup_classes_settings['content_container'],
  );
  $elements['popup']['classes']['background'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Background class'),
    '#default_value' => empty($popup_classes_settings['background']) ? $default_settings['popup']['classes']['background'] : $popup_classes_settings['background'],
  );
  $elements['popup']['classes']['arrow'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Arrow class'),
    '#default_value' => empty($popup_classes_settings['arrow']) ? $default_settings['popup']['classes']['arrow'] : $popup_classes_settings['arrow'],
  );
  $elements['popup']['classes']['arrow_outer'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Arrow outer class'),
    '#default_value' => empty($popup_classes_settings['arrow_outer']) ? $default_settings['popup']['classes']['arrow_outer'] : $popup_classes_settings['arrow_outer'],
  );
  $elements['popup']['classes']['arrow_inner'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Arrow inner class'),
    '#default_value' => empty($popup_classes_settings['arrow_inner']) ? $default_settings['popup']['classes']['arrow_inner'] : $popup_classes_settings['arrow_inner'],
  );
  $elements['zoom'] = array(
    '#type' => 'details',
    '#title' => $this
      ->t('Zoom'),
  );
  $zoom_settings = $this
    ->getSetting('zoom');
  $elements['zoom']['default'] = array(
    '#type' => 'select',
    '#options' => range(1, 23),
    '#title' => $this
      ->t('Default zoom level'),
    '#default_value' => empty($zoom_settings['default']) ? $default_settings['zoom']['default'] : $zoom_settings['default'],
    '#description' => $this
      ->t('Should be between the Min and Max zoom level.'),
    '#required' => TRUE,
  );
  $elements['zoom']['max'] = array(
    '#type' => 'select',
    '#options' => range(1, 23),
    '#title' => $this
      ->t('Max zoom level'),
    '#default_value' => empty($zoom_settings['max']) ? $default_settings['zoom']['max'] : $zoom_settings['max'],
    '#description' => $this
      ->t('Should be greater then the Min zoom level.'),
    '#required' => TRUE,
  );
  $elements['zoom']['min'] = array(
    '#type' => 'select',
    '#options' => range(1, 23),
    '#title' => $this
      ->t('Min zoom level'),
    '#default_value' => empty($zoom_settings['min']) ? $default_settings['zoom']['min'] : $zoom_settings['min'],
    '#description' => $this
      ->t('Should be smaller then the Max zoom level.'),
    '#required' => TRUE,
  );
  $elements['maptypecontrol'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable Map Type control'),
    '#default_value' => $this
      ->getSetting('maptypecontrol'),
  );
  $elements['scalecontrol'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable scale control'),
    '#default_value' => $this
      ->getSetting('scalecontrol'),
  );
  $elements['rotatecontrol'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable rotate control'),
    '#default_value' => $this
      ->getSetting('rotatecontrol'),
  );
  $elements['draggable'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable dragging'),
    '#default_value' => $this
      ->getSetting('draggable'),
  );
  $elements['mobile_draggable'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable mobile dragging'),
    '#description' => $this
      ->t('Sometimes when the map covers big part of touch device screen draggable feature can cause inability to scroll the page'),
    '#default_value' => $this
      ->getSetting('mobile_draggable'),
  );
  $elements['streetviewcontrol'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable street view control'),
    '#default_value' => $this
      ->getSetting('streetviewcontrol'),
  );
  $elements['zoomcontrol'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable zoom control'),
    '#default_value' => $this
      ->getSetting('zoomcontrol'),
  );
  return $elements;
}