1

I have Drupal version 7.41 and I am using xubuntu 14.04 as my OS.

I have a module that displays footer links as a list. The module will fetch available links from the database and show them in individual blocks. Here is my fetch function:

function footer_links_footerblock_contents($variables) {
switch ($variables) {
    case 'group_1':
        $query = db_select('footer_links', 'fl')
                ->fields('fl', array('name'))
                ->condition('fl.group', 1);
        break;
    case 'group_2':
        $query = db_select('footer_links', 'fl')
                ->fields('fl', array('name'))
                ->condition('fl.group', 2);
        break;
    case 'group_3':
        $query = db_select('footer_links', 'fl')
                ->fields('fl', array('name'))
                ->condition('fl.group', 3);
        break;
    case 'group_4':
        $query = db_select('footer_links', 'fl')
                ->fields('fl', array('name'))
                ->condition('fl.group', 4);
        break;
    case 'group_5':
        $query = db_select('footer_links', 'fl')
                ->fields('fl', array('name'))
                ->condition('fl.group', 5);
        break;
}

return $query->execute();
}

The result array produced from the $query->execute(); will be going to my module's block_view function:

function footer_links_block_view($delta = '') {
switch ($delta) {
    case 'group_1':
        $block['subject'] = t('Footer Group 1');
        if (user_access('access content')) {
            $result = footer_links_footerblock_contents($delta);
            $items = array();
            foreach($result as $node) {
                $items[] = $node->name;
            }
            if (!empty($items)) {
                $block['content'] = theme('item_list', $items);
            }
            else {
                $block['content'] = t('');
            }
        }
        break;
    case 'group_2':
        $block['subject'] = t('Footer Group 2');
        if (user_access('access content')) {
            $result = footer_links_footerblock_contents($delta);
            $items = array();
            foreach($result as $node) {
                $items[] = $node->name;
            }
            if (!empty($items)) {
                $block['content'] = theme('item_list', $items);
            }
            else {
                $block['content'] = t('');
            }
        }
        break;
    }
    return $block;
}

In there I used the theme function theme('item_list', $var) to display them as a list. I have overridden the theme function like this in my template.php file:

function mytheme_item_list($variables) {
$output = '<div class="col-lg-2">';
$output .= '<ul class="list-unstyled">';

foreach ($variables as $member) {
    $output .= '<li><a href="#">' . $member . '</a></li>';
}

$output .= '</ul></div>'; return $output;
}

And here is my problem. When I looked at the output in the footer section, I see list items that are not even in my database in which I fetch. I see 'Array', 'ul', 'item_list' extra added list items. What is the source of this extra output? Am I not properly overriding the theme function?

  • 1
    There are really only two things that can be wrong here, but you haven't addressed either of them in your question: 1. what does the $items array contain? If it's structured incorrectly, the output will be correspondingly broken. 2. Does the default version of theme_item_list() work correctly? If so, your theme implementation must be wrong (on that subject, the default implementation is 7-8x as long as yours...) – bhotel Nov 6 '15 at 11:47
0

Your code is wrong, since the items to render are in $variables['items'] not $variables as evident in theme_list_items().

function theme_item_list($variables) {
  $items = $variables['items'];
  $title = $variables['title'];
  $type = $variables['type'];
  $attributes = $variables['attributes'];

  // Omissis
}

You are confusing what the theme function gets in Drupal 7 with what the same theme function gets in Drupal 6.

In general, in Drupal 7 what the theme functions need to render is always an index of the array $variables passed them; they never use $variables directly like your code does.

function theme_table($variables) {
  $header = $variables['header'];
  $rows = $variables['rows'];
  $attributes = $variables['attributes'];
  $caption = $variables['caption'];
  $colgroups = $variables['colgroups'];
  $sticky = $variables['sticky'];
  $empty = $variables['empty'];

  // Add sticky headers, if applicable.
  if (count($header) && $sticky) {
    drupal_add_js('misc/tableheader.js');
    // Add 'sticky-enabled' class to the table to identify it for JS.
    // This is needed to target tables constructed by this function.
    $attributes['class'][] = 'sticky-enabled';
  }

  $output = '<table' . drupal_attributes($attributes) . ">\n";

  if (isset($caption)) {
    $output .= '<caption>' . $caption . "</caption>\n";
  }

  // Omissis
}

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.