You are here

protected function EntityArgument::getCoordinatesFromEntityId in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/views/argument/EntityArgument.php \Drupal\geolocation\Plugin\views\argument\EntityArgument::getCoordinatesFromEntityId()

Get coordinates from entity ID.

Parameters

int $entity_id: Entity ID.

Return value

array|false Coordinates.

1 call to EntityArgument::getCoordinatesFromEntityId()
EntityArgument::getParsedReferenceLocation in src/Plugin/views/argument/EntityArgument.php
Processes the passed argument into an array of relevant geolocation data.

File

src/Plugin/views/argument/EntityArgument.php, line 117

Class

EntityArgument
Argument handler for geolocation.

Namespace

Drupal\geolocation\Plugin\views\argument

Code

protected function getCoordinatesFromEntityId($entity_id) {
  if (empty($this->options['geolocation_entity_argument_source'])) {
    return FALSE;
  }
  $values = [];
  $source_parts = explode(':', $this->options['geolocation_entity_argument_source']);
  $entity_type = $source_parts[0];
  $bundle_type = $source_parts[1];
  $field_name = $source_parts[2];
  if (empty($entity_type) || empty($bundle_type) || empty($field_name)) {
    return FALSE;
  }

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $this->entityTypeManager
    ->getStorage($entity_type)
    ->load($entity_id);
  if (empty($entity)) {
    return FALSE;
  }
  $field = $entity
    ->get($field_name);
  if (empty($field) || $field
    ->isEmpty()) {
    return FALSE;
  }

  /** @var \Drupal\geolocation\Plugin\Field\FieldType\GeolocationItem $item */
  $item = $field
    ->first();
  $values['lat'] = $item
    ->get('lat')
    ->getValue();
  $values['lng'] = $item
    ->get('lng')
    ->getValue();
  return $values;
}