nortmas
9/27/2017 - 2:54 PM

Item list

<h3>Example of using #theme</h3>
<ul>
  <li>Simple string</li>
  <li>Simple <span>#markup</span> string</li>
  <li class="custom-item-class">Custom item</li>
  <li>Parent item
    <ul>
      <li>Simple string child</li>
      <li class="custom-child-item-class">Second child item with custom attributes</li>
    </ul>
  </li>
</ul>
$items = array();
// A simple string item.
$items[] = 'Simple string';

// A simple string item as render array.
$items[] = [
  '#markup' => 'Simple <span>#markup</span> string',
];

// Set custom attributes for a list item.
$items[] = [
  '#markup' => 'Custom item',
  '#wrapper_attributes' => array(
    'class' => array('custom-item-class'),
  ),
];

// An item with a nested list.
$items[] = [
  '#markup' => 'Parent item',
  'children' => [
    'Simple string child',
    [
      '#markup' => 'Second child item with custom attributes',
      '#wrapper_attributes' => [
        'class' => array('custom-child-item-class'),
      ],
    ],
  ],
];

$build['list'] = [
  '#theme' => 'item_list',
  '#items' => [
    $this->t('This is some text that should be put in a list'),
    $this->t('This is some more text that we need in the list'),
  ],
];

$build['theme_element'] = [
  '#theme' => 'item_list',
  '#title' => $this->t('Example of using #theme item_list'),
  '#items' => $items,
];