Score:1

Why is -content-add-list.html.twig template not being used?

fi flag

I have generated a custom content entity via the Drupal Console. It has bundles. I would like to customise the screen which you get to when adding a new such entity - the screen which displays the list of bundles, to click on the type you want to add.

When I generated the entity, a bunch of template files were created, including one called <my_entity>-content-add-list.html.twig. I am certain this is intended to override the core template called entity-add-list-html.twig. However, changes I make to it do not take effect, and by putting twig in debug mode and viewing the source, I can see that it is not being overridden.

Do I need to add a theme suggestion hook? If so, what would the name of the function be? I feel like I must be missing something. Given that the console generated that template I am surprised it is not automatically used. But surely there must be a simple way of telling Drupal to use it.

EDIT: I've found what to name the function: it's mymodule_theme_suggestions_entity_add_list. But the machine name of the entity is nowhere in the $variables array that I can easily get at. I'm looking at parsing the route to get it out, which seems rather hacky

Score:0
fi flag

So I got there in the end (the machine name of the entity being oc_activity:

function opencase_entities_theme_suggestions_entity_add_list(array $variables) {
  $route_name = \Drupal::routeMatch()->getRouteName();
  $suggestions = []; 
  if ($route_name == "entity.oc_activity.add_page") {
    $suggestions[] = 'oc_activity_content_add_list';
  }
  return $suggestions;
}

But it feels too hacky & hard-coded; it seems there ought to be a nicer way.

Also, the template file that was generate by the console assumed that the array of bundles was called types when actually it was bundles. So I had to change the generated code from for type in types to for type in bundles. Just putting that here in case it's useful to anyone...

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.