In a custom block, with the method build() as follow :

  public function build() {
    $build = [
      '#theme' => 'item_list',
      '#items' => [
        'lorem',
        'ipsum'
      ],
      '#list_type' => 'ul',
      '#attributes' => [
        'class' => ['test-ul-class'],
        'id' => 'test-ul-id',
      ],
    ];
    return $build;
  }

You presume that your html ul tag will have the class "test-ul-class" and the id "test-ul-id", but the ul tag is alone without any attribute.

If you use the same response in a controller, the ul tag will have the correct class and id.

I try to dig inside template_preprocess_item_list and $variables['attributes'] is an empty array.

I don't know if it only concern item_list or/and within a block.

Comments

Kgaut created an issue. See original summary.

Kgaut’s picture

Issue summary: View changes
Cottser’s picture

Issue tags: +Twig

Very curious, thanks for the report.

claudiu.cristea’s picture

Status: Active » Closed (works as designed)

This is not a bug, your render array is wrong. The build to be rendered should be an array of elements. Your item_list is such an element, thus you'll need to wrap the element in an array:

  public function build() {
    $build = [
      [
        '#theme' => 'item_list',
        '#items' => [
          'lorem',
          'ipsum'
        ],
        '#list_type' => 'ul',
        '#attributes' => [
          'class' => ['test-ul-class'],
          'id' => 'test-ul-id',
        ],
      ],
    ];
    return $build;
  }