You are here

class ColorizedGmapBlock in Colorized google maps block 8

Provides a 'Example: configurable text string' block.

Drupal\Core\Block\BlockBase gives us a very useful set of basic functionality for this configurable block. We can just fill in a few of the blanks with defaultConfiguration(), blockForm(), blockSubmit(), and build().

Plugin annotation


@Block(
  id = "colorized_gmap",
  admin_label = @Translation("Colorized Google Map"),
)

Hierarchy

Expanded class hierarchy of ColorizedGmapBlock

File

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

Namespace

Drupal\colorized_gmap\Plugin\Block
View source
class ColorizedGmapBlock extends BlockBase implements ContainerFactoryPluginInterface {
  use MessengerTrait;

  /**
   * Configuration Factory.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * Construct.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Config\ConfigFactory $configFactory
   *   Configuration Factory.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactory $configFactory) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configFactory = $configFactory;
  }

  /**
   * Create.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   Container.
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   *
   * @return \Drupal\Core\Plugin\ContainerFactoryPluginInterface|static
   *   Container factory object.
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {

    // Check Google API Key.
    $this
      ->checkApiKey();
    $form['#attached']['library'][] = 'colorized_gmap/colorized_gmap.gmap_api';
    $form['#attached']['library'][] = 'colorized_gmap/colorized_gmap.block_admin';
    $form['#attached']['drupalSettings'] = $this
      ->getFormattedJsMapAdminSettings();

    // Build long settings form.
    $this
      ->buildFormStylers($form, $form_state);
    $this
      ->buildFormCoordinates($form, $form_state);
    $form['additional_settings'] = [
      '#type' => 'fieldset',
      '#tree' => TRUE,
      '#title' => $this
        ->t('Additional gmap api settings'),
      '#weight' => 4,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    ];
    $this
      ->buildFormZoom($form, $form_state);
    $this
      ->buildFormControls($form, $form_state);
    $this
      ->buildFormControlsPosition($form, $form_state);
    $this
      ->buildFormMarker($form, $form_state);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'coordinates' => [
        'latitude' => '48.853358',
        'longitude' => '2.348903',
      ],
      'colorized_map_styles' => [],
      'additional_settings' => [
        'controls' => [
          'min_drag_width' => 0,
          'streetViewControl' => TRUE,
          'panControl' => TRUE,
          'mapTypeControl' => TRUE,
        ],
        'controls_position' => [
          'streetViewControl' => '1',
          'panControl' => '1',
          'mapTypeControl' => '3',
        ],
        'zoom_controls' => [
          'zoom' => '15',
          'zoomControl' => TRUE,
          'scrollwheel' => TRUE,
          'zoomControlSize' => '2',
          'zoomControlPosition' => '1',
        ],
        'marker_settings' => [
          'displayPopupContent' => '',
          'marker' => [
            'url' => '',
          ],
          'markertitle' => $this
            ->t('Destination'),
          'scrollwheel' => TRUE,
          'info_window' => [
            'format' => NULL,
            'value' => '',
          ],
        ],
      ],
      'machine_name' => '',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $this->configuration['coordinates'] = $values['coordinates'];
    $this->configuration['colorized_map_styles'] = $values['colorized_map_styles'];
    $this->configuration['additional_settings'] = $values['additional_settings'];

    // Save entity id in the block configuration page.
    // I cannot get block id on block build
    // so I save entity id in such ugly way.
    // todo: need to change it somehow.
    $user_input = $form_state
      ->getUserInput();
    if (isset($user_input['id']) && $user_input['id']) {
      $this->configuration['machine_name'] = $user_input['id'];
    }

    // Process file save marker.
    //    if ($values['additional_settings']['marker_settings']['marker'][0]) {
    //      // Remove previous file.
    //      $fid = $values['additional_settings']['marker_settings']['marker'][0];
    //      $file = \Drupal::entityTypeManager()->getStorage('file')->load($fid);
    //      $file->delete();
    //
    //      // Save new one.
    //      $fid = $values['additional_settings']['marker_settings']['marker'][0];
    //      $file = \Drupal::entityTypeManager()->getStorage('file')->load($fid);
    //      $file->setPermanent();
    //      $file->save();
    //    }
  }

  /**
   * {@inheritdoc}
   */
  public function build() {

    // Check API key.
    $this
      ->checkApiKey();
    return [
      '#machine_name' => $this
        ->getConfiguration()['machine_name'],
      '#theme' => 'colorized_gmap_output',
      '#attached' => [
        'library' => [
          'colorized_gmap/colorized_gmap.gmap_api',
          'colorized_gmap/colorized_gmap.block',
        ],
        'drupalSettings' => $this
          ->getFormattedJsMapSettings(),
      ],
    ];
  }

