The section "Use #type='link' for adding a single link to a render array, particularly for tables that include operation links like 'edit', 'delete', etc." is not clear for tables because the only provided example applies only to forms.
It should be made more clear that in order to pass a render array as a cell in a table, the array must be in an array in the 'data' index. An example like the following would be very helpful:

Drupal 6:

  $row[] = l(t('link title'), 'path/to/location');

Drupal 7:

  $row[] = array('data' => array('#type' => 'link', '#title' => t('link title'), '#href' => 'path/to/location'));

If you don't put that render array in the 'data' index of an array, it treats the render array as attributes for the cell.

Comments

arianek’s picture

Project: Documentation » Drupal core
Version: » 7.x-dev
Component: Correction/Clarification » documentation

it's borderline which queue this should be in, but moving it to core docs so jhodgdon can review

joachim’s picture

I'm not entirely I follow this...

Is what you are saying:

  // wrong! won't work
  $row[] = array('#type' => 'link', '#title' => t('link title'), '#href' => 'path/to/location');
  // this is correct
  $row[] = array('data' => array('#type' => 'link', '#title' => t('link title'), '#href' => 'path/to/location'));
  // but this should still work, no?
  $row[] = l(t('link title'), 'path/to/location');

The example as it stands it not very clear anyway, as it's not clear that what's being assembled by taxonomy_overview_vocabularies() will then go to theme_table().

For an unthemed form, this works fine:

  $form['foo'] = array('#type' => 'link', '#title' => t('link title'), '#href' => 'path/to/location');  
jhodgdon’s picture

This is the right queue for the Updating Modules pages. I don't have time to review this right at the moment...

jhodgdon’s picture

Issue tags: +Needs documentation

Adding tag that we use for Updating page revisions

jhodgdon’s picture

New tagging scheme

jhodgdon’s picture

Status: Active » Fixed

Sorry for the delay on this one.

The original post in my opinion is someone who just didn't understand how to build a table row using complex cells rather than just putting values into cells.

That said, the example in that section was unduly complex. So what I did is simplify the example down to two lines for d6/7 - one line as a form element and one line as a table cell. That should make everyone happy.

Status: Fixed » Closed (fixed)
Issue tags: -Needs documentation updates

Automatically closed -- issue fixed for 2 weeks with no activity.

dianacastillo’s picture

Drupal 8 :

use Drupal\Component\Render\FormattableMarkup;  
  $rows[] = array(

      array('data' => new FormattableMarkup('<a href=":link">@name</a>', 
        [':link' => $entry['link_url'], 
        '@name' => $entry['name']])
      ),
gmangones’s picture

In 8 work fine.

ilechcod’s picture

Issue summary: View changes

I just spent days trying to debug this in 2021. It's still not documented anywhere that the render array has to be put in the 'data' array for things to work. Was about to give up when I stumbled on this thread. This is unnecessary expense of energy. What does it take to document this officially inside the table element page?

jhodgdon’s picture

If you want to change the documentation for
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21...
what you need to do is:

a) Create a new issue describing what needs to be added to the documentation.

b) In the Issue summary and relationships section, mark it as Related to this issue and to #2486967: [meta] Move/Create Form Element Documentation.

c) Add a new comment to both of the issues in (b) with a link to your new issue, so that people following either issue will know about your new issue.

d) If possible, make a patch for your new issue, or if not, be ready to review a patch that someone else creates. See
https://www.drupal.org/community/contributor-guide/task/create-a-patch-f...
for info on making patches.