You are here

public function GeofieldGoogleMapViewStyle::buildOptionsForm in Geofield Map 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/views/style/GeofieldGoogleMapViewStyle.php \Drupal\geofield_map\Plugin\views\style\GeofieldGoogleMapViewStyle::buildOptionsForm()

Provide a form to edit options for this plugin.

Overrides StylePluginBase::buildOptionsForm

File

src/Plugin/views/style/GeofieldGoogleMapViewStyle.php, line 253

Class

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

Namespace

Drupal\geofield_map\Plugin\views\style

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $default_settings = self::defineOptions();

  // Get a list of fields and a sublist of geo data fields in this view.
  $fields = [];
  $fields_geo_data = [];

  /* @var \Drupal\views\Plugin\views\ViewsHandlerInterface $handler) */
  foreach ($this->displayHandler
    ->getHandlers('field') as $field_id => $handler) {
    $label = $handler
      ->adminLabel() ?: $field_id;
    $fields[$field_id] = $label;
    if (is_a($handler, '\\Drupal\\views\\Plugin\\views\\field\\EntityField')) {

      /* @var \Drupal\views\Plugin\views\field\EntityField $handler */
      $field_storage_definitions = $this->entityFieldManager
        ->getFieldStorageDefinitions($handler
        ->getEntityType());
      $field_storage_definition = $field_storage_definitions[$handler->definition['field_name']];
      if ($field_storage_definition
        ->getType() == 'geofield') {
        $fields_geo_data[$field_id] = $label;
      }
    }
  }

  // Check whether we have a geo data field we can work with.
  if (empty($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' => [
          'geofield-map-warning',
        ],
      ],
      '#attached' => [
        'library' => [
          'geofield_map/geofield_map_general',
        ],
      ],
    ];
    return;
  }

  // Map data source.
  $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,
  ];
  $desc_options = array_merge([
    '0' => $this
      ->t('- Any - No Infowindow'),
  ], $fields);

  // Add an option to render the entire entity using a view mode.
  if ($this->entityType) {
    $desc_options += [
      '#rendered_entity' => $this
        ->t('- Rendered @entity entity -', [
        '@entity' => $this->entityType,
      ]),
    ];
  }
  $this->options['infowindow_content_options'] = $desc_options;
  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'] = [
      '#fieldset' => 'map_marker_and_infowindow',
      '#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' => !empty($this->options['view_mode']) ? $this->options['view_mode'] : 'full',
      '#states' => [
        'visible' => [
          ':input[name="style_options[map_marker_and_infowindow][infowindow_field]"]' => [
            'value' => '#rendered_entity',
          ],
        ],
      ],
    ];
  }
  $form = $form + $this
    ->generateGmapSettingsForm($form, $form_state, $this->options, $default_settings);
}