Page callback information, with objects provided by krumo if available

  • <?php
    /**
     * Process variables for show page callback theme function.
     */
    function template_preprocess_xray_show_page_callback(&$variables) {
      if (
    $variables['page_arguments']) {
       
    // If Krumo is available we'll print any array or object arguments.
       
    $if_krumo_see_below = function_exists('krumo_ob') ? ' ' . t('(see below)') : '';
       
    $lumps = array();
        foreach (
    $variables['page_arguments'] as $key => $value) {
         
    // Arrays and objects can't be easily printed in a message, so instead
          // identify what they are.
         
    $value = xray_flatten_argument($value, $lumps, $if_krumo_see_below);
         
    // Sanitize for security and add emphasis to each argument.
         
    $variables['page_arguments'][$key] = drupal_placeholder($value);
        }
        if (
    $if_krumo_see_below) {
          foreach (
    $lumps as $lump) {
           
    $variables['extra'] .= krumo_ob($lump);
          }
        }
      }
    }

    /**
     * Theme the page callback and optionally other elements of a router item.
     *
     * @param $variables
     *   An associative array, typically generated by menu_get_item(), containing:
     *   - page_callback: The function called to display a web page.
     *   - page_arguments: (optional) An array of arguments passed to the page
     *     callback function.
     *   - include_file: (optional) A file included before the page callback is
     *     called; this allows required code to be in a separate file.
     *
     * @see template_preprocess_xray_show_page_callback()
     *
     * @ingroup themeable
     */
    function theme_xray_show_page_callback($variables) {
     
    extract($variables, EXTR_SKIP);

     

    $output = '';
     
    $output .= '<p class="xray-help xray-page-callback">';
     
    $output .= t('This page is brought to you by ');
      if (
    $page_arguments) {
       
    $output .= format_plural(count($page_arguments),
         
    'the argument !arg handed to ',
         
    'the arguments !arg handed to ',
          array(
    '!arg' => xray_oxford_comma_list($page_arguments))
        );
      }
     
    $output .= t('the function %func',
                   array(
    '%func' => $page_callback . '()'));
      if (
    $include_file) {
       
    $output .= t(' and the included file %file',
                     array(
    '%file' => $include_file));
      }
     
    $output .= '.</p>';
     
    $output .= $extra;
      return
    $output;
    }

    /**
     * Flattens an object or array variable to a string describing the variable.
     *
     * The flattened version is simply 'an array with N elements' or 'an object
     * with N properties'.
     *
     * @param $value
     *   The value to be flattened (if necessary).
     *
     * @param $lumps
     *   An array, passed by reference, to which original, non-flattened objects
     *   and arrays will be added.
     *
     * @param $extra_text
     *   (optional) A string of text to add onto flattened values.
     *
     * @return
     *   A string describing an object or array, or the original value if not an
     *   object or array.
     */
    function xray_flatten_argument($value, &$lumps = array(), $extra_text = '') {
      if (!
    is_array($value) && !is_object($value)) {
       
    // It's already flat.
       
    return $value;
      }
     
    $lumps[] = $value;
     
    // Count elements or properties (object cast to array or count returns 1).
     
    $count = count((array) $value);
     
    $output = '';
      if (
    is_array($value)) {
       
    // It's an array.
       
    $output .= format_plural($count, 'an array with one element', 'an array with @count elements');
      }
      else {
       
    // It's an object.
       
    $output .= format_plural($count, 'an object with one property', 'an object with @count properties');
      }
     
    $output .= $extra_text;
      return
    $output;
    }

    /**
     * Implements hook_theme().
     */
    function xray_theme() {
      return array(
       
    'xray_show_page_callback' => array(
         
    'variables' => array(
           
    'page_callback' => NULL,
           
    'include_file' => NULL,
           
    'page_arguments' => NULL,
           
    'extra' => '',
          ),
        ),
    // ...
     
    );
    }

    /**
     * Implements hook_help().
     */
    function xray_help($path, $arg) {
     
    $help = '';
     
    // Display in a help message the function which provides the current page.
     
    $help .= xray_show_page_callback();
      switch (
    $path) {
       
    // Summaries for main administrative sections.
       
    case 'admin/content':
         
    $variables = array('data' => xray_content_summary());
          return
    $help . theme('xray_content_summary__help', $variables);
    // [additional not relevant code not shown]...
        // The main help page for the module itself.
       
    case 'admin/help#xray':
         
    // This is shown as a regular page; do not include the
          // xray_show_page_callback $help or it is shown twice.
         
    return _xray_help_page();
        default:
          return
    $help;
      }
    }
    ?>