I have a special html list that needs to have some html attributes assigned to the wrapping DIV element.
So I copied the original item-list.html.twig template into my module's templates directory and renamed it to item-list--mylist.html.twig and added the html attributes to the wrapping DIV element.

In hook_theme of my module I've added the theme suggestion

mymodule_theme($existing, $type, $theme, $path) {
  return array(
    ..
    'item_list__mylist' => array(
      'base hook' => 'item_list',
      'template' => 'item-list--mylist',
  );
}

then I displayed the list

  return array(
    '#theme' => 'item_list__mylist',
    '#items' => $items
  );

Unfortunately this resulted in php error due to the fact that the theme enegine put the original data form the render array into a new array item of the resulting render array with empty value as the key, like so:

item list

Then I tried to change the definition in hook_theme to:

  'mylist' => array(
    'base hook' => 'item_list',
    'template' => 'item-list--mylist',
  )

and

  'mylist' => array(
    'base hook' => 'item_list',
    'template' => 'mylist',
  )

which resulted in proper renderable array structure BUT in both cases the template from my module was not picked up.

The documentation and implentation of block__system_branding_block in system_theme suggests that the first definition I've used should work. But it does not. In other two cases it works but without the overriding template being used but the original hook's one which is the whole point of doing this.

Files: 
CommentFileSizeAuthor
item-list-suggestion.jpg97.28 KBivanjaros

Comments

Cottser’s picture

Version: 8.0-alpha11 » 8.0.x-dev
Issue tags: +Twig, +DX (Developer Experience)

The problem is hook_theme() defaults to use 'render element', not 'variables', and item_list uses 'variables'. This code should work better for you:

mymodule_theme($existing, $type, $theme, $path) {
  return array(
    ..
    'item_list__mylist' => array(
      'base hook' => 'item_list',
      // Template line no longer needed - https://www.drupal.org/node/2231673
      // 'template' => 'item-list--mylist',
      'variables' => array('items' => array(), 'title' => '', 'list_type' => 'ul', 'attributes' => array(), 'empty' => NULL),
  );
}

It's not clear to me right now why 'base hook' isn't inheriting the variables/render element setting from item_list. Either this issue could morph into that or we can make a new issue to look into that.

Cottser’s picture

Category: Bug report » Support request
Status: Active » Fixed

I decided a new issue would make more sense than taking this one over, @ivanjaros :)

#2457295: 'base hook' key in hook_theme() is limited in what it inherits from the base hook

As I have provided a workaround for now I think this is fixed, if you are interested in improving this experience please join us in the new issue. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.