You are here

class GeolocationGpx in Geolocation Field 8.3

Provides GPX.

Plugin annotation


@DataProvider(
  id = "geolocation_gpx",
  name = @Translation("Geolocation GPX"),
  description = @Translation("Tracks & Waypoints."),
)

Hierarchy

Expanded class hierarchy of GeolocationGpx

File

modules/geolocation_gpx/src/Plugin/geolocation/DataProvider/GeolocationGpx.php, line 24

Namespace

Drupal\geolocation_gpx\Plugin\geolocation\DataProvider
View 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

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::getFieldItemsFromViewsRow protected function
DataProviderBase::getLocationsFromViewsRow public function Get locations from views row. Overrides DataProviderInterface::getLocationsFromViewsRow 1
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::getSettings protected function Add default settings.
DataProviderBase::getShapesFromViewsRow public function Get shapes from views row. Overrides DataProviderInterface::getShapesFromViewsRow 1
DataProviderBase::getTokenHelp public function Return field item tokens. Overrides DataProviderInterface::getTokenHelp 1
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
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
GeolocationGpx::defaultSettings protected function Default settings. Overrides DataProviderBase::defaultSettings
GeolocationGpx::getGpxFileFromItem public function Get GPX file.
GeolocationGpx::getLocationsFromItem public function Get locations from field item list. Overrides DataProviderBase::getLocationsFromItem
GeolocationGpx::getSettingsForm public function Provide data provider settings form array. Overrides DataProviderBase::getSettingsForm
GeolocationGpx::getShapesFromItem public function Get shapes from field item list. Overrides DataProviderBase::getShapesFromItem
GeolocationGpx::isFieldGeoOption public function Determine valid field geo option. Overrides DataProviderInterface::isFieldGeoOption
GeolocationGpx::isViewsGeoOption public function Determine valid views option. Overrides DataProviderBase::isViewsGeoOption
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.