I have created an item list inside a foreach loop like
$items[] = [
'#markup' => 'some html text'
]
then pass it to
themed_list = [
'#theme' => 'item_list',
'#items' => $items,
'#attributes' => ['class' => ['my-list-class']],
];
this works fine with the Drupal’s standard item_list.html.twig
{% if context.list_style %}
{%- set wrapper_attributes = wrapper_attributes.addClass('item-list--' ~ context.list_style) %}
{%- set attributes = attributes.addClass('item-list__' ~ context.list_style) %}
{% endif %}
{% if items or empty -%}
<div{{wrapper_attributes.addClass('item-list')}}>
{%- if title is not empty -%}
<h3>{{ title }}</h3>
{%- endif -%}
{%- if items -%}
<{{list_type}}{{attributes.addClass('list-group')}}>
{%- for item in items -%}
<li{{item.attributes.addClass('list-group-item')}}>{{ item.value }}</li>
{%- endfor -%}
</{{list_type}}>
{%- else -%}
{{- empty -}}
{%- endif -%}
</div>
{%- endif %}
and produces the expected code
<ul class="my-list-class list-group">
<li class="list-group-item">my html text</li>
...
</ul>
Now I want to add a custom class to the <li>
elements so I could display
<li class="list-group-item my-li-class">my html text</li>
I tried
$items[] = [
'#markup' => 'some html text',
'#attributes' =>['class' => ['my-li-class]]
]
but this doesn’t have any effect although if I add prefix and suffix
$items[] = [
'#markup' => 'some html text',
'#attributes' =>['class' => ['my-li-class]],
'#prefix' => '<div class="div-class">',
'#suffix' => '</div>'
],
#prefix and #suffix are correctly displayed.
<li class="list-group-item">
<div class="div-class">my html text</div>
</li>
What I don’t understand is that both Drupal documentation and twig file comments say
items: A list of items. Each item contains:
attributes: HTML attributes to be applied to each list item.
value: The content of the list element.
I tried ‘#value’ and ‘#attributes’ as well as ‘value’ and ‘attributes’ without success. I’m stuck on how can I define the renderable array. Any help welcome.