13

I'm attempting to create an unordered list using a render array in a custom block in a custom module.

I understand if I wanted to wrap things in div I could always use the 'container' type. Like so

    $content = [
        'container'    => [
            '#type'       => 'container',
            '#attributes' => [
                'class'      => ['container'],
            ],
            'item 1'      => [
                '#type'       => 'html_tag',
                '#tag'        => 'p',
                '#value'      => 'Item 1',
            ],
            'item 2'      => [
                '#type'       => 'html_tag',
                '#tag'        => 'p',
                '#value'      => 'Item 2',
            ],
        ],
    ];

But I'd like to be able to use an unordered list like so

    $content = [
        'container'    => [
            '#type'       => 'html_tag',
            '#attributes' => [
                'class'      => ['container'],
            ],
            '#tag' => 'ul',
            'item 1'      => [
                '#type'       => 'html_tag',
                '#tag'        => 'li',
                '#value'      => 'Item 1',
            ],
            'item 2'      => [
                '#type'       => 'html_tag',
                '#tag'        => 'li',
                '#value'      => 'Item 2',
            ],
        ],
    ];

What I'd like that to produce is

<ul class="container">
  <li>Item 1</li>
  <li>Item 2</li>
</ul>

But instead it produces

<ul class="container"></ul>
<li>Item 1</li>
<li>Item 2</li>   

Is there a way to create an unordered list using render arrays?

1 Answer 1

33

Use the theme 'item_list':

$content = [
  '#theme' => 'item_list',
  '#list_type' => 'ul',
  '#title' => 'My List',
  '#items' => ['item 1', 'item 2'],
  '#attributes' => ['class' => 'mylist'],
  '#wrapper_attributes' => ['class' => 'container'],
];          

The wrapper_attributes are not used in all themes. In core this is classy and bartik, which is based on classy.

5
  • 1
    Would you point me to the documentation for this? Commented Feb 1, 2017 at 14:05
  • 2
    I think the best documentation is in the template: cgit.drupalcode.org/drupal/tree/core/modules/system/templates/…
    – 4uk4
    Commented Feb 1, 2017 at 14:14
  • 2
    You should replace '#list_type' with '#type' according to api.drupal.org/api/drupal/includes!theme.inc/function/… Commented May 18, 2018 at 8:39
  • 1
    @rudolfbyker, this is a D7 link, but how is #list_type (a template variable of item_list) and #type (specifies a render element) connected?
    – 4uk4
    Commented May 18, 2018 at 9:06
  • 2
    Sorry, I didn't see the "8" tag on the question. What I meant to say is this: The code in the answer works for D7 only if you change #list_type to #type. #list_type is ignored. Commented May 18, 2018 at 11:40

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.