8

I would like to override the container.html.twig template but the twig debugging tool doesn't give any suggestion.

I tried to add suggestion following https://drupal.stackexchange.com/a/249565/85029 answer but I end up with the suggestion being container----.html.twig.

The context is a display field with an embed view in a node.

I guess, the solution might be pretty close from what I have now (using the suggestion hook) but I'm not able to have some satisfying suggestion.

My current hook is

function mytheme_theme_suggestions_container_alter(&$suggestions, array $variables) {
  $element = $variables['element'];

  if (isset($element['#type']) && $element['#type'] == 'view') {
    $suggestions[] = 'container__' . $element['#name'];
    $suggestions[] = 'container__' . $element['#name'] . '__' . $element['#display_id'];
  }

  if (isset($element['#type']) && $element['#type'] == 'container' && isset($element['children']['#type'])) {
    $suggestions[] = 'container__' . $element['children']['#type'];
  }
}

Edit :

Ended up with something corresponding to my expectations by adding

if (isset($element[0]) && $element[0]["#theme"]) {
    foreach ($element[0]["#theme"] as $themElem) {
        $suggestions[] = 'container__' . $themElem;
        }
}

3 Answers 3

13

I just did this a month ago for the same reason:

/**
 * Implements hook_theme_suggestions_container_alter().
 */
function mytheme_theme_suggestions_container_alter(&$suggestions, array $variables) {
  $element = $variables['element'];

  if (isset($element['#type']) && $element['#type'] == 'view') {
    $suggestions[] = 'container__' . $element['#name'];
    $suggestions[] = 'container__' . $element['#name'] . '__' . $element['#display_id'];
  }

  if (isset($element['#type']) && $element['#type'] == 'container' && isset($element['children']['#type'])) {
    $suggestions[] = 'container__' . $element['children']['#type'];
  }
}

This will work for Views and general types. It is coming up blank because you need to handle it a little differently depending on whats being rendered.

Then in my twig file I am just doing:

{{ children }}

because I didn't want wrapping divs.

9
  • It seems none of the two conditions are filled in my case since I don't have any suggestion with that code :s Might it be because I used an infinite scroll view for my view ?
    – Tibo
    Commented Jun 25, 2018 at 16:53
  • It shouldn’t matter, it’s still a View. Did you clear the cache? Are you sure the code is being hit?
    – Kevin
    Commented Jun 25, 2018 at 16:58
  • I've cleared the cache and the code is being hit since removing a condition give me a suggestion of container----
    – Tibo
    Commented Jun 25, 2018 at 17:02
  • Did you debug it? Whats in $suggestions? Whats in $variables?
    – Kevin
    Commented Jun 25, 2018 at 17:20
  • Is $suggestions defined correctly as being modifiable? You should post the code you have.
    – Kevin
    Commented Jun 25, 2018 at 17:27
2

Initially based on Kevin's code, the Template Suggest module now includes container template suggestions. An example of what it will provide for a view:

 FILE NAME SUGGESTIONS:
   * container--view--case-study--page-listing.html.twig
   * container--view--case-study.html.twig
   x container--view.html.twig
   * container--no-parent.html.twig
   * container.html.twig
2
  • How do you get Template Suggest to produce output??? Typical Drupal Contrib fashion- not a shred of documentation :(
    – sea26.2
    Commented Feb 18, 2019 at 21:52
  • 1
    @sea26.2 Enable the module and it provides template suggestions for the scenarios documented on its project page. You can see the suggestions it (and anything else) provides by enabling twig debugging.
    – mlncn
    Commented Oct 21, 2019 at 21:49
1

Works for me:

/**
 * Implements hook_theme_suggestions_container_alter().
 */
function THEMENAME_theme_suggestions_container_alter(array &$suggestions, array $element)
{
    if (isset($element['element'][0]) && $element['element'][0]["#theme"]) {
        foreach ($element['element'][0]["#theme"] as $them_item) {
            $suggestions[] = 'container__' . $them_item;
        }
    }
}
1
  • Welcome to Drupal Answers :)! Thank you for your contribution. Unfortunately you posted a code-only answer. Maybe you can add some explanation why you think your snippet should work for OP.
    – leymannx
    Commented Jun 3, 2019 at 10:36

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.