You are here

public function LeafletMap::buildOptionsForm in Leaflet 2.0.x

Same name and namespace in other branches
  1. 8 modules/leaflet_views/src/Plugin/views/style/LeafletMap.php \Drupal\leaflet_views\Plugin\views\style\LeafletMap::buildOptionsForm()
  2. 2.1.x modules/leaflet_views/src/Plugin/views/style/LeafletMap.php \Drupal\leaflet_views\Plugin\views\style\LeafletMap::buildOptionsForm()

Provide a form to edit options for this plugin.

Overrides StylePluginBase::buildOptionsForm

File

modules/leaflet_views/src/Plugin/views/style/LeafletMap.php, line 436

Class

LeafletMap
Style plugin to render a View output as a Leaflet map.

Namespace

Drupal\leaflet_views\Plugin\views\style

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {

  // If data source changed then apply the changes.
  if ($form_state
    ->get('entity_source')) {
    $this->options['entity_source'] = $form_state
      ->get('entity_source');
    $this->entityInfo = $this
      ->getEntitySourceEntityInfo($this->options['entity_source']);
    $this->entityType = $this->entityInfo
      ->id();
    $this->entitySource = $this->options['entity_source'];
  }
  parent::buildOptionsForm($form, $form_state);
  $form['#tree'] = TRUE;
  $form['#attached'] = [
    'library' => [
      'leaflet/general',
    ],
  ];

  // Get a sublist of geo data fields in the view.
  $fields_geo_data = $this
    ->getAvailableDataSources();

  // Check whether we have a geo data field we can work with.
  if (!count($fields_geo_data)) {
    $form['error'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $this
        ->t('Please add at least one Geofield to the View and come back here to set it as Data Source.'),
      '#attributes' => [
        'class' => [
          'leaflet-warning',
        ],
      ],
    ];
    return;
  }
  $wrapper_id = 'leaflet-map-views-style-options-form-wrapper';
  $form['#prefix'] = '<div id="' . $wrapper_id . '">';
  $form['#suffix'] = '</div>';

  // Map preset.
  $form['data_source'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Data Source'),
    '#description' => $this
      ->t('Which field contains geodata?'),
    '#options' => $fields_geo_data,
    '#default_value' => $this->options['data_source'],
    '#required' => TRUE,
  ];

  // Get the possible entity sources.
  $entity_sources = $this
    ->getAvailableEntitySources();

  // If there is only one entity source it will be the base entity, so don't
  // show the element to avoid confusing people.
  if (count($entity_sources) == 1) {
    $form['entity_source'] = [
      '#type' => 'value',
      '#value' => key($entity_sources),
    ];
  }
  else {
    $form['entity_source'] = [
      '#type' => 'select',
      '#title' => new TranslatableMarkup('Entity Source'),
      '#description' => new TranslatableMarkup('Select which Entity should be used as Leaflet Mapping base Entity.<br><u>Leave as "View Base Entity" to rely on default Views behaviour, and don\'t specifically needed otherwise</u>.'),
      '#options' => $entity_sources,
      '#default_value' => !empty($this->options['entity_source']) ? $this->options['entity_source'] : '__base_table',
      '#ajax' => [
        'wrapper' => $wrapper_id,
        'callback' => [
          static::class,
          'optionsFormEntitySourceSubmitAjax',
        ],
        'trigger_as' => [
          'name' => 'entity_source_submit',
        ],
      ],
    ];
    $form['entity_source_submit'] = [
      '#type' => 'submit',
      '#value' => new TranslatableMarkup('Update Entity Source'),
      '#name' => 'entity_source_submit',
      '#submit' => [
        [
          static::class,
          'optionsFormEntitySourceSubmit',
        ],
      ],
      '#validate' => [],
      '#limit_validation_errors' => [
        [
          'style_options',
          'entity_source',
        ],
      ],
      '#attributes' => [
        'class' => [
          'js-hide',
        ],
      ],
      '#ajax' => [
        'wrapper' => $wrapper_id,
        'callback' => [
          static::class,
          'optionsFormEntitySourceSubmitAjax',
        ],
      ],
    ];
  }

  // Name field.
  $form['name_field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Title Field'),
    '#description' => $this
      ->t('Choose the field which will appear as a title on tooltips.'),
    '#options' => array_merge([
      '' => ' - None - ',
    ], $this->viewFields),
    '#default_value' => $this->options['name_field'],
  ];
  $desc_options = array_merge([
    '' => ' - None - ',
  ], $this->viewFields);

  // Add an option to render the entire entity using a view mode.
  if ($this->entityType) {
    $desc_options += [
      '#rendered_entity' => $this
        ->t('< @entity entity >', [
        '@entity' => $this->entityType,
      ]),
      '#rendered_entity_ajax' => $this
        ->t('< @entity entity via ajax >', [
        '@entity' => $this->entityType,
      ]),
      '#rendered_view_fields' => $this
        ->t('# Rendered View Fields (with field label, format, classes, etc)'),
    ];
  }
  $form['description_field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Description Field'),
    '#description' => $this
      ->t('Choose the field or rendering method which will appear as a description on tooltips or popups.'),
    '#required' => FALSE,
    '#options' => $desc_options,
    '#default_value' => $this->options['description_field'],
  ];
  if ($this->entityType) {

    // Get the human readable labels for the entity view modes.
    $view_mode_options = [];
    foreach ($this->entityDisplay
      ->getViewModes($this->entityType) as $key => $view_mode) {
      $view_mode_options[$key] = $view_mode['label'];
    }

    // The View Mode drop-down is visible conditional on "#rendered_entity"
    // being selected in the Description drop-down above.
    $form['view_mode'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('View mode'),
      '#description' => $this
        ->t('View mode the entity will be displayed in the Infowindow.'),
      '#options' => $view_mode_options,
      '#default_value' => $this->options['view_mode'],
      '#states' => [
        'visible' => [
          ':input[name="style_options[description_field]"]' => [
            [
              'value' => '#rendered_entity',
            ],
            [
              'value' => '#rendered_entity_ajax',
            ],
          ],
        ],
      ],
    ];
  }

  // Generate the Leaflet Map General Settings.
  $this
    ->generateMapGeneralSettings($form, $this->options);

  // Generate the Leaflet Map Reset Control.
  $this
    ->setResetMapControl($form, $this->options);

  // Generate the Leaflet Map Position Form Element.
  $map_position_options = $this->options['map_position'];
  $form['map_position'] = $this
    ->generateMapPositionElement($map_position_options);

  // Generate the Leaflet Map weight/zIndex Form Element.
  $form['weight'] = $this
    ->generateWeightElement($this->options['weight']);

  // Generate Icon form element.
  $icon_options = $this->options['icon'];
  $form['icon'] = $this
    ->generateIconFormElement($icon_options);

  // Set Map Marker Cluster Element.
  $this
    ->setMapMarkerclusterElement($form, $this->options);

  // Set Map Geometries Options Element.
  $this
    ->setMapPathOptionsElement($form, $this->options);

  // Set Map Geocoder Control Element, if the Geocoder Module exists,
  // otherwise output a tip on Geocoder Module Integration.
  $this
    ->setGeocoderMapControl($form, $this->options);
}