class AddressFieldProvider in Geolocation Field 8.3
Same name and namespace in other branches
- 8.2 modules/geolocation_address/src/Plugin/geolocation/DataProvider/AddressFieldProvider.php \Drupal\geolocation_address\Plugin\geolocation\DataProvider\AddressFieldProvider
Provides default address field.
Plugin annotation
@DataProvider(
id = "geolocation_address_field_provider",
name = @Translation("Address Field"),
description = @Translation("Address Field."),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\geolocation\DataProviderBase implements ContainerFactoryPluginInterface, DataProviderInterface
- class \Drupal\geolocation_address\Plugin\geolocation\DataProvider\AddressFieldProvider implements DataProviderInterface
- class \Drupal\geolocation\DataProviderBase implements ContainerFactoryPluginInterface, DataProviderInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of AddressFieldProvider
File
- modules/
geolocation_address/ src/ Plugin/ geolocation/ DataProvider/ AddressFieldProvider.php, line 28
Namespace
Drupal\geolocation_address\Plugin\geolocation\DataProviderView source
class AddressFieldProvider extends DataProviderBase implements DataProviderInterface {
/**
* Geocoder manager.
*
* @var \Drupal\geolocation\GeocoderManager
*/
protected $geocoderManager = NULL;
/**
* Geocoder.
*
* @var \Drupal\geolocation\GeocoderInterface
*/
protected $geocoder = NULL;
/**
* The address format repository.
*
* @var \CommerceGuys\Addressing\AddressFormat\AddressFormatRepositoryInterface
*/
protected $addressFormatRepository;
/**
* The country repository.
*
* @var \CommerceGuys\Addressing\Country\CountryRepositoryInterface
*/
protected $countryRepository;
/**
* AddressFieldProvider constructor.
*
* @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.
* @param \Drupal\geolocation\GeocoderManager $geocoder_manager
* Geocoder Manager.
* @param \CommerceGuys\Addressing\AddressFormat\AddressFormatRepositoryInterface $address_format_repository
* The address format repository.
* @param \CommerceGuys\Addressing\Country\CountryRepositoryInterface $country_repository
* The country repository.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityFieldManagerInterface $entity_field_manager, GeocoderManager $geocoder_manager, AddressFormatRepositoryInterface $address_format_repository, CountryRepositoryInterface $country_repository) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_field_manager);
$this->geocoderManager = $geocoder_manager;
if (!empty($configuration['geocoder'])) {
$this->geocoder = $this->geocoderManager
->createInstance($configuration['geocoder']);
}
$this->addressFormatRepository = $address_format_repository;
$this->countryRepository = $country_repository;
}
/**
* {@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'), $container
->get('plugin.manager.geolocation.geocoder'), $container
->get('address.address_format_repository'), $container
->get('address.country_repository'));
}
/**
* {@inheritdoc}
*/
public function isViewsGeoOption(FieldPluginBase $views_field) {
if ($views_field instanceof EntityField) {
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager */
$entityFieldManager = \Drupal::service('entity_field.manager');
$field_map = $entityFieldManager
->getFieldMap();
if (!empty($field_map) && !empty($views_field->configuration['entity_type']) && !empty($views_field->configuration['field_name']) && !empty($field_map[$views_field->configuration['entity_type']]) && !empty($field_map[$views_field->configuration['entity_type']][$views_field->configuration['field_name']])) {
if ($field_map[$views_field->configuration['entity_type']][$views_field->configuration['field_name']]['type'] == 'address') {
return TRUE;
}
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function isFieldGeoOption(FieldDefinitionInterface $fieldDefinition) {
return $fieldDefinition
->getType() == 'address';
}
/**
* {@inheritdoc}
*/
public function getPositionsFromItem(FieldItemInterface $item) {
if (!$item instanceof AddressItem) {
return [];
}
if (empty($this->geocoder)) {
return [];
}
$address_format = str_replace([
"\r",
"\n",
], ' ', $this->addressFormatRepository
->get($item
->getCountryCode())
->getFormat());
$formatted_address = new FormattableMarkup(str_replace('%', ':', $address_format), [
':givenName' => $item
->getGivenName(),
':familyName' => $item
->getFamilyName(),
':organization' => $item
->getOrganization(),
':addressLine1' => $item
->getAddressLine1(),
':addressLine2' => $item
->getAddressLine2(),
':dependentLocality' => $item
->getDependentLocality(),
':locality' => $item
->getLocality(),
':administrativeArea' => $item
->getAdministrativeArea(),
':postalCode' => $item
->getPostalCode(),
':sortingCode' => $item
->getSortingCode(),
]);
$address = (string) $formatted_address;
$address = trim($address);
$address = $address . ' ' . $this->countryRepository
->get($item
->getCountryCode())
->getName();
$coordinates = $this->geocoder
->geocode($address);
return [
$coordinates['location'],
];
}
/**
* {@inheritdoc}
*/
public function getSettingsForm(array $settings, array $parents = []) {
$element = parent::getSettingsForm($settings, $parents);
$geocoder_options = [];
foreach ($this->geocoderManager
->getDefinitions() as $geocoder_id => $geocoder_definition) {
if (empty($geocoder_definition['locationCapable'])) {
continue;
}
$geocoder_options[$geocoder_id] = $geocoder_definition['name'];
}
if (empty($geocoder_options)) {
return [
'#markup' => $this
->t('No geocoder option found'),
];
}
$element['geocoder'] = [
'#type' => 'select',
'#title' => $this
->t('Geocoder'),
'#options' => $geocoder_options,
'#default_value' => empty($settings['geocoder']) ? key($geocoder_options) : $settings['geocoder'],
'#description' => $this
->t('Choose plugin to geocode address into coordinates.'),
'#weight' => -1,
];
return $element;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddressFieldProvider:: |
protected | property | The address format repository. | |
AddressFieldProvider:: |
protected | property | The country repository. | |
AddressFieldProvider:: |
protected | property | Geocoder. | |
AddressFieldProvider:: |
protected | property | Geocoder manager. | |
AddressFieldProvider:: |
public static | function |
Creates an instance of the plugin. Overrides DataProviderBase:: |
|
AddressFieldProvider:: |
public | function |
Get positions from field item list. Overrides DataProviderBase:: |
|
AddressFieldProvider:: |
public | function |
Provide data provider settings form array. Overrides DataProviderBase:: |
|
AddressFieldProvider:: |
public | function |
Determine valid field geo option. Overrides DataProviderInterface:: |
|
AddressFieldProvider:: |
public | function |
Determine valid views option. Overrides DataProviderBase:: |
|
AddressFieldProvider:: |
public | function |
AddressFieldProvider constructor. Overrides DataProviderBase:: |
|
DataProviderBase:: |
protected | property | Entity field manager. | |
DataProviderBase:: |
protected | property | Field definition. | |
DataProviderBase:: |
protected | property | Views field. | |
DataProviderBase:: |
protected | function | Default settings. | 2 |
DataProviderBase:: |
public | function | Token replacement support function, callback to token replacement function. | |
DataProviderBase:: |
protected | function | ||
DataProviderBase:: |
public | function |
Get locations from field item list. Overrides DataProviderInterface:: |
2 |
DataProviderBase:: |
public | function |
Get locations from views row. Overrides DataProviderInterface:: |
1 |
DataProviderBase:: |
public | function |
Get positions from views row. Overrides DataProviderInterface:: |
1 |
DataProviderBase:: |
protected | function | Add default settings. | |
DataProviderBase:: |
public | function |
Get shapes from field item list. Overrides DataProviderInterface:: |
2 |
DataProviderBase:: |
public | function |
Get shapes from views row. Overrides DataProviderInterface:: |
1 |
DataProviderBase:: |
public | function |
Return field item tokens. Overrides DataProviderInterface:: |
1 |
DataProviderBase:: |
public | function |
Replace field item tokens. Overrides DataProviderInterface:: |
1 |
DataProviderBase:: |
public | function |
Set field definition. Overrides DataProviderInterface:: |
|
DataProviderBase:: |
public | function |
Set views field. Overrides DataProviderInterface:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |