You are here

public function Map::build in Route Planner 8

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/Map.php, line 22

Class

Map
Provides the Map block.

Namespace

Drupal\route_planner\Plugin\Block

Code

public function build() {
  $build = array();

  // Define block content:
  $build['map'] = array(
    '#type' => 'inline_template',
    '#template' => '<div id="map_canvas" style="height: {{ height }}; width: {{ width }};"></div>',
    '#context' => array(
      'height' => \Drupal::config('route_planner.settings')
        ->get('route_planner_map_height'),
      'width' => \Drupal::config('route_planner.settings')
        ->get('route_planner_map_width'),
    ),
  );

  // Add google maps api.
  $build['#attached']['library'][] = 'route_planner/googleapis';

  // Add some custom javascript to display the map.
  $build['#attached']['library'][] = 'route_planner/route_planner';

  // Attach the settings from route_planner settings form.
  $route_settings = array(
    'zoomlevel' => \Drupal::config('route_planner.settings')
      ->get('route_planner_map_zoom'),
    'zoomcontrol' => \Drupal::config('route_planner.settings')
      ->get('route_planner_map_zoomcontrol'),
    'scrollwheel' => \Drupal::config('route_planner.settings')
      ->get('route_planner_map_scrollwheel'),
    'mapTypeControl' => \Drupal::config('route_planner.settings')
      ->get('route_planner_map_maptypecontrol'),
    'scaleControl' => \Drupal::config('route_planner.settings')
      ->get('route_planner_map_scalecontrol'),
    'draggable' => \Drupal::config('route_planner.settings')
      ->get('route_planner_map_draggable'),
    'doubbleclick' => \Drupal::config('route_planner.settings')
      ->get('route_planner_map_doubbleclick'),
    'streetviewcontrol' => \Drupal::config('route_planner.settings')
      ->get('route_planner_map_streetviewcontrol'),
    'overviewmapcontrol' => \Drupal::config('route_planner.settings')
      ->get('route_planner_map_overviewmapcontrol'),
    'unitSystem' => \Drupal::config('route_planner.settings')
      ->get('route_planner_unitsystem'),
    'defaultui' => \Drupal::config('route_planner.settings')
      ->get('route_planner_map_defaultui'),
    'end' => \Drupal::config('route_planner.settings')
      ->get('route_planner_address'),
  );
  $build['#attached']['drupalSettings']['route_planner'] = $route_settings;
  return $build;
}