You are here

function _devel_print_object in Devel 6

Same name and namespace in other branches
  1. 7 devel.module \_devel_print_object()

Recursive (and therefore magical) function goes through an array or object and returns a nicely formatted listing of its contents.

@todo currently there are problems sending an array with a varname

Parameters

$obj: array or object to recurse through

$prefix: prefix for the output items (example "$node->", "$user->", "$")

$parents: used by recursion

$object: used by recursion

Return value

fomatted html

1 call to _devel_print_object()
devel_print_object in ./devel.module
Display an object or array

File

./devel.module, line 1579

Code

function _devel_print_object($obj, $prefix = NULL, $parents = NULL, $object = FALSE) {
  static $root_type, $out_format;

  // TODO: support objects with references. See http://drupal.org/node/234581.
  if (isset($obj->view)) {
    return;
  }
  if (!isset($root_type)) {
    $root_type = gettype($obj);
    if ($root_type == 'object') {
      $object = TRUE;
    }
  }
  if (is_object($obj)) {
    $obj = (array) $obj;
  }
  if (is_array($obj)) {
    $output = "<dl>\n";
    foreach ($obj as $field => $value) {
      if ($field == 'devel_flag_reference') {
        continue;
      }
      if (!is_null($parents)) {
        if ($object) {
          $field = $parents . '->' . $field;
        }
        else {
          if (is_int($field)) {
            $field = $parents . '[' . $field . ']';
          }
          else {
            $field = $parents . '[\'' . $field . '\']';
          }
        }
      }
      $type = gettype($value);
      $show_summary = TRUE;
      $summary = NULL;
      if ($show_summary) {
        switch ($type) {
          case 'string':
          case 'float':
          case 'integer':
            if (strlen($value) == 0) {
              $summary = t("{empty}");
            }
            elseif (strlen($value) < 40) {
              $summary = htmlspecialchars($value);
            }
            else {
              $summary = format_plural(drupal_strlen($value), '1 character', '@count characters');
            }
            break;
          case 'array':
          case 'object':
            $summary = format_plural(count((array) $value), '1 element', '@count elements');
            break;
          case 'boolean':
            $summary = $value ? t('TRUE') : t('FALSE');
            break;
        }
      }
      if (!is_null($summary)) {
        $typesum = '(' . $type . ', <em>' . $summary . '</em>)';
      }
      else {
        $typesum = '(' . $type . ')';
      }
      $output .= '<span class="devel-attr">';
      $output .= "<dt><span class=\"field\">{$prefix}{$field}</span> {$typesum}</dt>\n";
      $output .= "<dd>\n";

      // Check for references.
      if (is_array($value) && isset($value['devel_flag_reference'])) {
        $value['devel_flag_reference'] = TRUE;
      }

      // Check for references to prevent errors from recursions.
      if (is_array($value) && isset($value['devel_flag_reference']) && !$value['devel_flag_reference']) {
        $value['devel_flag_reference'] = FALSE;
        $output .= _devel_print_object($value, $prefix, $field);
      }
      elseif (is_object($value)) {
        $value->devel_flag_reference = FALSE;
        $output .= _devel_print_object((array) $value, $prefix, $field, TRUE);
      }
      else {
        $value = is_bool($value) ? $value ? 'TRUE' : 'FALSE' : $value;
        $output .= htmlspecialchars(print_r($value, TRUE)) . "\n";
      }
      $output .= "</dd></span>\n";
    }
    $output .= "</dl>\n";
  }
  return $output;
}