You are here

public function ColorizedGmapBlock::buildFormStylers in Colorized google maps block 8

1 call to ColorizedGmapBlock::buildFormStylers()
ColorizedGmapBlock::blockForm in src/Plugin/Block/ColorizedGmapBlock.php

File

src/Plugin/Block/ColorizedGmapBlock.php, line 266

Class

ColorizedGmapBlock
Provides a 'Example: configurable text string' block.

Namespace

Drupal\colorized_gmap\Plugin\Block

Code

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

  // List of available map features.
  $feature_types = [
    'water' => 'Water',
    'landscape' => 'Landscape',
    'landscape.man_made' => 'Landscape (man made)',
    'landscape.natural' => 'Landscape (natural)',
    'landscape.natural.landcover' => 'Landscape (natural landcover)',
    'landscape.natural.terrain' => 'Landscape (natural terrain)',
    'road' => 'Road',
    'road.highway' => 'Road (highway)',
    'road.highway.controlled_access' => 'Road highway (controlled access)',
    'road.arterial' => 'Road (Arterial)',
    'road.local' => 'Road (local)',
    'poi' => 'Poi',
    'poi.park' => 'Poi (park)',
    'poi.business' => 'Poi (business)',
    'poi.attraction' => 'Poi (attraction)',
    'poi.medical' => 'Poi (medical)',
    'poi.school' => 'Poi (school)',
    'poi.sports_complex' => 'Poi (sports complex)',
    'poi.government' => 'Poi (government)',
    'poi.place_of_worship' => 'Poi (place of worship)',
    'administrative' => 'Administrative',
    'administrative.country' => 'Administrative (country)',
    'administrative.land_parcel' => 'Administrative (land parcel)',
    'administrative.locality' => 'Administrative (locality)',
    'administrative.neighborhood' => 'Administrative (neighborhood)',
    'administrative.province' => 'Administrative (province)',
    'all' => 'All',
    'transit' => 'Transit',
    'transit.line' => 'Transit (line)',
    'transit.station' => 'Transit station',
    'transit.station.airport' => 'Transit station (airport)',
    'transit.station.bus' => 'Transit station (bus)',
    'transit.station.rail' => 'Transit station (rail)',
  ];

  // List of available map elements.
  $elements = [
    'all' => 'All',
    'geometry' => 'Geometry',
    'geometry.fill' => 'Geometry fill',
    'geometry.stroke' => 'Geometry stroke',
    'labels' => 'Labels',
    'labels.icon' => 'Labels icon',
    'labels.text' => 'Labels text',
    'labels.text.fill' => 'Labels text fill',
    'labels.text.stroke' => 'Labels text stroke',
  ];

  // @todo: get styles from block config.
  $styles = $this->configuration['colorized_map_styles'];
  $styles_count = $form_state
    ->get('styles_count');
  if (empty($styles) && !$styles_count) {
    $form_state
      ->set('styles_count', 1);
    $styles_count = $form_state
      ->get('styles_count');
  }
  if (!$styles_count) {
    $form_state
      ->set('styles_count', count($styles));
    $styles_count = $form_state
      ->get('styles_count');
  }
  $form['#tree'] = TRUE;
  $form['colorized_map_styles'] = [
    '#type' => 'table',
    '#weight' => 2,
    '#title' => $this
      ->t('Map styles'),
    '#prefix' => '<div id="gmap-ajax-wrapper">',
    '#suffix' => '</div>',
    '#header' => [
      $this
        ->t('Feature type'),
      $this
        ->t('Element type'),
      $this
        ->t('Stylers'),
    ],
  ];

  // Example map div.
  $form['markup'] = [
    '#markup' => '<div id="colorized-gmap-content"></div>',
  ];

  // @todo: move to method.
  $style_element_ajax = [
    'callback' => 'Drupal\\colorized_gmap\\Plugin\\Block\\ColorizedGmapBlock::stylesMapUpdateCallback',
    'wrapper' => 'colorized-gmap-content',
    'event' => 'change',
    'progress' => [
      'type' => 'none',
    ],
  ];
  for ($i = 0; $i < $styles_count; $i++) {
    $featureType = !empty($this->configuration['colorized_map_styles'][$i]['featureType']) ? $this->configuration['colorized_map_styles'][$i]['featureType'] : '';
    $form['colorized_map_styles'][$i]['featureType'] = [
      '#type' => 'select',
      '#options' => $feature_types,
      '#default_value' => $featureType,
      '#ajax' => $style_element_ajax,
    ];
    $elementType = !empty($this->configuration['colorized_map_styles'][$i]['elementType']) ? $this->configuration['colorized_map_styles'][$i]['elementType'] : '';
    $form['colorized_map_styles'][$i]['elementType'] = [
      '#type' => 'select',
      '#options' => $elements,
      '#default_value' => $elementType,
      '#ajax' => $style_element_ajax,
    ];
    $color = !empty($this->configuration['colorized_map_styles'][$i]['stylers'][0]['color']) ? $this->configuration['colorized_map_styles'][$i]['stylers'][0]['color'] : '';
    $form['colorized_map_styles'][$i]['stylers'][0] = [
      '#tree' => TRUE,
      'color' => [
        '#title' => $this
          ->t('Color'),
        '#type' => 'textfield',
        '#size' => 12,
        '#default_value' => $color,
        '#attributes' => [
          'class' => [
            'edit_color_input',
          ],
        ],
        '#ajax' => [
          'callback' => 'Drupal\\colorized_gmap\\Plugin\\Block\\ColorizedGmapBlock::stylesMapUpdateCallback',
          'wrapper' => 'colorized-gmap-content',
          'event' => 'textfield_change',
          'progress' => [
            'type' => 'none',
          ],
        ],
      ],
    ];
    $visibility = !empty($this->configuration['colorized_map_styles'][$i]['stylers'][1]['visibility']) ? $this->configuration['colorized_map_styles'][$i]['stylers'][1]['visibility'] : '';
    $form['colorized_map_styles'][$i]['stylers'][1] = [
      'visibility' => [
        '#type' => 'select',
        '#title' => $this
          ->t('Visibility'),
        '#default_value' => $visibility,
        '#options' => [
          'on' => 'On',
          'off' => 'Off',
          'simplified' => 'simplified',
        ],
        '#ajax' => $style_element_ajax,
      ],
    ];
    $saturation = !empty($this->configuration['colorized_map_styles'][$i]['stylers'][2]['saturation']) ? $this->configuration['colorized_map_styles'][$i]['stylers'][2]['saturation'] : '';
    $form['colorized_map_styles'][$i]['stylers'][2] = [
      'saturation' => [
        '#type' => 'textfield',
        '#size' => 6,
        '#title' => $this
          ->t('Saturation'),
        '#default_value' => $saturation,
        '#ajax' => $style_element_ajax,
      ],
    ];
    $lightness = !empty($this->configuration['colorized_map_styles'][$i]['stylers'][3]['lightness']) ? $this->configuration['colorized_map_styles'][$i]['stylers'][3]['lightness'] : '';
    $form['colorized_map_styles'][$i]['stylers'][3] = [
      'lightness' => [
        '#type' => 'textfield',
        '#size' => 4,
        '#title' => $this
          ->t('Lightness'),
        '#default_value' => $lightness,
        '#ajax' => $style_element_ajax,
      ],
    ];
    $weight = !empty($this->configuration['colorized_map_styles'][$i]['stylers'][4]['weight']) ? $this->configuration['colorized_map_styles'][$i]['stylers'][4]['weight'] : '';
    $form['colorized_map_styles'][$i]['stylers'][4] = [
      'weight' => [
        '#type' => 'textfield',
        '#size' => 4,
        '#title' => $this
          ->t('Weight'),
        '#default_value' => $weight,
        '#ajax' => $style_element_ajax,
      ],
    ];
  }

  // Buttons.
  $form_state;
  $form['ajax_buttons'] = [
    '#type' => 'fieldset',
    '#weight' => 3,
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  ];
  $form['ajax_buttons']['add_more'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add More'),
    '#submit' => [
      'Drupal\\colorized_gmap\\Plugin\\Block\\ColorizedGmapBlock::stylesAddOneMore',
    ],
    '#ajax' => [
      'callback' => 'Drupal\\colorized_gmap\\Plugin\\Block\\ColorizedGmapBlock::stylesUpdateCallback',
      'wrapper' => 'gmap-ajax-wrapper',
    ],
  ];
  $form['ajax_buttons']['remove_row'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Remove Row'),
    '#submit' => [
      'Drupal\\colorized_gmap\\Plugin\\Block\\ColorizedGmapBlock::stylesRemoveOne',
    ],
    '#ajax' => [
      'callback' => 'Drupal\\colorized_gmap\\Plugin\\Block\\ColorizedGmapBlock::stylesUpdateCallback',
      'wrapper' => 'gmap-ajax-wrapper',
    ],
  ];
  return $form;
}