You are here

public function GeolocationFieldProvider::geolocationItemTokens in Geolocation Field 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/geolocation/DataProvider/GeolocationFieldProvider.php \Drupal\geolocation\Plugin\geolocation\DataProvider\GeolocationFieldProvider::geolocationItemTokens()

File

src/Plugin/geolocation/DataProvider/GeolocationFieldProvider.php, line 68

Class

GeolocationFieldProvider
Provides default geolocation field.

Namespace

Drupal\geolocation\Plugin\geolocation\DataProvider

Code

public function geolocationItemTokens(array &$replacements, array $data, array $options) {
  if (isset($data['geolocation_current_item'])) {

    /** @var \Drupal\geolocation\Plugin\Field\FieldType\GeolocationItem $item */
    $item = $data['geolocation_current_item'];
    $replacements['[geolocation_current_item:lat_sex]'] = GeolocationItem::decimalToSexagesimal($item
      ->get('lat')
      ->getValue());
    $replacements['[geolocation_current_item:lng_sex]'] = GeolocationItem::decimalToSexagesimal($item
      ->get('lng')
      ->getValue());

    // Handle data tokens.
    $metadata = $item
      ->get('data')
      ->getValue();
    if (is_array($metadata) || $metadata instanceof \Traversable) {
      foreach ($metadata as $key => $value) {
        try {

          // Maybe there is values inside the values.
          if (is_array($value) || $value instanceof \Traversable) {
            foreach ($value as $deepkey => $deepvalue) {
              if (is_string($deepvalue)) {
                $replacements['[geolocation_current_item:data:' . $key . ':' . $deepkey . ']'] = (string) $deepvalue;
              }
            }
          }
          else {
            $replacements['[geolocation_current_item:data:' . $key . ']'] = (string) $value;
          }
        } catch (\Exception $e) {
          watchdog_exception('geolocation', $e);
        }
      }
    }
  }
}