You are here

abstract class DataProviderBase in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 src/DataProviderBase.php \Drupal\geolocation\DataProviderBase

Class DataProviderBase.

@package Drupal\geolocation

Hierarchy

Expanded class hierarchy of DataProviderBase

4 files declare their use of DataProviderBase
AddressFieldProvider.php in modules/geolocation_address/src/Plugin/geolocation/DataProvider/AddressFieldProvider.php
Geofield.php in modules/geolocation_geofield/src/Plugin/geolocation/DataProvider/Geofield.php
GeolocationFieldProvider.php in src/Plugin/geolocation/DataProvider/GeolocationFieldProvider.php
SearchAPI.php in modules/geolocation_search_api/src/Plugin/geolocation/DataProvider/SearchAPI.php

File

src/DataProviderBase.php, line 21

Namespace

Drupal\geolocation
View source
abstract class DataProviderBase extends PluginBase implements DataProviderInterface, ContainerFactoryPluginInterface {

  /**
   * Entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * Views field.
   *
   * @var \Drupal\views\Plugin\views\field\FieldPluginBase
   */
  protected $viewsField;

  /**
   * Field definition.
   *
   * @var \Drupal\Core\Field\FieldDefinitionInterface
   */
  protected $fieldDefinition;

  /**
   * Constructs a new GeocoderBase object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   Entity type manager.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritdoc}
   */
  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')) {

      // Add the token UI from the token module if present.
      $element['token_help'] = [
        '#theme' => 'token_tree_link',
        '#prefix' => '<h4>' . $this
          ->t('Additional Tokens') . '</h4>',
        '#token_types' => [
          $fieldDefinition
            ->getTargetEntityTypeId(),
        ],
        '#weight' => 100,
      ];
    }
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Token replacement support function, callback to token replacement function.
   *
   * @param array $replacements
   *   An associative array variable containing mappings from token names to
   *   values (for use with strtr()).
   * @param array $data
   *   Current item replacements.
   * @param array $options
   *   A keyed array of settings and flags to control the token replacement
   *   process. See \Drupal\Core\Utility\Token::replace().
   */
  public function fieldItemTokens(array &$replacements, array $data, array $options) {
    if (isset($data['geolocation_current_item'])) {

      /** @var \Drupal\Core\Field\FieldItemInterface $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();
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function isViewsGeoOption(FieldPluginBase $viewsField) {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  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']})) {

      /** @var \Drupal\Core\Field\FieldItemListInterface $geo_items */
      $geo_items = $entity->{$viewsField->definition['field_name']};
      foreach ($geo_items as $item) {
        $positions[] = $this
          ->getPositionsFromItem($item);
      }
    }
    return $positions;
  }

  /**
   * {@inheritdoc}
   */
  public function setViewsField(FieldPluginBase $viewsField) {
    $this->viewsField = $viewsField;
  }

  /**
   * {@inheritdoc}
   */
  public function setFieldDefinition(FieldDefinitionInterface $fieldDefinition) {
    $this->fieldDefinition = $fieldDefinition;
  }

  /**
   * {@inheritdoc}
   */
  public function getPositionsFromItem(FieldItemInterface $fieldItem) {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array $settings, array $parents = []) {
    return [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DataProviderBase::$entityFieldManager protected property Entity field manager.
DataProviderBase::$fieldDefinition protected property Field definition.
DataProviderBase::$viewsField protected property Views field.
DataProviderBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
DataProviderBase::fieldItemTokens public function Token replacement support function, callback to token replacement function.
DataProviderBase::getPositionsFromItem public function Get positions from field item list. Overrides DataProviderInterface::getPositionsFromItem 3
DataProviderBase::getPositionsFromViewsRow public function Get positions from views row. Overrides DataProviderInterface::getPositionsFromViewsRow 1
DataProviderBase::getSettingsForm public function Provide data provider settings form array. Overrides DataProviderInterface::getSettingsForm 1
DataProviderBase::getTokenHelp public function Return field item tokens. Overrides DataProviderInterface::getTokenHelp 1
DataProviderBase::isViewsGeoOption public function Determine valid views option. Overrides DataProviderInterface::isViewsGeoOption 4
DataProviderBase::replaceFieldItemTokens public function Replace field item tokens. Overrides DataProviderInterface::replaceFieldItemTokens 1
DataProviderBase::setFieldDefinition public function Set field definition. Overrides DataProviderInterface::setFieldDefinition
DataProviderBase::setViewsField public function Set views field. Overrides DataProviderInterface::setViewsField
DataProviderBase::__construct public function Constructs a new GeocoderBase object. Overrides PluginBase::__construct 1
DataProviderInterface::isFieldGeoOption public function Determine valid field geo option. 4
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.