You are here

public function FirstRow::getCoordinates in Geolocation Field 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/geolocation/Location/FirstRow.php \Drupal\geolocation\Plugin\geolocation\Location\FirstRow::getCoordinates()

Get map location.

Parameters

int $location_option_id: Location option ID.

array $location_option_settings: The current feature settings.

mixed $context: Context like field formatter, field widget or view.

Return value

array With content 'lat' => latitude 'lng' => longitude

Overrides LocationBase::getCoordinates

File

src/Plugin/geolocation/Location/FirstRow.php, line 40

Class

FirstRow
Derive center from first row.

Namespace

Drupal\geolocation\Plugin\geolocation\Location

Code

public function getCoordinates($location_option_id, array $location_option_settings, $context = NULL) {
  if (!($displayHandler = self::getViewsDisplayHandler($context))) {
    return parent::getCoordinates($location_option_id, $location_option_settings, $context);
  }
  $views_style = $displayHandler
    ->getPlugin('style');
  if (empty($views_style->options['geolocation_field'])) {
    return parent::getCoordinates($location_option_id, $location_option_settings, $context);
  }

  /** @var \Drupal\geolocation\Plugin\views\field\GeolocationField $geolocation_field */
  $geolocation_field = $views_style->view->field[$views_style->options['geolocation_field']];
  if (empty($geolocation_field)) {
    return parent::getCoordinates($location_option_id, $location_option_settings, $context);
  }
  if (empty($views_style->view->result[0])) {
    return parent::getCoordinates($location_option_id, $location_option_settings, $context);
  }
  $entity = $geolocation_field
    ->getEntity($views_style->view->result[0]);
  if (empty($entity)) {
    return parent::getCoordinates($location_option_id, $location_option_settings, $context);
  }
  if (isset($entity->{$geolocation_field->definition['field_name']})) {

    /** @var \Drupal\geolocation\Plugin\Field\FieldType\GeolocationItem $item */
    $item = $entity->{$geolocation_field->definition['field_name']}
      ->first();
    if (empty($item)) {
      return parent::getCoordinates($location_option_id, $location_option_settings, $context);
    }
    return [
      'lat' => $item
        ->get('lat')
        ->getValue(),
      'lng' => $item
        ->get('lng')
        ->getValue(),
    ];
  }
  return parent::getCoordinates($location_option_id, $location_option_settings, $context);
}