class GeolocationGpx in Geolocation Field 8.3
Provides GPX.
Plugin annotation
@DataProvider(
id = "geolocation_gpx",
name = @Translation("Geolocation GPX"),
description = @Translation("Tracks & Waypoints."),
)
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_gpx\Plugin\geolocation\DataProvider\GeolocationGpx implements DataProviderInterface
- class \Drupal\geolocation\DataProviderBase implements ContainerFactoryPluginInterface, DataProviderInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of GeolocationGpx
File
- modules/
geolocation_gpx/ src/ Plugin/ geolocation/ DataProvider/ GeolocationGpx.php, line 24
Namespace
Drupal\geolocation_gpx\Plugin\geolocation\DataProviderView source
class GeolocationGpx extends DataProviderBase implements DataProviderInterface {
/**
* {@inheritdoc}
*/
protected function defaultSettings() {
$settings = parent::defaultSettings();
$settings['return_tracks'] = TRUE;
$settings['return_waypoints'] = TRUE;
$settings['return_track_locations'] = FALSE;
$settings['return_waypoint_locations'] = FALSE;
$settings['track_stroke_color'] = '#FF0044';
$settings['track_stroke_color_randomize'] = TRUE;
$settings['track_stroke_width'] = 2;
$settings['track_stroke_opacity'] = 1;
return $settings;
}
/**
* {@inheritdoc}
*/
public function isViewsGeoOption(FieldPluginBase $views_field) {
if ($views_field instanceof EntityField && $views_field
->getPluginId() == 'field') {
$field_storage_definitions = $this->entityFieldManager
->getFieldStorageDefinitions($views_field
->getEntityType());
if (!empty($field_storage_definitions[$views_field->field])) {
$field_storage_definition = $field_storage_definitions[$views_field->field];
if ($field_storage_definition
->getType() == 'geolocation_gpx_file') {
return TRUE;
}
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getSettingsForm(array $settings, array $parents = []) {
$element = parent::getSettingsForm($settings, $parents);
$settings = $this
->getSettings($settings);
if (!empty($this->viewsField)) {
$form_parent = "style_options[data_provider_settings]";
}
elseif (!empty($this->fieldDefinition)) {
$form_parent = "fields['" . $this->fieldDefinition
->getName() . "'][settings_edit_form][settings]";
}
else {
$form_parent = '';
}
$element['return_tracks'] = [
'#weight' => -99,
'#type' => 'checkbox',
'#title' => $this
->t('Add tracks'),
'#description' => $this
->t('Will be displayed as polylines; names should show up on hover/click.'),
'#default_value' => $settings['return_tracks'],
];
$element['return_waypoints'] = [
'#weight' => -100,
'#type' => 'checkbox',
'#title' => $this
->t('Add waypoints'),
'#description' => $this
->t('Will be displayed as regular markers, with the name as marker title.'),
'#default_value' => $settings['return_waypoints'],
];
$element['return_track_locations'] = [
'#weight' => -100,
'#type' => 'checkbox',
'#title' => $this
->t('Add raw track locations'),
'#default_value' => $settings['return_track_locations'],
];
$element['return_waypoint_locations'] = [
'#weight' => -100,
'#type' => 'checkbox',
'#title' => $this
->t('Add raw waypoint locations'),
'#default_value' => $settings['return_waypoint_locations'],
];
$element['track_stroke_color'] = [
'#weight' => -98,
'#type' => 'color',
'#title' => $this
->t('Track color'),
'#default_value' => $settings['track_stroke_color'],
'#states' => [
'visible' => [
':input[name="' . $form_parent . '[return_tracks]"]' => [
'checked' => TRUE,
],
],
],
];
$element['track_stroke_color_randomize'] = [
'#weight' => -98,
'#type' => 'checkbox',
'#title' => $this
->t('Randomize track colors'),
'#default_value' => $settings['track_stroke_color_randomize'],
'#states' => [
'visible' => [
':input[name="' . $form_parent . '[return_tracks]"]' => [
'checked' => TRUE,
],
],
],
];
$element['track_stroke_width'] = [
'#weight' => -98,
'#type' => 'number',
'#title' => $this
->t('Track Width'),
'#description' => $this
->t('Width of the tracks in pixels.'),
'#default_value' => $settings['track_stroke_width'],
'#states' => [
'visible' => [
':input[name="' . $form_parent . '[return_tracks]"]' => [
'checked' => TRUE,
],
],
],
];
$element['track_stroke_opacity'] = [
'#weight' => -98,
'#type' => 'number',
'#step' => 0.01,
'#title' => $this
->t('Track Opacity'),
'#description' => $this
->t('Opacity of the tracks from 1 = fully visible, 0 = complete see through.'),
'#default_value' => $settings['track_stroke_opacity'],
'#states' => [
'visible' => [
':input[name="' . $form_parent . '[return_tracks]"]' => [
'checked' => TRUE,
],
],
],
];
return $element;
}
/**
* {@inheritdoc}
*/
public function getShapesFromItem(FieldItemInterface $fieldItem) {
$settings = $this
->getSettings();
if (!$settings['return_tracks']) {
return [];
}
$gpxFile = $this
->getGpxFileFromItem($fieldItem);
if (empty($gpxFile)) {
\Drupal::logger('geolocation_gpx')
->warning('Reading file as GPX failed');
return [];
}
$positions = [];
foreach ($gpxFile->tracks as $track) {
$coordinates = '';
foreach ($track->segments as $segment) {
foreach ($segment->points as $point) {
$coordinates .= $point->latitude . ',' . $point->longitude . ' ';
}
}
$positions[] = [
'#type' => 'geolocation_map_polyline',
'#title' => $track->name,
'#coordinates' => $coordinates,
'#stroke_color' => $settings['track_stroke_color_randomize'] ? sprintf('#%06X', mt_rand(0, 0xffffff)) : $settings['track_stroke_color'],
'#stroke_width' => (int) $settings['track_stroke_width'],
'#stroke_opacity' => (double) $settings['track_stroke_opacity'],
];
}
return $positions;
}
/**
* {@inheritdoc}
*/
public function getLocationsFromItem(FieldItemInterface $fieldItem) {
$settings = $this
->getSettings();
if (!$settings['return_waypoints']) {
return [];
}
$gpxFile = $this
->getGpxFileFromItem($fieldItem);
if (empty($gpxFile)) {
\Drupal::logger('geolocation_gpx')
->warning('Reading file as GPX failed');
return [];
}
$positions = [];
foreach ($gpxFile->waypoints as $waypoint) {
$positions[] = [
'#type' => 'geolocation_map_location',
'#title' => $waypoint->name,
'#coordinates' => [
'lat' => $waypoint->latitude,
'lng' => $waypoint->longitude,
],
];
}
return $positions;
}
/**
* {@inheritdoc}
*/
public function isFieldGeoOption(FieldDefinitionInterface $fieldDefinition) {
return $fieldDefinition
->getType() == 'geolocation_gpx_file';
}
/**
* Get GPX file.
*
* @param \Drupal\Core\Field\FieldItemInterface $fieldItem
* Field item.
*
* @return \phpGPX\Models\GpxFile|false
* GPX file or false.
*/
public function getGpxFileFromItem(FieldItemInterface $fieldItem) {
if ($fieldItem instanceof GeolocationGpxFile) {
$target_id = $fieldItem
->get('target_id')
->getValue();
if (empty($target_id)) {
return FALSE;
}
$file = File::load($target_id);
if (empty($file)) {
return FALSE;
}
$filename = $file
->getFileUri();
$gpx = new phpGPX();
$file = $gpx
->load($filename);
if (empty($file)) {
return FALSE;
}
return $file;
}
return FALSE;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DataProviderBase:: |
protected | property | Entity field manager. | |
DataProviderBase:: |
protected | property | Field definition. | |
DataProviderBase:: |
protected | property | Views field. | |
DataProviderBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
DataProviderBase:: |
public | function | Token replacement support function, callback to token replacement function. | |
DataProviderBase:: |
protected | function | ||
DataProviderBase:: |
public | function |
Get locations from views row. Overrides DataProviderInterface:: |
1 |
DataProviderBase:: |
public | function |
Get positions from field item list. Overrides DataProviderInterface:: |
3 |
DataProviderBase:: |
public | function |
Get positions from views row. Overrides DataProviderInterface:: |
1 |
DataProviderBase:: |
protected | function | Add default settings. | |
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:: |
|
DataProviderBase:: |
public | function |
Constructs a new GeocoderBase object. Overrides PluginBase:: |
1 |
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 | |
GeolocationGpx:: |
protected | function |
Default settings. Overrides DataProviderBase:: |
|
GeolocationGpx:: |
public | function | Get GPX file. | |
GeolocationGpx:: |
public | function |
Get locations from field item list. Overrides DataProviderBase:: |
|
GeolocationGpx:: |
public | function |
Provide data provider settings form array. Overrides DataProviderBase:: |
|
GeolocationGpx:: |
public | function |
Get shapes from field item list. Overrides DataProviderBase:: |
|
GeolocationGpx:: |
public | function |
Determine valid field geo option. Overrides DataProviderInterface:: |
|
GeolocationGpx:: |
public | function |
Determine valid views option. Overrides DataProviderBase:: |
|
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. |