You are here

function webform_charts_tokens in Webform Charts 7

Implements hook_tokens().

File

./webform_charts.tokens.inc, line 24
Builds chart tokens for Webform nodes.

Code

function webform_charts_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);

  // Webform tokens (caching globally).
  if ($type !== 'node' || empty($data['node']) || empty($data['node']->webform) || empty($data['node']->webform['components'])) {
    return;
  }

  // Prepare all the submission data that we will likely need more than once.
  $node = $data['node'];
  $format = $sanitize ? 'html' : 'text';

  // Replace individual tokens that have an exact replacement.
  foreach ($tokens as $name => $original) {
    foreach ($node->webform['components'] as $cid => $component) {

      // Build the list of parents for this component.
      $parents = $component['pid'] == 0 ? array(
        $component['form_key'],
      ) : webform_component_parent_keys($node, $component);
      $parent_token = 'webform-charts:' . implode(':', $parents);
      if (strpos($name, $parent_token) === 0) {
        if ($chart_renderable = webform_charts_component_chart($node, $component)) {
          $replacements[$original] = drupal_render($chart_renderable);
        }
      }
    }
  }
  return $replacements;
}