You are here

function apigee_edge_actions_tokens in Apigee Edge 8

Implements hook_tokens().

File

modules/apigee_edge_actions/apigee_edge_actions.tokens.inc, line 78
Implements tokens for Apigee Edge entities.

Code

function apigee_edge_actions_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  if ($type == 'entity' && !empty($data['entity_type']) && !empty($data['entity']) && !empty($data['token_type'])) {

    /* @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $data['entity'];
    if ($entity instanceof EdgeEntityInterface) {
      foreach ($tokens as $field_name => $original) {

        // Ensure entity has requested field and is not empty.
        if (!$entity
          ->hasField($field_name) || $entity
          ->get($field_name)
          ->isEmpty()) {
          continue;
        }
        $replacements[$original] = $entity
          ->get($field_name)->value;
      }
    }
  }
  return $replacements;
}