You are here

function token_custom_token_render in Custom Tokens 7.2

Evaluates a string using the given token.

Parameters

string $machine_name: The machine_name of the custom token being used.

string $code: A string containing the code to evaluate.

array $data: The $data array passed on to token_replace().

array $options: The $options array passed on to token_replace().

Return value

string A string containing the printed output of the code, followed by the returned output of the code.

1 call to token_custom_token_render()
token_custom_tokens in ./token_custom.module
Implements hook_tokens().

File

./token_custom.module, line 476
It gives the user the ability to create custom tokens using PHP code for specific replacements that can improve other modules relying on the token Drupal 7 core API.

Code

function token_custom_token_render($machine_name, $code, $data, $options) {

  // Load the custom token.
  $token = token_custom_load($machine_name);

  // Determine which format to use.
  $format = isset($token->format) ? $token->format : NULL;

  /*
   * If it's a php_filter token, then pass the $data variable
   * to the evaluated token.
   */
  if ($format == 'php_code') {

    // Generate a unique static key.
    $static_count =& drupal_static('token_custom_data:counter', 0);
    $static_key = 'token_custom_data:' . $static_count;
    $static_count++;

    /*
     * Store value in static cache and leave it there.
     */
    drupal_static($static_key, array(
      'data' => $data,
      'options' => $options,
    ));

    // Add static data to the evaluated code.
    $code = '
<?php
  if ($static = drupal_static(\'' . $static_key . '\')) {
    extract($static);
  } ?>' . $code;
    $output = check_markup($code, $format);

    // Clear static variable to potentially free memory.
    drupal_static_reset($static_key);
    return $output;
  }
  else {

    // Format the output using the format.
    return check_markup($code, $format);
  }
}