You are here

public function GeofieldMapLegend::build in Geofield Map 8.2

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/GeofieldMapLegend.php, line 332

Class

GeofieldMapLegend
Provides a custom Geofield Map Legend block.

Namespace

Drupal\geofield_map\Plugin\Block

Code

public function build() {
  $legend = [];
  $build = [
    '#type' => 'container',
    '#attached' => [
      'library' => [
        'geofield_map/geofield_map_general',
      ],
    ],
  ];
  $selected_geofield_map_legend = $this->configuration['geofield_map_legend'];
  if (!empty($selected_geofield_map_legend)) {
    list($view_id, $view_display_id) = explode(':', $selected_geofield_map_legend);
    $failure_message = $this
      ->t("The Legend can't be rendered because the chosen [@view_id:@view_display_id] view & view display combination don't exists or correspond to a valid Geofield Map Legend anymore. <u>Please reconfigure this Geofield Map Legend block consequently.</u>", [
      '@view_id' => $view_id,
      '@view_display_id' => $view_display_id,
    ]);
    $legend_failure = $this
      ->legendFailureElement($failure_message);
    $map_themer_plugin_and_values = $this
      ->getMapThemerPluginAndValues($view_id, $view_display_id);
    if (!empty($map_themer_plugin_and_values)) {

      /* @var \Drupal\geofield_map\MapThemerInterface $map_themer_plugin */
      list($map_themer_plugin, $theming_values) = $map_themer_plugin_and_values;
      $legend = !empty($theming_values) ? $map_themer_plugin
        ->getLegend($theming_values, $this->configuration) : $this
        ->legendFailureElement($this
        ->t('The legend cannot be rendered due to a wrong setup of the Map Themer in the ViewStyle'));
    }
    else {
      $legend = $legend_failure;
    }
  }
  $build['legend'] = $legend;
  return $build;
}