I have the following in a simple Drupal 8 controller. I would like to add an item to an item list that has two different links and some text, but cannot figure out how to do it. In my code below, the first bullet point is just a link. The second is just some text. The third is will print each link as escaped HTML and concatinate them with the " and ". How do I print a combination of links and text in an item list?

$basic_items[] = Link::createFromRoute('Basic example','flot_examples.basic_usage');
$basic_items[] = "Different graph types and simple categories/textual data";
$basic_items[] = Link::createFromRoute('Different graph types', 'flot_examples.series_types')->toString() .
" and " .
Link::createFromRoute('simple categories/textual data', 'flot_examples.categories')->toString();

$output['basic'] = [
  '#title' => 'Basic Usage',
  '#theme' => 'item_list',
  '#items' => $basic_items,
];
return $output

Comments

titouille’s picture

Achieve it by using a renderable array for each item:

$basic_items[] = ['#markup' => Link::createFromRoute('Different graph types', 'flot_examples.series_types')->toString() .
" and " .
Link::createFromRoute('simple categories/textual data', 'flot_examples.categories')->toString()];

Maybe late but useful for other. I have spent time on to found the solution...

In my case, I'd like to add an item list in a ListViewBuilder row, and I used:

$render = [
  '#theme' => 'item_list',
  '#items' => $list,
];
$row['my_row'] = \Drupal::service('renderer')->render($render);

I used the renderer service to render the renderable array because the row take a string.

Beakerboy’s picture

I think the best solution is to use something like:

$link_array = [
  ':one' => 'http://example1.com',
  ':two' => 'http://www.example2.org',
];
$this->t('<a href=":one">Link One</a> and also <a href=":two">Link Two</a>', $link_array);