  /**
   * Helper function check if google api keys is set.
   */
  public function checkApiKey() {
    $config = $this->configFactory
      ->get('colorized_gmap.settings');
    $api_key = $config
      ->get('colorized_gmap_api_key');
    if (empty($api_key)) {
      $url = Url::fromUri('http://googlegeodevelopers.blogspot.ru/2016/06/building-for-scale-updates-to-google.html');
      $url
        ->setOptions([
        'external' => TRUE,
        'attributes' => [
          'target' => '_blank',
        ],
      ]);
      $info_link = Link::fromTextAndUrl($this
        ->t('api key'), $url)
        ->toString();
      $url = Url::fromUri('https://developers.google.com/maps/documentation/javascript/get-api-key');
      $url
        ->setOptions([
        'external' => TRUE,
        'attributes' => [
          'target' => '_blank',
        ],
      ]);
      $get_key_link = Link::fromTextAndUrl($this
        ->t('this'), $url)
        ->toString();
      $url = Url::fromRoute('colorized_gmap.admin_settings');
      $settings_link = Link::fromTextAndUrl($this
        ->t('module settings page'), $url)
        ->toString();
      $missing_api_key_message = $this
        ->t('Google maps are no longer working without @info. Please visit @get-key page to get API key and follow further instructions. After that, please enter your api key on @settings-page.', [
        '@info' => $info_link,
        '@get-key' => $get_key_link,
        '@settings-page' => $settings_link,
      ]);
      $this
        ->messenger()
        ->addWarning($missing_api_key_message);
    }
  }

