dprint_rx

custom/devel_helper/devel_helper.module, line 44

Versions
6
dprint_rx($value, $expand = 1, $return = FALSE)

Print a variable in a friendly and usable format to the page.

Parameters

$value Value to be displayed

$expand Number of levels to be initially expanded

$return TRUE will return the output and FALSE will print the output

▾ 2 functions call dprint_rx()

dprx in custom/devel_helper/devel_helper.module
Shortcut alias for dprint_rx().
dvmx in custom/devel_helper/devel_helper.module
Print a variable in a friendly and usable format to the 'message' area of the page. Uses drupal_set_message()

Code

<?php
function dprint_rx($value, $expand = 1, $return = FALSE) {
  // This recursion limit could be set from a settings page but 9 seems like a reasonable level.
  $recursion_limit = 9;

  // Get type
  $type = gettype($value);

  // Cast object as array
  $value = (array)$value;

  $output = '';
  if ($type == 'array' || $type == 'object') { // Recurse

    // Get path
    $base_path = base_path();
    $path = drupal_get_path('module', 'devel_helper');
    // Load css and js the drupal way but if the page is not fully rendered the css and js is added below.
    drupal_add_css($path .'/dprx.css', 'module');
    drupal_add_js($path .'/dprx.js', 'module');

    // CSS and JS will be directly embedded to insure they are loaded even if the output is not rendered inside a drupal template.
    // This code would only be loaded when someone calls dprint_rx(); and then exit(); 
    // or they call dprx() within the templatepage.tpl.php which has already loaded the css and js.
    // NOTE: This approach is not valid xhtml but it allows the needed css and js to remain in external files.
    
    
    // Check for jQuery and Drupal
    $output .= '<script type="text/javascript">';
    $output .= 'if(!window.jQuery){document.write(\'<\' + \'script type="text/javascript" src="'. $base_path .'misc/jquery.js"></\' + \'script>\');}';
    $output .= 'if(!window.Drupal){document.write(\'<\' + \'script type="text/javascript" src="'. $base_path .'misc/drupal.js"></\' + \'script>\');}';
    $output .= '</script>';
    
    // Check for initDevelHelper behavior. If it is not loaded assume the css is missing
    $output .= '<script type="text/javascript">';
    $output .= 'if(!Drupal.behaviors.initDevelHelper){';
    $output .= 'document.write(\'<link type="text/css" rel="stylesheet" media="all" href="'. $base_path . $path .'/dprx.css" />\');';
    $output .= 'document.write(\'<\' + \'script type="text/javascript" src="'. $base_path . $path .'/dprx.js"></\' + \'script>\');';
    $output .= '}</script>';

    // Look for a variable names, beginning with $, in the value's keys then output them separately.
    $keys = array_keys($value);
    foreach ($keys as $arr_key) {
      if (strpos($arr_key, '$') === 0) {
        $output .= '<div class="dprx">';
        $output .= _dprint_rx_recurse($value[$arr_key], $arr_key, $expand, 0, $recursion_limit);
        $output .= '</div>';
        // Remove the value
        unset($value[$arr_key]);
      }
    }

    // If $value still contains items then output the data.
    if (count($value)) {
      $output .= '<div class="dprx">';
      $output .= _dprint_rx_recurse($value, FALSE, $expand, 0, $recursion_limit);
      $output .= '</div>';
    }

  }
  else { // A single item
    $output .= '<pre class="dprx">'. var_export($value, TRUE) .'</pre>';
  }

  if ($return) {
    return $output;
  }
  else {
    print($output);
  }
}
?>