You are here

function contemplate_array_variables in Content Templates (Contemplate) 5

Same name and namespace in other branches
  1. 6 contemplate.module \contemplate_array_variables()
  2. 7 contemplate.module \contemplate_array_variables()

Recursive (and therefore magical) function goes through node object returns html representation of the node strings are clickable and insert into teaser/body fields

Parameters

$array: array to recurse through

$target: target field for javascript insert

$parents: used by recursion

$object: used by recursion

Return value

unknown

1 call to contemplate_array_variables()
contemplate_examples in ./contemplate.module
Load an example node and display its parts

File

./contemplate.module, line 631
Create templates to customize teaser and body content.

Code

function contemplate_array_variables($array, $target, $parents = FALSE, $object = FALSE, $depth = 0, &$recursion_limit_hit) {
  global $contemplate_fids;
  if ($depth >= variable_get('contemplate_max_recursion_depth', 10)) {

    #drupal_set_message(t('Contemplate exceeded max recursion depth. There are more variables available that have not been listed.'), 'error');
    $recursion_limit_hit++;
    return '';
  }

  //
  //  if $array['#node'] exists we need to remove it to prevent recursion and PHP Fatal errors on Memory Limit
  //
  if (isset($array['#node'])) {
    unset($array['#node']);
  }
  if (is_object($array)) {
    $array = (array) $array;
  }
  if (is_array($array)) {
    $output .= "<dl>\n";
    foreach ($array as $field => $value) {
      if ($parents) {
        if ($object) {
          $field = $parents . '->' . $field;
        }
        else {
          if (is_int($field)) {
            $field = $parents . '[' . $field . ']';
          }
          else {
            if ($field == 'fid') {

              // make a note of the fields named "fid"
              $contemplate_fids[] = "\$node->" . $parents . '[\'' . $field . '\']';
            }
            $field = $parents . '[\'' . $field . '\']';
          }
        }
      }
      $type = "";
      if (!is_string($value)) {
        $type = " (" . gettype($value) . ")";
      }
      if (!is_array($value) && !is_object($value)) {
        if ($field == 'title' || substr($field, -9) == "['value']") {
          $security = t(" <span style='color:red;font-weight:bold'>**</span>");
          $insert = "'<?php print check_plain(\$node->" . addslashes($field) . ") ?>'";
        }
        else {
          $security = '';
          $insert = "'<?php print \$node->" . addslashes($field) . " ?>'";
        }
        $output .= "<dt><a href=\"#\" onclick=\"insertAtCursor(document.getElementById('edit-{$target}'), {$insert});return false;\" title=\"" . t('insert this variable into') . " {$target}\">\$node->{$field}</a>{$security}{$type}</dt>\n";
      }
      else {
        $output .= "<dt>\$node->{$field}{$type}</dt>\n";
      }
      $output .= "<dd>\n";
      if (is_array($value)) {
        $output .= contemplate_array_variables($value, $target, $field, FALSE, $depth + 1, $recursion_limit_hit);
      }
      elseif (is_object($value)) {
        $output .= contemplate_array_variables((array) $value, $target, $field, TRUE, $depth + 1, $recursion_limit_hit);
      }
      else {
        $value = is_bool($value) ? $value ? 'TRUE' : 'FALSE' : $value;
        $output .= htmlspecialchars(print_r($value, TRUE)) . "\n";
      }
      $output .= "</dd>\n";
    }
    $output .= "</dl>\n";
  }
  return $output;
}