5

I'm trying this:

        $content[] = array(
          '#theme' => 'link',
          '#text' => $row->title,
          '#path' => $base_path . 'node/' . $row->nid,
          '#attributes' => array('attributes' => array('title' => $row->title)),
          '#prefix' => '<h3>',
          '#suffix' => '</h3>',
        );

but Drupal is complaining that "Arg 1 passed to drupal_attributes() must be an array, null given" My array looks fine? ...or does it xD

Here is the API doc I'm looking at

8

The theme_link() function behaves a bit like the l() function; the attributes array needs to be inside an #options property:

$link = array(
  '#theme' => 'link',
  '#text' => $row->title,
  '#path' => $base_path . 'node/' . $row->nid,
  '#options' => array('attributes' => array('title' => $row->title)),
  '#prefix' => '<h3>',
  '#suffix' => '</h3>'
);

echo render($link);
  • Who's awesome? Clive is awesome! – Merrick Mar 6 '12 at 17:04
  • Crap, I was even on that page & didn't see that comment, thanks for your attention to detail! – Merrick Mar 6 '12 at 17:07
  • @Merrick No probs, I've been stung by this one before myself :) – Clive Mar 6 '12 at 17:40
  • 5
    Apparently it's better to use '#type' => 'link' as otherwise some l() preprocessing gets missed. Note the keys differ depending on the method. – Andy Apr 11 '13 at 9:55
3

Wanted to edit Clive post, but got rejected :(


As Andy pointed out, it's not recommended to use theme_link instead of l(). Using drupal_pre_render_link() is cleaner for renderable array.

 $link = array(
  '#type'  => 'link',
  '#title' => $row->title,
  '#href'  => 'node/' . $row->nid,
);

You can also go for the "easy-quick-fix" solution with #markup (But don't)

$link = array(
  '#markup' => l($row->title, 'node/' . $row->nid),
);
1

I think your code should read

'#attributes' => array('title' => $row->title),

The wording of the doc in common.inc seems a little unclear, but see the doc for drupal_attributes() for an example.

  • Yeah, I tried that first and got the same error. – Merrick Mar 6 '12 at 16:35

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.