View source
<?php
namespace Drupal\geolocation;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\views\ResultRow;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Component\Utility\Html;
abstract class DataProviderBase extends PluginBase implements DataProviderInterface, ContainerFactoryPluginInterface {
protected $entityFieldManager;
protected $viewsField;
protected $fieldDefinition;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$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_field.manager'));
}
public function getTokenHelp(FieldDefinitionInterface $fieldDefinition = NULL) {
if (empty($fieldDefinition)) {
$fieldDefinition = $this->fieldDefinition;
}
$element = [];
$element['token_items'] = [
'#type' => 'table',
'#prefix' => '<h4>' . $this
->t('Geolocation Item Tokens') . '</h4>',
'#header' => [
$this
->t('Token'),
$this
->t('Description'),
],
];
foreach ($fieldDefinition
->getFieldStorageDefinition()
->getColumns() as $id => $column) {
$item = [
'token' => [
'#plain_text' => '[geolocation_current_item:' . $id . ']',
],
];
if (!empty($column['description'])) {
$item['description'] = [
'#plain_text' => $column['description'],
];
}
$element['token_items'][] = $item;
}
if (\Drupal::service('module_handler')
->moduleExists('token') && method_exists($fieldDefinition, 'getTargetEntityTypeId')) {
$element['token_help'] = [
'#theme' => 'token_tree_link',
'#prefix' => '<h4>' . $this
->t('Additional Tokens') . '</h4>',
'#token_types' => [
$fieldDefinition
->getTargetEntityTypeId(),
],
'#weight' => 100,
];
}
return $element;
}
public function replaceFieldItemTokens($text, FieldItemInterface $fieldItem) {
$token_context['geolocation_current_item'] = $fieldItem;
$entity = NULL;
try {
$entity = $fieldItem
->getParent()
->getParent()
->getValue();
} catch (\Exception $e) {
}
if (is_object($entity) && $entity instanceof ContentEntityInterface) {
$token_context[$entity
->getEntityTypeId()] = $entity;
}
$text = \Drupal::token()
->replace($text, $token_context, [
'callback' => [
$this,
'fieldItemTokens',
],
'clear' => TRUE,
]);
$text = Html::decodeEntities($text);
return $text;
}
public function fieldItemTokens(array &$replacements, array $data, array $options) {
if (isset($data['geolocation_current_item'])) {
$item = $data['geolocation_current_item'];
foreach ($this->fieldDefinition
->getFieldStorageDefinition()
->getColumns() as $id => $column) {
if ($item
->get($id) && isset($replacements['[geolocation_current_item:' . $id . ']'])) {
$replacements['[geolocation_current_item:' . $id . ']'] = $item
->get($id)
->getValue();
}
}
}
}
public function isViewsGeoOption(FieldPluginBase $viewsField) {
return FALSE;
}
public function getPositionsFromViewsRow(ResultRow $row, FieldPluginBase $viewsField = NULL) {
if (empty($viewsField)) {
$viewsField = $this->viewsField;
}
$positions = [];
$entity = $viewsField
->getEntity($row);
if (isset($entity->{$viewsField->definition['field_name']})) {
$geo_items = $entity->{$viewsField->definition['field_name']};
foreach ($geo_items as $item) {
$positions[] = $this
->getPositionsFromItem($item);
}
}
return $positions;
}
public function setViewsField(FieldPluginBase $viewsField) {
$this->viewsField = $viewsField;
}
public function setFieldDefinition(FieldDefinitionInterface $fieldDefinition) {
$this->fieldDefinition = $fieldDefinition;
}
public function getPositionsFromItem(FieldItemInterface $fieldItem) {
return [];
}
public function getSettingsForm(array $settings, array $parents = []) {
return [];
}
}