You are here

function contemplate_array_variables in Content Templates (Contemplate) 6

Same name and namespace in other branches
  1. 5 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

string - html dictionary list of $node elements

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

File

./contemplate.module, line 691
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 '';
  }

  /**
   * CCK fields are causing problems, they are added into the original array as $array['field_*']=>array();
   * and they are also added into $array['content']['field_*']=>array() this array has not only
   * the information for the CCK field, but also information for the whole node , even itself,
   * $array['content']['field_*']['#node'] recursively, this leads to memory issues while traversing
   * the array recursively
   * Drupal 6
   */
  if (isset($array['content']) && $parents == FALSE && $object == FALSE) {

    // by default we will remove the content array, the user can choose to have this not done
    if (variable_get('contemplate_remove_node_content', FALSE)) {
      unset($array['content']);
    }
  }

  //  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']);
    $array['#node'] = "\n ** RECURSION **";
  }
  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' || drupal_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=\"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;
}