You are here

public function YaMapsBlock::build in Yandex.Maps 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/YaMapsBlock.php, line 31

Class

YaMapsBlock
Provides a yandex map block.

Namespace

Drupal\yamaps\Plugin\Block

Code

public function build() {
  $config = $this
    ->getConfiguration();
  $block_output = [];
  $coords = Json::decode($config['yamaps_block_coords']);
  if (empty($coords)) {
    return $block_output;
  }
  $id = Html::getUniqueId(implode('-', [
    'ymap',
    'block',
    'yamaps',
  ]));
  $display_type = 'map';
  $width = $config['yamaps_block_width'];
  $height = $config['yamaps_block_height'];
  $traffic = $config['yamaps_block_traffic'];
  $auto_zoom = $config['yamaps_block_auto_zoom'];
  $clusterer = $config['yamaps_block_clusterer'];
  $controls = $config['yamaps_block_controls'];
  $block_type = $config['yamaps_block_type'];
  $placemarks = Json::decode($config['yamaps_block_placemarks']);
  $lines = Json::decode($config['yamaps_block_lines']);
  $polygons = Json::decode($config['yamaps_block_polygons']);
  $routes = Json::decode($config['yamaps_block_routes']);
  $map = [
    'init' => [
      'center' => $coords['center'],
      'zoom' => $coords['zoom'],
      'type' => $block_type,
      'behaviors' => \array_values(\array_filter($config['yamaps_block_behaviors'])),
    ],
    'display_options' => [
      'display_type' => $display_type,
    ],
    'controls' => $controls,
    'traffic' => $traffic,
    'clusterer' => $clusterer,
    'auto_zoom' => $auto_zoom,
    'placemarks' => $placemarks,
    'lines' => $lines,
    'polygons' => $polygons,
    'routes' => $routes,
    'edit' => FALSE,
  ];

  // Return map container div.
  $block_output['map_container'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#attributes' => [
      'id' => $id,
      'style' => 'width:' . $width . '; height:' . $height . ';',
      'class' => [
        'yamaps-map-container',
      ],
    ],
    '#value' => '',
  ];
  $block_output['#attached']['library'][] = 'yamaps/yandex-map-api';
  $block_output['#attached']['library'][] = 'yamaps/yamaps-placemark';
  $block_output['#attached']['library'][] = 'yamaps/yamaps-map';
  $block_output['#attached']['drupalSettings']['yamaps'] = [
    $id => $map,
  ];
  return $block_output;
}