You are here

public function GeofieldGoogleMapViewStyle::buildOptionsForm in Geofield Map 8.2

Same name and namespace in other branches
  1. 8 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 503

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) {

  // 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);
  $default_settings = self::defineOptions();
  $form['#attached'] = [
    'library' => [
      'geofield_map/geofield_map_general',
      'geofield_map/geofield_map_view_display_settings',
    ],
  ];
  $fields_geo_data = $this
    ->getAvailableDataSources();

  // 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',
        ],
      ],
    ];
    return;
  }
  $wrapper_id = 'geofield-map-views-style-options-form-wrapper';
  $form['#prefix'] = '<div id="' . $wrapper_id . '">';
  $form['#suffix'] = '</div>';

  // 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,
  ];

  // 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 Geofield Mapping base Entity.<br><u>Leave as "View Base Entity" to rely on default Views behaviour, and don\'t specifically needed otherwise</u>.<br><b>Note:</b> This would affect Map Theming logics and options.'),
      '#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',
        ],
      ],
    ];
  }
  $desc_options = array_merge([
    '0' => $this
      ->t('- Any - No Infowindow'),
  ], $this->viewFields);

  // 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,
      ]),
      '#rendered_entity_ajax' => $this
        ->t('- Rendered @entity entity via Ajax (Quicker Map start / Slower Infowindow show) -', [
        '@entity' => $this->entityType,
      ]),
      '#rendered_view_fields' => $this
        ->t('# Rendered View Fields (with field label, format, classes, etc)'),
    ];
  }
  $this->options['infowindow_content_options'] = $desc_options;
  $form += $this
    ->generateGmapSettingsForm($form, $form_state, $this->options, $default_settings);
  $form['map_marker_and_infowindow']['infowindow_field']['#description'] .= $this
    ->t("<br>'Rendered @entity entity' option: quick infowindow, but slower map start in case of many map features (> 50 / 100) and large infowindows content.<br>'Rendered @entity entity via ajax' option: quicker map start in case of many map features and large infowindows content.", [
    '@entity' => $this->entityType,
  ]);

  // Implement Map Theming based on available GeofieldMapThemers.
  $form['map_marker_and_infowindow']['theming'] = [
    '#type' => 'fieldset',
    '#title' => 'Map Theming Options',
    '#weight' => isset($form['map_marker_and_infowindow']['icon_image_path']['#weight']) ? $form['map_marker_and_infowindow']['icon_image_path']['#weight'] - 5 : -15,
    '#attributes' => [
      'id' => 'map-theming-container',
    ],
  ];
  $map_themers_definitions = $this->mapThemerManager
    ->getDefinitions();
  uasort($map_themers_definitions, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
  $map_themers_options = array_merge([
    'none' => 'None',
  ], $this->mapThemerManager
    ->getMapThemersList('ViewStyle'));
  $user_input = $form_state
    ->getUserInput();
  $map_themer_id = isset($user_input['style_options']['map_marker_and_infowindow']['theming']['plugin_id']) ? $user_input['style_options']['map_marker_and_infowindow']['theming']['plugin_id'] : NULL;
  $default_map_themer = isset($this->options['map_marker_and_infowindow']['theming']['plugin_id']) ? $this->options['map_marker_and_infowindow']['theming']['plugin_id'] : 'none';
  $selected_map_themer = !empty($map_themer_id) ? $map_themer_id : $default_map_themer;
  $plugin_id_warning = [
    'deprecated' => [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $this
        ->t('Deprecated.'),
      '#attributes' => [
        'class' => [
          'geofield-map-warning',
        ],
      ],
    ],
    'message' => [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $this
        ->t('Note: This Map Themer is not compatible with D8 configuration management & sync (deprecated). Use the correspondent Image Select version instead (!).'),
      '#attributes' => [
        'class' => [
          'geofield-map-warning',
        ],
      ],
    ],
  ];
  $form['map_marker_and_infowindow']['theming']['plugin_id'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Map Theming'),
    '#default_value' => $selected_map_themer,
    '#options' => $map_themers_options,
    '#ajax' => [
      'callback' => [
        get_class($this),
        'mapThemingOptionsUpdate',
      ],
      'effect' => 'fade',
    ],
  ];
  if ($selected_map_themer != 'none') {
    $form['map_marker_and_infowindow']['theming']['plugin_id_info'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $map_themers_definitions[$selected_map_themer]['description'],
    ];
    if (!$map_themers_definitions[$selected_map_themer]['markerIconSelection']['configSyncCompatibility']) {
      $form['map_marker_and_infowindow']['theming']['plugin_id_warning'] = $plugin_id_warning['message'];
    }
    try {
      $map_themer_plugin = $this->mapThemerManager
        ->createInstance($selected_map_themer);
      $form['map_marker_and_infowindow']['theming'][$map_themer_plugin->pluginId] = [
        '#type' => 'container',
        'id' => [
          '#type' => 'value',
          '#value' => $map_themer_plugin
            ->getPluginId(),
        ],
        'values' => $map_themer_plugin
          ->buildMapThemerElement($this->options, $form, $form_state, $this),
        'description' => [
          '#type' => 'value',
          '#value' => $map_themer_plugin
            ->getDescription(),
        ],
      ];
    } catch (PluginException $e) {
      $form['map_marker_and_infowindow']['theming']['plugin_id']['#default_value'] = $map_themers_options['none'];
    }
  }
  $form['map_marker_and_infowindow']['theming']['plugins_descriptions'] = [
    '#type' => 'container',
    'table' => [
      '#type' => 'table',
      '#caption' => $this
        ->t('Available Map Themers & Descriptions:'),
      '#attributes' => [
        'class' => 'map-theming-options',
      ],
    ],
  ];
  foreach ($map_themers_definitions as $k => $map_themer) {
    $form['map_marker_and_infowindow']['theming']['plugins_descriptions']['table'][$k] = [
      'td1' => [
        'label' => [
          '#type' => 'html_tag',
          '#tag' => 'div',
          '#value' => $map_themers_options[$k],
        ],
        'warning' => !$map_themers_definitions[$k]['markerIconSelection']['configSyncCompatibility'] ? $plugin_id_warning['deprecated'] : [],
      ],
      'td2' => [
        '#type' => 'container',
        'description' => [
          '#type' => 'html_tag',
          '#tag' => 'div',
          '#value' => $map_themer['description'],
        ],
        'warning' => !$map_themers_definitions[$k]['markerIconSelection']['configSyncCompatibility'] ? $plugin_id_warning['message'] : [],
      ],
      '#attributes' => !$map_themers_definitions[$k]['markerIconSelection']['configSyncCompatibility'] ? [
        'class' => [
          'deprecated',
        ],
      ] : [],
    ];
  }

  // Hide fall-backs in case the user choose a map themer.
  if ('none' != $selected_map_themer) {

    // Hide the Map Themers Plugins Descriptions.
    $form['map_marker_and_infowindow']['theming']['plugins_descriptions']['#attributes']['class'] = [
      'hidden',
    ];

    // Hide the icon_image_path element, with prefix/suffix (as hidden would
    // hide just the textfield and not label/title and description wrappers).
    $form['map_marker_and_infowindow']['icon_image_path']['#prefix'] = '<div id="icon-image-path" class="visually-hidden">';
    $form['map_marker_and_infowindow']['icon_image_path']['#suffix'] = '</div>';
  }
}