0

I'm just working through an example module in DGD7. The module reports statistics about the themes. It displays this information in the help region on the appearance admin page.

I can't get it to display and I'm wondering if I understand the theming process correctly.

My understanding is this:

  1. I call my function. (with a function)
  2. My function returns data.
  3. I tell drupal about my theme function with hook_theme
  4. My theme function then turns my data into HTML.

Here's the module:

It uses a switch to call the function based on the path:

function xray_help($path, $arg) { 
 switch ($path) {
  case 'admin/appearance':
   return xray_appearance_summary();
 }
}

Then this function calls data from a stats function which returns a compact. (the stats function works - tested with dpm())

function xray_appearance_summary() {
  $data = array();
  $data['themes'] = xray_stats_enabled_themes();
  return $data;
}

They give a theme function (I'll put it last because it's quite long) but don't show how to call it with hook_theme. I tried using this hook_theme to call the theme function:

function xray_theme() {
  return array(
    'appearance_summary' => array(
      'variables' => array(
        'themes' => NULL,
        'attr' => NULL,
      ),
    ),
  );
}

The theme function takes two variables $themes & $attr so I put both of those in. But I get the error.

Notice: Array to string conversion in menu_get_active_help() (line 1722 of C:\XAMPP\htdocs\drupal\includes\menu.inc).

Am I misunderstanding hook_theme?

function theme_xray_appearance_summary($variables) {
  $themes = $variables['data']['themes'];
  // Collapse attributes for paragraph tag to a string.
  $attr = drupal_attributes($variables['attributes']);

$output = '';
  $output .= "<p $attr>";
  $output .= t('This site');
  $output .= ' ';
  $output .= format_plural(
    $themes['num_themes'] - $themes['num_hidden'],
    'has only one theme available',
    'has @count themes available'
  );
  $output .= ' ';
  $output .= format_plural(
    $themes['num_hidden'],
    'plus one hidden theme.',
    'plus @count hidden themes.'
  );
  $output .= ' ';
  $output .= t('Of the non-hidden themes,');
  $output .= ' ';
  $output .= format_plural(
    $themes['num_enabled'],
    'one is enabled.  The enabled theme',
    '@count are enabled.  In the enabled themes,'
  );
  $output .= ' ';
  $summaries = array();
  foreach ($themes['summaries'] as $name => $info) {
    $summary = '';
    $summary .= $name . ' ';
    $summary .= format_plural(
      $info['regions'],
      'has one region',
      'has @count regions'
    );
    if ($info['overlay_regions'] || $info['regions_hidden']) {
      $summary .= ' ' . t('(including') . ' ';
      $summary .= format_plural(
        $info['overlay_regions'],
        'one overlay region',
        '@count overlay regions'
      );
      $summary .= ' ' . t('and') . ' ';
      $summary .= format_plural(
        $info['regions_hidden'],
        'one hidden region',
        '@count hidden regions'
      );
      $summary .= ')';
    }
    $summaries[] = $summary;
  }
  $output .= xray_oxford_comma_list($summaries);
  $output .= '.';
  $output .= '</p>';
  return $output;
}

EDIT ONE.

I've got rid of the error by changing the switch; thanks to Yuriy, but nothing still displays:

 case 'admin/appearance':
      $variables = array('data' => xray_appearance_summary());
      return theme('xray_appearance_summary', $variables);
1

First of all, your theme_xray_appearance_summary should be theme_appearance_summary, and should take $variables by reference: &$variables.

Second of all, hook_help() expects you to return a string, but your implementation returns the data from xray_appearance_summary(), which returns an array, thus the error you're getting.

  • I based some of my syntax off the theming example module. It uses theme_theming_example_content_array($variables) as a theme function. Why would that syntax not work for mine? – Dominic Woodman Jul 29 '13 at 19:04
  • Separated into two comments for clarity. On the second point I thought that the theme function takes $data from x_ray_appearance_summary and turns it into a the string (held in $output) which is then passed back. Otherwise how is the theming function involved? – Dominic Woodman Jul 29 '13 at 19:10
0

Ok so I had two problems in the end. The first which I put in my edit above, was I needed to turn $data (an array) into a string. This was done by using the theme function to send the $data to the theme which then themes it and sends it back as a string.

case 'admin/appearance':
     $variables = array('data' => xray_appearance_summary());
     return theme('xray_appearance_summary', $variables);

The second was my hook_theme. It's receiving $data from xray_summary_appearance. Not $themes from the theme function.

function xray_theme() {
  return array(
    'xray_appearance_summary' => array(
      'variables' => array(
        'data' => array(),
        'attributes' => array('class' => 'xray-help'),
        ),
      ),
    );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.