You are here

public function GeofieldMapLegend::blockForm in Geofield Map 8.2

Overrides BlockPluginTrait::blockForm

File

src/Plugin/Block/GeofieldMapLegend.php, line 208

Class

GeofieldMapLegend
Provides a custom Geofield Map Legend block.

Namespace

Drupal\geofield_map\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {

  // Attach Geofield Map Libraries.
  $form['#attached']['library'][] = 'geofield_map/geofield_map_general';
  $form['#attached']['library'][] = 'geofield_map/geofield_map_legend';
  $user_input = $form_state
    ->getUserInput();
  $user_selected_geofield_map_legend = !empty($user_input['settings']['geofield_map_legend']) ? $user_input['settings']['geofield_map_legend'] : NULL;
  $geofield_map_legends = $this
    ->getGeofieldMapLegends();
  $selected_geofield_map_legend = isset($user_selected_geofield_map_legend) ? $user_selected_geofield_map_legend : (isset($this->configuration['geofield_map_legend']) && array_key_exists($this->configuration['geofield_map_legend'], $geofield_map_legends) ? $this->configuration['geofield_map_legend'] : 'none:none');
  list($view_id, $view_display_id) = explode(':', $selected_geofield_map_legend);
  $map_themer_plugin_and_values = $this
    ->getMapThemerPluginAndValues($view_id, $view_display_id);
  $map_themer_plugin = NULL;
  if (!empty($map_themer_plugin_and_values)) {

    /* @var \Drupal\geofield_map\MapThemerInterface $map_themer_plugin */
    $map_themer_plugin = $map_themer_plugin_and_values[0];
    $map_themer_plugin_definition = $map_themer_plugin
      ->getPluginDefinition();
  }
  $geofield_map_legends_options = array_merge([
    'none:none' => '_ none _',
  ], $geofield_map_legends);
  $form['#prefix'] = '<div id="geofield-map-legend-settings-block-wrapper">';
  $form['#suffix'] = '</div>';
  if (!empty($geofield_map_legends)) {
    $form['geofield_map_legend'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Geofield Map Legend'),
      '#description' => $this
        ->t('Select the Geofield Map legend to render in this block<br>Choose the View and the Display you want to grab the Legend definition from.'),
      '#options' => $geofield_map_legends_options,
      '#default_value' => $selected_geofield_map_legend ?: 'none',
      '#required' => TRUE,
      '#ajax' => [
        'callback' => [
          static::class,
          'mapLegendSelectionUpdate',
        ],
        'effect' => 'fade',
      ],
    ];
    $form['values_label'] = [
      '#title' => $this
        ->t('Values Column Label'),
      '#type' => 'textfield',
      '#description' => $this
        ->t('Set the Label text to be shown for the Values column. Empty for any Label.'),
      '#default_value' => isset($this->configuration['values_label']) ? $this->configuration['values_label'] : $this
        ->t('Value'),
      '#size' => 26,
    ];
    $form['markers_label'] = [
      '#title' => $this
        ->t('Markers Column Label'),
      '#type' => 'textfield',
      '#description' => $this
        ->t('Set the Label text to be shown for the Markers/Icon column. Empty for any Label.'),
      '#default_value' => isset($this->configuration['markers_label']) ? $this->configuration['markers_label'] : $this
        ->t('Marker/Icon'),
      '#size' => 26,
    ];

    // Define the list of possible legend icon image style.
    $markers_image_style_options = array_merge([
      '_map_theming_image_style_' => '<- Reflect the Map Theming Icon Image Styles ->',
    ], $this->markerIcon
      ->getImageStyleOptions());

    // Force add the geofield_map_default_icon_style, if not existing.
    if (!in_array('geofield_map_default_icon_style', array_keys($markers_image_style_options))) {
      $markers_image_style_options['geofield_map_default_icon_style'] = 'geofield_map_default_icon_style';
    }
    if ($map_themer_plugin instanceof MapThemerInterface && $map_themer_plugin_definition['markerIconSelection']['type'] == 'managed_file') {
      $form['markers_image_style'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Markers Image style'),
        '#options' => $markers_image_style_options,
        '#default_value' => isset($this->configuration['markers_image_style']) ? $this->configuration['markers_image_style'] : 'geofield_map_default_icon_style',
        '#description' => $this
          ->t('Choose the image style the markers icons will be rendered in the Legend with.'),
      ];
    }
    if ($map_themer_plugin instanceof MapThemerInterface && $map_themer_plugin_definition['markerIconSelection']['type'] == 'file_uri') {
      $form['markers_width'] = [
        '#type' => 'number',
        '#title' => $this
          ->t('Markers Width (pixels)'),
        '#default_value' => isset($this->configuration['markers_width']) ? $this->configuration['markers_width'] : 50,
        '#description' => $this
          ->t('Choose the max image width for the marker in the legend.<br>(Empty value for natural image weight.)'),
        '#min' => 10,
        '#max' => 300,
        '#size' => 4,
        '#step' => 5,
      ];
    }
    $form['legend_caption'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Legend Caption'),
      '#description' => $this
        ->t('Write here the Table Legend Caption).'),
      '#default_value' => isset($this->configuration['legend_caption']) ? $this->configuration['legend_caption'] : '',
      '#rows' => 1,
    ];
    $form['legend_notes'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Legend Notes'),
      '#description' => $this
        ->t("Write here Notes to the Legend (Footer as default, might be altered in the Map Themer plugin)."),
      '#default_value' => isset($this->configuration['legend_notes']) ? $this->configuration['legend_notes'] : '',
      '#rows' => 3,
    ];
  }
  else {
    $form['geofield_map_legend_warning'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $this
        ->t('No eligible Geofield Map View Style and Theming have been defined/found.<br>Please @define_view_link with Geofield Map View Style and (not null) Theming to be able to choose at least a Legend to render.', [
        '@define_view_link' => $this->link
          ->generate($this
          ->t('define one View'), Url::fromRoute('entity.view.collection')),
      ]),
      '#attributes' => [
        'class' => [
          'geofield-map-warning',
        ],
      ],
    ];
  }
  return $form;
}