You are here

public static function TextimageImager::processTokens in Textimage 7.3

Textimage tokens replacement.

Parameters

string $key: The Textimage token key within the main token [textimage:key:...]. Key can take 'uri' or 'url' values.

array $tokens: The tokens to resolve.

object $node: The node for which to resolve the tokens.

Return value

array An array of token replacements.

1 call to TextimageImager::processTokens()
textimage_tokens in ./textimage.module
Implements hook_tokens().

File

classes/TextimageImager.inc, line 285
Textimage - Image processor class.

Class

TextimageImager
Textimage - Image processor class.

Code

public static function processTokens($key, $tokens, $node) {

  // Need to avoid endless loops, that would occur if there are
  // circular references in the tokens. Set static variables for
  // the nesting level and the stack of fields accessed so far.
  static $nesting_level;
  static $field_stack;
  if (!isset($nesting_level)) {
    $nesting_level = 0;
    $field_stack = array();
  }
  else {
    $nesting_level++;
  }

  // Get tokens specific for the required key.
  $sub_tokens = token_find_with_prefix($tokens, $key);

  // Return immediately if none, or no node.
  if (empty($sub_tokens) || !$node) {
    self::rollbackStack($nesting_level, $field_stack);
    return array();
  }

  // Determine the callback function.
  switch ($key) {
    case 'uri':
      $callback_method = 'getImageUri';
      break;
    case 'url':
      $callback_method = 'getImageUrl';
      break;
  }

  // Loops through the tokens to resolve.
  $replacements = array();
  foreach ($sub_tokens as $sub_token => $original) {

    // Clear current nesting level field stack.
    unset($field_stack[$nesting_level]);

    // Get token elements.
    $sub_token_array = explode(':', $sub_token);

    // Get requested field name, continue if missing.
    $field_name = isset($sub_token_array[0]) ? $sub_token_array[0] : NULL;
    if (!$field_name) {
      continue;
    }

    // Check for recursion, i.e. the field is already engaged in a
    // token resolution. Throw a TextimageImagerTokenException in case.
    if (in_array($field_name, $field_stack)) {
      self::rollbackStack($nesting_level, $field_stack);
      throw new TextimageImagerTokenException($original);
    }

    // Set current requested field in the field stack.
    $field_stack[$nesting_level] = $field_name;

    // Get requested display mode, default to 'default'.
    $display_mode = isset($sub_token_array[1]) ? $sub_token_array[1] : 'default';

    // Get requested sequence, default to NULL.
    $index = isset($sub_token_array[2]) ? $sub_token_array[2] : NULL;

    // Get general field info, continue if missing.
    $field_info = field_info_field($field_name);
    if (!$field_info) {
      continue;
    }

    // Get node (bundle) dependent field info, continue if missing.
    $node_type = $node->type;
    $instance_info = field_info_instance('node', $field_name, $node_type);
    if (!$instance_info) {
      continue;
    }

    // Get info on module providing formatting, continue if missing.
    $display_module = isset($instance_info['display'][$display_mode]['module']) ? $instance_info['display'][$display_mode]['module'] : NULL;
    if (!$display_module) {
      continue;
    }

    // At this point, if Textimage is providing field formatting for the
    // current field, we can proceed accessing the data needed to resolve
    // the token.
    if ($display_module == 'textimage') {

      // Get the image style used for the field formatting.
      $image_style = isset($instance_info['display'][$display_mode]['settings']['image_style']) ? $instance_info['display'][$display_mode]['settings']['image_style'] : NULL;
      if (!$image_style) {
        continue;
      }

      // Get the field items.
      $items = field_get_items('node', $node, $field_name);

      // Invoke Textimage API functions to return the token value requested.
      if ($field_info['module'] == 'text') {

        // Text field. Get sanitized text items and return a single image.
        $text = TextimageImager::getTextFieldText($items, $field_info, $instance_info, $node);
        try {
          $replacements[$original] = call_user_func(array(
            'TextimageImager',
            $callback_method,
          ), $image_style, NULL, $text, 'png', TRUE, $node);
        } catch (TextimageImagerTokenException $e) {

          // Callback ended up in circular loop, mark the failing token.
          $replacements[$original] = str_replace('textimage', 'void-textimage', $original);
          if ($nesting_level > 0) {

            // Returns up in the nesting of iteration with the failing token.
            self::rollbackStack($nesting_level, $field_stack);
            throw new TextimageImagerTokenException($e
              ->getToken());
          }
          else {

            // Inform about the token failure.
            $msg = t("Textimage token @token in node '@node_title' can not be resolved (circular reference). Remove the token to avoid this message.", array(
              '@token' => $original,
              '@node_title' => $node->title,
            ));
            _textimage_diag($msg, WATCHDOG_WARNING);
          }
        }
      }
      elseif ($field_info['module'] == 'image') {

        // Image field. Get a separate Textimage from each of the images
        // in the field.
        try {
          $ret = array();
          foreach ($items as $item) {

            // Get source image from the image field item.
            $source_image_file = file_load($item['fid']);
            $ret[] = call_user_func(array(
              'TextimageImager',
              $callback_method,
            ), $image_style, NULL, NULL, 'png', TRUE, $node, $source_image_file);
          }

          // Return a single URI/URL if requested, or a comma separated
          // list of all the URIs/URLs generated.
          if (!is_null($index) && isset($ret[$index])) {
            $replacements[$original] = $ret[$index];
          }
          else {
            $replacements[$original] = implode(',', $ret);
          }
        } catch (TextimageImagerTokenException $e) {

          // Callback ended up in circular loop, mark the failing token.
          $replacements[$original] = str_replace('textimage', 'void-textimage', $original);
          if ($nesting_level > 0) {

            // Returns up in the nesting of iteration with the failing token.
            self::rollbackStack($nesting_level, $field_stack);
            throw new TextimageImagerTokenException($e
              ->getToken());
          }
          else {

            // Inform about the token failure.
            $msg = t("Textimage token @token in node '@node_title' can not be resolved (circular reference). Remove the token to avoid this message.", array(
              '@token' => $original,
              '@node_title' => $node->title,
            ));
            _textimage_diag($msg, WATCHDOG_WARNING);
          }
        }
      }
    }
  }

  // Return to previous iteration.
  self::rollbackStack($nesting_level, $field_stack);
  return $replacements;
}