Score:1

Trying to insert a line break inside a table header array

tw flag

So I'm programming a module in Drupal that contains several custom forms, one of which also has a table.

One section of the code of the table mentioned above looks like this:

$form['completion_' . $week] = array(
   '#type' => 'table',
   '#title' => 'Completion Week',
   '#header' => array(' ', 'Header 1', 'Header 2', 'Header 3'),             
);

The forms and the tables themselves work correctly. All that I want to do now is add a line break inside the strings of the header array, since my header titles are made of two strings.

So, it should look something like the following:

$form['completion_' . $week] = array(
   '#type' => 'table',
   '#title' => 'Completion Week',
   '#header' => array(' ', $myTitle1 + Line Break + $myAdditionalText1, $myTitle2 + Line Break + $myAdditionalText2, $myTitle3 + Line Break + $myAdditionalText3),             
);

What I have tried so far is the following:

  • \n
  • \r\n
  • br-Tag
  • nl2br
  • PHP_EOL

For example, when I try ...

'#header' => array(' ', 'test1' . PHP_EOL . 'test2', "test1\r\ntest2", 'Header 3'),

... the line breaks for headers one and two get ignored and the string 'test2" remains on the same line as 'test1'.

Any ideas on how to enforce line breaks within the strings of the header array will be highly appreciated. Thank you very much.

cn flag
Never actually had the need to try this, but`['data' => Markup::create('test1<br />test2')]` instead of the string might work
4uk4 avatar
cn flag
or `['data' => ['#markup' => 'test1<br />test2']]`
cn flag
Go with the shorter version - less chars and no alias needed for the Markup class. Bytes save lives.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.