  /**
   * Helper function returns options for map controls positions.
   *
   * (comes from GMAP api v3 reference).
   */
  public function getPositionOptions() {
    return [
      '1' => 'Top Left',
      '2' => 'Top Center',
      '3' => 'Top Right',
      '4' => 'Left Center',
      '5' => 'Left Top',
      '6' => 'Left Bottom',
      '7' => 'Right Top',
      '8' => 'Right Center',
      '9' => 'Right Bottom',
      '10' => 'Bottom Left',
      '11' => 'Bottom Center',
      '12' => 'Bottom Right',
    ];
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Callback for both ajax-enabled buttons.
   *
   * Selects and returns the fieldset with the names in it.
   *
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   *
   * @return mixed
   *   Colorized map styles settings.
   */
  public function stylesUpdateCallback(array &$form, FormStateInterface $form_state) {
    return $form['settings']['colorized_map_styles'];
  }

  /**
   * Ajax callback for updating colorized map settings.
   *
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   Ajax response.
   */
  public function stylesMapUpdateCallback(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $settings = ColorizedGmapBlock::getFormattedJsMapAdminSettings($values['settings']);
    $response = new AjaxResponse();
    $response
      ->addCommand(new SettingsCommand($settings, TRUE));
    return $response;
  }

  /**
   * Helper function.
   *
   * Builds formatted settigns array based on form state values.
   */
  public function getFormattedJsMapSettings() {
    $config = $this
      ->getConfiguration();
    $settings = [
      'coordinates' => $config['coordinates'],
      'style' => $config['colorized_map_styles'],
      'additional_settings' => $config['additional_settings'],
      'machine_name' => $config['machine_name'],
    ];
    return [
      'colorized_gmap' => [
        $config['machine_name'] => $settings,
      ],
    ];
  }

  /**
   * Helper function.
   *
   * Builds formatted settigns array based on form state values.
   *
   * @param null $config
   *   Configuration array.
   *
   * @return array
   *   Colorized gmap settings array.
   */
  public function getFormattedJsMapAdminSettings($config = NULL) {
    if (!$config) {
      $config = $this
        ->getConfiguration();
    }
    $settings = [
      'coordinates' => $config['coordinates'],
      'style' => $config['colorized_map_styles'],
      'additional_settings' => $config['additional_settings'],
    ];
    return [
      'colorized_gmap' => $settings,
    ];
  }

  /**
   * Submit handler for the "add-one-more" button.
   *
   * Increments the max counter and causes a rebuild.
   *
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   */
  public function stylesAddOneMore(array &$form, FormStateInterface $form_state) {
    $styles_count = $form_state
      ->get('styles_count');
    $styles_count++;
    $form_state
      ->set('styles_count', $styles_count);
    $form_state
      ->setRebuild();
  }

  /**
   * Submit handler for the "remove one" button.
   *
   * Decrements the max counter and causes a form rebuild.
   *
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   */
  public function stylesRemoveOne(array &$form, FormStateInterface $form_state) {
    $styles_count = $form_state
      ->get('styles_count');
    if ($styles_count > 1) {
      $styles_count--;
      $form_state
        ->set('styles_count', $styles_count);
    }
    $form_state
      ->setRebuild();
  }

  /**
   * Submit handler for the "remove one" button.
   *
   * Decrements the max counter and causes a form rebuild.
   *
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   */
  public function removeCallback(array &$form, FormStateInterface $form_state) {
    $name_field = $form_state
      ->get('num_names');
    if ($name_field > 1) {
      $remove_button = $name_field - 1;
      $form_state
        ->set('num_names', $remove_button);
    }
    $form_state
      ->setRebuild();
  }

  /**
   * Helper function. Create form elements for map zoom position settings.
   *
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   */
  public function buildFormCoordinates(array &$form, FormStateInterface &$form_state) {
    $form['coordinates'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Coordinates'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
      '#weight' => -1,
    ];
    $form['coordinates']['latitude'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Latitude'),
      '#size' => 10,
      '#weight' => 0,
      '#default_value' => $this->configuration['coordinates']['latitude'],
      '#ajax' => [
        'callback' => 'Drupal\\colorized_gmap\\Plugin\\Block\\ColorizedGmapBlock::stylesMapUpdateCallback',
        'wrapper' => 'colorized-gmap-content',
        'event' => 'change',
      ],
    ];
    $form['coordinates']['longitude'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Longitude'),
      '#size' => 10,
      '#weight' => 2,
      '#default_value' => $this->configuration['coordinates']['longitude'],
      '#ajax' => [
        'callback' => 'Drupal\\colorized_gmap\\Plugin\\Block\\ColorizedGmapBlock::stylesMapUpdateCallback',
        'event' => 'change',
        'wrapper' => 'colorized-gmap-content',
      ],
    ];
  }

  /**
   * Helper function. Create form elements for map zoom settings.
   *
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   */
  public function buildFormZoom(array &$form, FormStateInterface &$form_state) {

    // @todo: get existing configs.
    $form['additional_settings']['zoom_controls'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Zoom control settings'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    ];
    $form['additional_settings']['zoom_controls']['zoom'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Zoom'),
      '#size' => 10,
      '#default_value' => $this->configuration['additional_settings']['zoom_controls']['zoom'],
      '#description' => $this
        ->t('Enter zoom amount'),
    ];
    $form['additional_settings']['zoom_controls']['zoomControl'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable zoom control'),
      '#default_value' => $this->configuration['additional_settings']['zoom_controls']['zoomControl'],
    ];
    $form['additional_settings']['zoom_controls']['scrollwheel'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable scrollwheel zoom'),
      '#default_value' => $this->configuration['additional_settings']['zoom_controls']['scrollwheel'],
    ];
    $form['additional_settings']['zoom_controls']['zoomControlSize'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Zoom Control Size'),
      '#options' => [
        '1' => 'Small',
        '2' => 'Large',
      ],
      '#default_value' => $this->configuration['additional_settings']['zoom_controls']['zoomControlSize'],
    ];
    $form['additional_settings']['zoom_controls']['zoomControlPosition'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Zoom Control Position'),
      '#options' => $this
        ->getPositionOptions(),
      '#default_value' => $this->configuration['additional_settings']['zoom_controls']['zoomControlPosition'],
    ];
  }

  /**
   * Helper function. Create form elements for map controls settings.
   *
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   * @param null $entity
   *   Entity.
   */
  public function buildFormControls(array &$form, FormStateInterface &$form_state, $entity = NULL) {

    // @todo: get existing configs.
    $form['additional_settings']['controls'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Controls'),
      '#weight' => 1,
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    ];
    $form['additional_settings']['controls']['min_drag_width'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Min draggable screnn width'),
      '#size' => 5,
      '#description' => $this
        ->t('If your screen width is greater, the map will be draggable. Enter 0 to make map always draggable.'),
      '#default_value' => $this->configuration['additional_settings']['controls']['min_drag_width'],
      '#field_suffix' => 'px',
    ];
    $form['additional_settings']['controls']['streetViewControl'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable streetview control'),
      '#default_value' => $this->configuration['additional_settings']['controls']['min_drag_width'],
    ];
    $form['additional_settings']['controls']['panControl'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable pan control'),
      '#default_value' => $this->configuration['additional_settings']['controls']['panControl'],
    ];
    $form['additional_settings']['controls']['mapTypeControl'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable map type control'),
      '#default_value' => $this->configuration['additional_settings']['controls']['mapTypeControl'],
    ];
  }

  /**
   * Helper function. Create form elements for map controls position settings.
   *
   * @param array $form
   *   Form array.
   * @param FormStateInterface $form_state
   *   Form state.
   */
  public function buildFormControlsPosition(array &$form, FormStateInterface &$form_state) {

    // @todo: get existing configs.
    $form['additional_settings']['controls_position'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Controls Position'),
      '#weight' => 2,
      '#attributes' => [
        'class' => [
          'controls_position',
        ],
      ],
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    ];
    $form['additional_settings']['controls_position']['streetViewControl'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Streetview control position'),
      '#options' => $this
        ->getPositionOptions(),
      '#default_value' => $this->configuration['additional_settings']['controls_position']['streetViewControl'],
    ];
    $form['additional_settings']['controls_position']['panControl'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Pan control position'),
      '#options' => $this
        ->getPositionOptions(),
      '#default_value' => $this->configuration['additional_settings']['controls_position']['panControl'],
    ];
    $form['additional_settings']['controls_position']['mapTypeControl'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Map type control position'),
      '#options' => $this
        ->getPositionOptions(),
      '#default_value' => $this->configuration['additional_settings']['controls_position']['mapTypeControl'],
    ];
  }

  /**
   * Helper function. Create form elements for map marker settings.
   *
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   */
  public function buildFormMarker(array &$form, FormStateInterface &$form_state) {
    $form['additional_settings']['marker_settings'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Marker'),
      '#attributes' => [
        'class' => [
          'gmap_colorizer_input',
        ],
      ],
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    ];
    $form['additional_settings']['marker_settings']['markertitle'] = [
      '#type' => 'textfield',
      '#size' => 30,
      '#title' => $this
        ->t('Title'),
      '#default_value' => $this->configuration['additional_settings']['marker_settings']['markertitle'],
      '#description' => $this
        ->t('Title to display on the mouseover'),
    ];
    $form['additional_settings']['marker_settings']['displayPopupContent'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t("Open a marker\\'s content when the page is loaded"),
      '#default_value' => $this->configuration['additional_settings']['marker_settings']['displayPopupContent'],
    ];
    $form['additional_settings']['marker_settings']['info_window'] = [
      '#type' => 'text_format',
      '#title' => $this
        ->t('Marker Popup Content (info window)'),
      '#description' => $this
        ->t('Text for info window. An InfoWindow displays content (usually text or images) in a popup window above the map after clicking on the marker'),
      '#format' => $this->configuration['additional_settings']['marker_settings']['info_window']['format'],
      '#default_value' => $this->configuration['additional_settings']['marker_settings']['info_window']['value'],
      '#ajax' => [
        'callback' => 'Drupal\\colorized_gmap\\Plugin\\Block\\ColorizedGmapBlock::stylesMapUpdateCallback',
        'event' => 'change',
        'wrapper' => 'colorized-gmap-content',
      ],
    ];
    $form['additional_settings']['marker_settings']['marker'] = [
      'url' => [
        '#type' => 'textfield',
        '#size' => 30,
        '#title' => $this
          ->t('Url icon'),
        '#default_value' => $this->configuration['additional_settings']['marker_settings']['marker']['url'],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockPluginInterface::BLOCK_LABEL_VISIBLE constant Indicates the block label (title) should be displayed to end users.
BlockPluginTrait::$transliteration protected property The transliteration service.
BlockPluginTrait::access public function
BlockPluginTrait::baseConfigurationDefaults protected function Returns generic default configuration for block plugins.
BlockPluginTrait::blockAccess protected function Indicates whether the block should be shown. 16
BlockPluginTrait::blockValidate public function 3
BlockPluginTrait::buildConfigurationForm public function Creates a generic configuration form for all block types. Individual block plugins can add elements to this form by overriding BlockBase::blockForm(). Most block plugins should not override this method unless they need to alter the generic form elements. 2
BlockPluginTrait::calculateDependencies public function
BlockPluginTrait::getConfiguration public function 1
BlockPluginTrait::getMachineNameSuggestion public function 1
BlockPluginTrait::getPreviewFallbackString public function 3
BlockPluginTrait::label public function
BlockPluginTrait::setConfiguration public function
BlockPluginTrait::setConfigurationValue public function
BlockPluginTrait::setTransliteration public function Sets the transliteration service.
BlockPluginTrait::submitConfigurationForm public function Most block plugins should not override this method. To add submission handling for a specific block type, override BlockBase::blockSubmit().
BlockPluginTrait::transliteration protected function Wraps the transliteration service.
BlockPluginTrait::validateConfigurationForm public function Most block plugins should not override this method. To add validation for a specific block type, override BlockBase::blockValidate(). 1
ColorizedGmapBlock::$configFactory protected property Configuration Factory.
ColorizedGmapBlock::blockForm public function Overrides BlockPluginTrait::blockForm
ColorizedGmapBlock::blockSubmit public function Overrides BlockPluginTrait::blockSubmit
ColorizedGmapBlock::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build
ColorizedGmapBlock::buildFormControls public function Helper function. Create form elements for map controls settings.
ColorizedGmapBlock::buildFormControlsPosition public function Helper function. Create form elements for map controls position settings.
ColorizedGmapBlock::buildFormCoordinates public function Helper function. Create form elements for map zoom position settings.
ColorizedGmapBlock::buildFormMarker public function Helper function. Create form elements for map marker settings.
ColorizedGmapBlock::buildFormStylers public function
ColorizedGmapBlock::buildFormZoom public function Helper function. Create form elements for map zoom settings.
ColorizedGmapBlock::checkApiKey public function Helper function check if google api keys is set.
ColorizedGmapBlock::create public static function Create. Overrides ContainerFactoryPluginInterface::create
ColorizedGmapBlock::defaultConfiguration public function Overrides BlockPluginTrait::defaultConfiguration
ColorizedGmapBlock::getFormattedJsMapAdminSettings public function Helper function.
ColorizedGmapBlock::getFormattedJsMapSettings public function Helper function.
ColorizedGmapBlock::getPositionOptions public function Helper function returns options for map controls positions.
ColorizedGmapBlock::removeCallback public function Submit handler for the "remove one" button.
ColorizedGmapBlock::stylesAddOneMore public function Submit handler for the "add-one-more" button.
ColorizedGmapBlock::stylesMapUpdateCallback public function Ajax callback for updating colorized map settings.
ColorizedGmapBlock::stylesRemoveOne public function Submit handler for the "remove one" button.
ColorizedGmapBlock::stylesUpdateCallback public function Callback for both ajax-enabled buttons.
ColorizedGmapBlock::__construct public function Construct. Overrides BlockPluginTrait::__construct
ContextAwarePluginAssignmentTrait::addContextAssignmentElement protected function Builds a form element for assigning a context to a given slot.
ContextAwarePluginAssignmentTrait::contextHandler protected function Wraps the context handler.
ContextAwarePluginBase::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginBase::$contexts Deprecated private property Data objects representing the contexts passed in the plugin configuration.
ContextAwarePluginBase::createContextFromConfiguration protected function Overrides ContextAwarePluginBase::createContextFromConfiguration
ContextAwarePluginBase::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts 9
ContextAwarePluginBase::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge 7
ContextAwarePluginBase::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags 4
ContextAwarePluginBase::getContext public function This code is identical to the Component in order to pick up a different Context class. Overrides ContextAwarePluginBase::getContext
ContextAwarePluginBase::getContextDefinition public function Overrides ContextAwarePluginBase::getContextDefinition
ContextAwarePluginBase::getContextDefinitions public function Overrides ContextAwarePluginBase::getContextDefinitions
ContextAwarePluginBase::getContextMapping public function Gets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::getContextMapping
ContextAwarePluginBase::getContexts public function Gets the defined contexts. Overrides ContextAwarePluginInterface::getContexts
ContextAwarePluginBase::getContextValue public function Gets the value for a defined context. Overrides ContextAwarePluginInterface::getContextValue
ContextAwarePluginBase::getContextValues public function Gets the values for all defined contexts. Overrides ContextAwarePluginInterface::getContextValues
ContextAwarePluginBase::setContext public function Set a context on this plugin. Overrides ContextAwarePluginBase::setContext
ContextAwarePluginBase::setContextMapping public function Sets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::setContextMapping
ContextAwarePluginBase::setContextValue public function Sets the value for a defined context. Overrides ContextAwarePluginBase::setContextValue
ContextAwarePluginBase::validateContexts public function Validates the set values for the defined contexts. Overrides ContextAwarePluginInterface::validateContexts
ContextAwarePluginBase::__get public function Implements magic __get() method.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginWithFormsTrait::getFormClass public function
PluginWithFormsTrait::hasFormClass public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2
TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2