View source
<?php
namespace Drupal\geolocation\Plugin\views\argument;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityArgument extends ProximityArgument implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $entityFieldManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_field.manager'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['geolocation_entity_argument_source'] = [
'default' => '',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
unset($form['description']);
$options = [];
foreach ($this->entityFieldManager
->getFieldMapByFieldType('geolocation') as $entity_type => $fields) {
$entity_type_definition = $this->entityTypeManager
->getDefinition($entity_type);
foreach ($fields as $field_name => $field) {
foreach ($field['bundles'] as $bundle) {
$field_definitions = $this->entityFieldManager
->getFieldDefinitions($entity_type, $bundle);
$options[$entity_type . ':' . $bundle . ':' . $field_name] = $entity_type_definition
->getLabel() . ' - ' . $entity_type_definition
->getBundleLabel() . ' - ' . $field_definitions[$field_name]
->getLabel();
}
}
}
$form['geolocation_entity_argument_source'] = [
'#type' => 'select',
'#title' => $this
->t('Geolocation Entity Argument Source'),
'#options' => $options,
'#weight' => -10,
'#default_value' => $this->options['geolocation_entity_argument_source'],
'#description' => $this
->t('Format should be in the following format: <strong>"654<=5mi"</strong> (defaults to km). Alternatively, just a valid entity ID, for use as reference location in other fields.'),
];
}
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;
}
$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;
}
$item = $field
->first();
$values['lat'] = $item
->get('lat')
->getValue();
$values['lng'] = $item
->get('lng')
->getValue();
return $values;
}
public function getParsedReferenceLocation() {
static $values;
if (!isset($values)) {
preg_match('/^([0-9]+)([<>=]+)([0-9.]+)(.*$)/', $this
->getValue(), $values);
if (empty($values) && !empty($this
->getValue()) && is_numeric($this
->getValue())) {
$values = $this
->getCoordinatesFromEntityId($this
->getValue());
return $values;
}
elseif (empty($values)) {
return [];
}
else {
$values = is_array($values) ? [
'id' => isset($values[1]) && is_numeric($values[1]) ? intval($values[1]) : FALSE,
'operator' => isset($values[2]) && in_array($values[2], [
'<>',
'=',
'>=',
'<=',
'>',
'<',
]) ? $values[2] : '<=',
'distance' => isset($values[3]) ? floatval($values[3]) : FALSE,
'unit' => !empty($values[4]) ? $values[4] : 'km',
] : FALSE;
}
$coordinates = $this
->getCoordinatesFromEntityId($values['id']);
if (empty($coordinates)) {
return [];
}
$values = array_replace($values, $coordinates);
}
return $values;
}
}