You are here

public function GeolocationItemTokenTrait::geolocationItemTokens in Geolocation Field 8

Token replacement support function, callback to token replacement function.

Parameters

array $replacements: An associative array variable containing mappings from token names to values (for use with strtr()).

array $data: Current item replacements.

array $options: A keyed array of settings and flags to control the token replacement process. See \Drupal\Core\Utility\Token::replace().

File

src/GeolocationItemTokenTrait.php, line 123

Class

GeolocationItemTokenTrait
Class GeolocationItemTokenTrait - Provide Token for geolocation items.

Namespace

Drupal\geolocation

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]'] = $item
      ->get('lat')
      ->getValue();
    $replacements['[geolocation_current_item:lat_sex]'] = GeolocationCore::decimalToSexagesimal($item
      ->get('lat')
      ->getValue());
    $replacements['[geolocation_current_item:lng]'] = $item
      ->get('lng')
      ->getValue();
    $replacements['[geolocation_current_item:lng_sex]'] = GeolocationCore::decimalToSexagesimal($item
      ->get('lng')
      ->getValue());
    $replacements['[geolocation_current_item:lat_sin]'] = $item
      ->get('lat_sin')
      ->getValue();
    $replacements['[geolocation_current_item:lat_cos]'] = $item
      ->get('lat_cos')
      ->getValue();
    $replacements['[geolocation_current_item:lng_rad]'] = $item
      ->get('lng_rad')
      ->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);
        }
      }
    }
  }
}