1

I'm struggling to get a render-array constructed, and I think it's because of how theme_item_list works. I would be grateful if anyone could help find the problem (or tell me to implement it without item_list!)

The context: This function is called from my module's hook_block_view() to render the content of the block. The module is an example module showing how to use a new API. I hope nothing of that is needed to answer this point, except that these calls are not made from a theme.

The code I have is:

function example_render() {
  $block = array();

/* fill $providers[] ... */

  $items = array();
  foreach ($providers as $metric => $provider) {
    $value = $provider['value'];
    $class_link = $provider['name'];
    $name_link = $provider['name'];

    // Construct a renderable array wrapper for the template invocation.
    $items[$metric] = array(
      '#type' => 'container',
      '#attributes' => array('class' => array('provider')),
      'info' => array(
        'providername' => array(
          '#prefix' => '<span class="provider-name">',
          '#markup' => $name_link,
          '#suffix' => '</span>',
        ),
        'providervalue' => array(
          '#prefix' => '<span class="metric-value">',
          '#markup' => $value,
          '#suffix' => '</span>',
        ),
        'metricname' => array(
          '#prefix' => '<span class="metric-name">',
          '#markup' => $class_link,
          '#suffix' => '</span>',
        ),
      ),
    );
  }

  // Finally, bring it all together.. a container with the via text then a list
  // of the providers.
  $block['content'] = array(
    '#type' => 'container',
    '#attributes' => array('class' => array('example')),
    'ttl_text' => array(
      '#prefix' => '<h3 class="metrics-title">',
      '#markup' => $record['title'],
      '#suffix' => '</h3>',
    ),
    'via_text' => array(
      '#prefix' => '<span class="metrics-via">',
      '#markup' => $via_text,
      '#suffix' => '</span>',
    ),
    'metrics' => array(
      '#items' => $items,
      '#theme' => 'item_list',
      '#attributes' => array(
        'class' => 'providers-list',
      ),
    ),
  );
  return $block;
}

and this is what I get when run with 9 items in the list:

<div class="example"><h3 class="metrics-title">A bacterial sulfonolipid triggers multicellular development in the closest living relatives of animals</h3><span class="metrics-via">via <a href="http://alm.svr.elifesciences.org/articles/info:doi/10.7554/eLife.00013" external="1" target="_blank">Lagotto article information</a> courtesy of <a href="http://articlemetrics.github.io/" external="1" target="_blank">Lagotto</a></span><div class="item-list"><ul class="providers-list"><li #type="container" #attributes="Array" info="Array Array Array" class="first"></li>
<li #type="container" #attributes="Array" info="Array Array Array"></li>
<li #type="container" #attributes="Array" info="Array Array Array"></li>
<li #type="container" #attributes="Array" info="Array Array Array"></li>
<li #type="container" #attributes="Array" info="Array Array Array"></li>
<li #type="container" #attributes="Array" info="Array Array Array"></li>
<li #type="container" #attributes="Array" info="Array Array Array"></li>
<li #type="container" #attributes="Array" info="Array Array Array"></li>
<li #type="container" #attributes="Array" info="Array Array Array" class="last"></li>
</ul></div></div>

not very inspiring!

The three 'Array' under 'info' in the html here obviously correspond to the providername/providervalue/metricname arrays in the code i.e. the $items array isn't being interpreted as a render array at all.

How can I make this work?

1 Answer 1

3

According to https://api.drupal.org/api/drupal/includes!theme.inc/function/theme_item_list/7 theme_item_list is looking for data key so your list item should look like this:

// Construct a renderable array wrapper for the template invocation.
$tmp = array(
  '#type' => 'container',
  '#attributes' => array('class' => array('provider')),
  'providername' => array(
    '#prefix' => '<span class="provider-name">',
    '#type' => 'markup',
    '#markup' => $name_link,
    '#suffix' => '</span>',
  ),
  'providervalue' => array(
    '#prefix' => '<span class="metric-value">',
    '#type' => 'markup',
    '#markup' => $value,
    '#suffix' => '</span>',
  ),
  'metricname' => array(
    '#prefix' => '<span class="metric-name">',
    '#type' => 'markup',
    '#markup' => $class_link,
    '#suffix' => '</span>',
  ),
);
$items[$metric]['data'] = drupal_render($tmp);

That should give you single element:

<li class="first"><div class="provider"><span class="provider-name">test1</span><span class="metric-value">test1</span><span class="metric-name">test1</span></div></li>

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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