2

I'm creating a table within a render array. I can easily create rows within the table like so

$rows = [
    'test 1',
    'test 2',
    'test 3',
];
$header = [
    'header 1',
    'header 2',
    'header 3',
];
$render['table'] = [
  '#theme' => 'table',
  '#header' => $header,
  '#rows' => $rows,
]; 

However when I attempt to include html tags within the row like so:

$rows = [
    '<a href="test1">test 1</a>',
    '<a href="test2">test 2</a>',        
    '<a href="test3">test 3</a>',    
];

it spits it out with the tags written out. It shows

<a href="test1">test 1</a><a href="test2">test 2</a><a href="test3">test 3</a>

instead of showing

test 1 test 2 test 3

Is there a way to use html tags inside rows of a table element in a render array?

UPDATE: I've also tried

'#rows'    => [
                        'test 1'  => [
                            '#type'  => 'html_tag',
                            '#tag'   => 'h1',
                            '#value' => 'Test 1',
                        ],
                        'test 2'  => [
                            '#type'  => 'html_tag',
                            '#tag'   => 'h1',
                            '#value' => 'Test 2',
                        ],
                        'test 3'  => [
                            '#type'  => 'html_tag',
                            '#tag'   => 'h1',
                            '#value' => 'Test 3',
                        ],
                    ],

But that returns a table with 3 rows and 3 columns that say

'html_tag' 'h1' 'Test 1'

'html_tag' 'h1' 'Test 2'

'html_tag' 'h1' 'Test 3'

Instead of actually producing an html tag.

4 Answers 4

3

Try creating the links using function Drupal::l,

For example,

$url = Url::fromUri('http://example.com/');
$link1 = \Drupal::l(t('Link1'), $url);
$url = Url::fromUri('http://example.com/');
$link2 = \Drupal::l(t('Link2'), $url);
$url = Url::fromUri('http://example.com/');
$link3 = \Drupal::l(t('Link3'), $url);
$rows = [
  array(
    $link1,
    $link2,       
    $link3,  
  ),
];
$header = [
    'header 1',
    'header 2',
    'header 3',
];
$render['table'] = [
  '#theme' => 'table',
  '#header' => $header,
  '#rows' => $rows,
]; 

To add other html tags to table , you can use Drupal markup element and renderer service,

$element['#markup'] = '<span>Example Span Tag</span>';
$span_tag = \Drupal::service('renderer')->render($element);
3
  • This may work for a hyperlink but I have a variety of html tags I need, such as <audio> or <video> tags.
    – Amy
    Aug 30, 2016 at 17:58
  • I have updated the answer to include other html tags in table.
    – Syam
    Aug 31, 2016 at 5:05
  • It you definitely can't trust the render mechanism to create what you need, you can use Markup::create().
    – Gábor
    Jun 12, 2018 at 17:11
1

You can use the Markup class:

Markup::create('<strong>test 1</strong>')

Full example with rendering a table can be found here: https://stefvanlooveren.me/blog/how-render-table-drupal-8

0

In order to avoid HTML tags being escaped, #rows items should be defined as arrays with data property. For example:

$rows = [
  ['data' => '<strong>test 1</strong>'],
  ['data' => '<s>test 2</s>'],
  ['data' => '<div>test 3</div>'],
];
$header = [
  'header 1',
  'header 2',
  'header 3',
];
$render['table'] = [
  '#theme' => 'table',
  '#header' => $header,
  '#rows' => $rows,
];

I could not find a reference to this in the documentation. Discovered via trial and error.

1
  • Doesn't work, for two reasons. Your $rows variable needs another level of array nesting. And even if you add that extra array wrapper the markup is still escaped.
    – Bob Kline
    Apr 29, 2022 at 21:20
0

None of the above worked for me, but this did:

$link = Link::createFromRoute('Test', '<front>');
$row['col1'] = ['data' => [ '#markup' => 'Something (' . $link->toString() . ')']];

Your Answer

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

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