You are here

public function CurrentVisitorMapBlock::build in IP Geolocation Views & 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/CurrentVisitorMapBlock.php, line 48

Class

CurrentVisitorMapBlock
IPGV&M: Set my location .

Namespace

Drupal\ip_geoloc\Plugin\Block

Code

public function build() {

  // Calling the set location form in block
  // TODO find a way to add subject
  // $block['subject'] = $this->t('Your location');.
  $config = $this
    ->getConfiguration();
  $map_options = isset($config['ip_geoloc_current_visitor_map_options']) ? $config['ip_geoloc_current_visitor_map_options'] : IP_GEOLOC_CURRENT_VISITOR_MAP_OPTIONS;
  $api = \Drupal::service('ip_geoloc.api');
  $location = $api
    ->getVisitorLocation();
  $map_style = $config['ip_geoloc_current_visitor_map_div_style'] ? $config['ip_geoloc_current_visitor_map_div_style'] : IP_GEOLOC_MAP_DIV_DEFAULT_STYLE;
  $div_id = $config['ip_geoloc_current_visitor_map_div_id'] ? $config['ip_geoloc_current_visitor_map_div_id'] : 'ip-geoloc-block-current-visitor-map';

  // TODO check if it is correct.
  $latitude = isset($location['latitude']) ? (double) $location['latitude'] : (empty($location['fixed_address']) ? NULL : 0.0);
  $longitude = isset($location['longitude']) ? (double) $location['longitude'] : (empty($location['fixed_address']) ? NULL : 0.0);
  $info_text = isset($location['formatted_address']) ? $location['formatted_address'] : NULL;
  $settings['ip_geoloc_block'][$div_id] = [
    'ip_geoloc_current_location_map_div' => $div_id,
    'ip_geoloc_current_location_map_style' => $map_style,
    'ip_geoloc_current_location_map_options' => Json::decode($map_options),
    'ip_geoloc_current_location_map_latlng' => [
      $latitude,
      $longitude,
    ],
    'ip_geoloc_current_location_map_info_text' => $info_text,
  ];
  $build = [
    '#theme' => 'ip_geoloc_map_current_visitor',
    '#ip_geoloc_settings' => $settings['ip_geoloc_block'][$div_id],
    // '#ipgeolocsettings' => 'test',.
    '#attached' => [
      'library' => [
        'ip_geoloc/current_location_js',
      ],
      'drupalSettings' => $settings,
    ],
  ];
  return $build;
}