You are here

function workflow_tokens in Workflow 7.2

Same name and namespace in other branches
  1. 7 workflow.tokens.inc \workflow_tokens()

Implements hook_tokens().

N.B. Keep the following functions aligned when changing properties:

File

./workflow.tokens.inc, line 148
Tokens hooks for Workflow module.

Code

function workflow_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);
  $langcode = isset($options['language']) ? $options['language']->language : NULL;

  // The 'node' tokens have already been replaced with 'entity'.
  // Skip for easier debugging.
  // @todo: is this always the case, or only if Entity Tokens is enabled?
  if ($type == 'node' || $type == 'user' || $type == 'term') {
    return $replacements;
  }
  if ($type == 'date' && !empty($data['date'])) {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'seconds':

          // This is our custom date token in 'seconds ago' format.
          $seconds = REQUEST_TIME - $data['date'];
          $replacements[$original] = $seconds;

          // Avoid reporcessing in the remainder of this function.
          break;
      }
    }
    return $replacements;
  }
  elseif ($type == 'entity' && !empty($data['entity'])) {

    // new, chained tokens, as of version 7.x-2.3.
    $entity = $data['entity'];
    $entity_type = $data['entity_type'];

    // $token_type = $data['token_type'];
    foreach ($tokens as $name => $original) {
      if (FALSE !== strpos($name, ':')) {

        // This is a chained property (contains ':').
        $name_parts = explode(':', $name);
        switch (end($name_parts)) {
          case 'comment':
            if (isset($entity->workflow_transitions) && isset($entity->workflow_transitions[$name_parts[0]])) {
              $replacements[$original] = $entity->workflow_transitions[$name_parts[0]]->{$name_parts[1]};
            }
            break;
        }
      }
      elseif (isset($data['workflow_token_type'])) {

        // This part taken from entity_token_tokens().
        $wrapper = entity_metadata_wrapper($data['workflow_token_type'], $data[$data['workflow_token_type']]);
        $property_name = str_replace('-', '_', $name);
        try {
          if ($name == 'workflow' || $name == 'states' || $name == 'transitions') {
            $replacement = _workflow_token_get_token($wrapper->{$property_name}, $options);
          }
          else {
            $replacement = _entity_token_get_token($wrapper->{$property_name}, $options);
          }
          if (isset($replacement)) {
            $replacements[$original] = $replacement;
          }
        } catch (EntityMetadataWrapperException $e) {

          // dpm('token not found: ' . $name);
          // If tokens for not existing values are requested, just do nothing.
        }
      }
    }

    // If this is a Last Transition, get subtokens for it.
    if ($sub_tokens = token_find_with_prefix($tokens, 'last-transition')) {

      // Get the workflow tokens from the transition of this entity.
      $transition = _workflow_tokens_get_transition($entity_type, $entity, NULL);
      $sub_data = array(
        'WorkflowTransition' => $transition,
        'workflow_token_type' => 'WorkflowTransition',
      );
      $sub_data += $data;
      $replacements += token_generate('entity', $sub_tokens, $sub_data, $options);
    }
    if (isset($data['WorkflowTransition'])) {

      // If this is a WorkflowField, get subtokens for it.
      $name_parts = explode(':', $name, 2);
      $field_name = reset($name_parts);
      if ($field_name != 'created' && isset($entity->{$field_name}) && ($sub_tokens = token_find_with_prefix($tokens, $field_name))) {
        $transition = _workflow_tokens_get_transition($entity_type, $entity, $field_name);
        $sub_data = array(
          'WorkflowTransition' => $transition,
          'workflow_token_type' => 'WorkflowTransition',
        );
        $sub_data += $data;
        $replacements += token_generate('entity', $sub_tokens, $sub_data, $options);
      }
      $transition = $data['WorkflowTransition'];
      $field_name = $transition->field_name;
      if ($sub_tokens = token_find_with_prefix($tokens, 'Workflow')) {
        $sub_data = array(
          'Workflow' => $transition
            ->getWorkflow(),
          'workflow_token_type' => 'Workflow',
        );
        $sub_data += $data;
        $replacements += token_generate('entity', $sub_tokens, $sub_data, $options);
      }

      // Unify to old-state, new-state.
      // Do not use underscores, or workflow_tokens will not work!
      if ($sub_tokens = token_find_with_prefix($tokens, 'old-state')) {
        $sub_data = array(
          'WorkflowState' => $transition
            ->getOldState(),
          'workflow_token_type' => 'WorkflowState',
        );
        $sub_data += $data;
        $replacements += token_generate('entity', $sub_tokens, $sub_data, $options);
      }
      if ($sub_tokens = token_find_with_prefix($tokens, 'new-state')) {
        $sub_data = array(
          'WorkflowState' => $transition
            ->getNewState(),
          'workflow_token_type' => 'WorkflowState',
        );
        $sub_data += $data;
        $replacements += token_generate('entity', $sub_tokens, $sub_data, $options);
      }
      if ($sub_tokens = token_find_with_prefix($tokens, 'user')) {
        if (isset($data['WorkflowTransition'])) {
          $sub_entity = $data['WorkflowTransition'];
        }
        $sub_data = array(
          'entity_type' => 'user',
          'user' => user_load($sub_entity->uid),
          'token_type' => 'user',
        );
        $replacements += token_generate('user', $sub_tokens, $sub_data, $options);
      }
      if ($sub_tokens = token_find_with_prefix($tokens, 'created')) {
        $sub_data = array(
          'entity_type' => 'date',
          'date' => $data['WorkflowTransition']->stamp,
          'token_type' => 'date',
        );
        $replacements += token_generate('date', $sub_tokens, $sub_data, $options);
      }
    }
  }
  return $replacements;
}