1

I have tried for a few days now, to figure this one out on my own, but have had next to no luck getting a working solution.

The issue:
I have a view that displays a list of images, where all wrappers have been removed, so that it only contains the images themselves. I have also overwritten templates for this view, so that any additional wrappers from templates are also removed.

The issue is that views automatically adds <div class="views-element-container"> around the entire views. Normally this would be fine, but I have no need for it for this view, it's just cluttering the markup.

I know that it in some cases is used for additional functionality, such as the contextual links, but this has also been disabled for this view.

What I have tried:

  • I have tried to figure out if views also adds a template suggestion to container.html.twig, but sadly found no indication anywhere on what that might be (even if I used normal views template naming convention, it didn't override)
  • I have tried to access the views render array in a preprocess function, to unset #theme_wrappers, in hopes that this would prevent views from using the container. Sadly this also didn't do anything. Code example below.
function rodekors_preprocess_views_view__paragraph_gallery(&$variables) {
  unset($variables['view_array']['#theme_wrappers']);
}

I simply want to remove the wrapper div from my specific view, yet have found no information on how to do that.

Does anyone know how to achieve this? Preferably without having to install a module for what should be such a simply thing to do.

3 Answers 3

3

You can use a preprocess function to add a theme suggestion specifically for this view.

Something like:

function rodekors_theme_suggestions_container_alter(array &$suggestions, array $variables) {
  // since kint() / dpm() aren't working, use a js console.log
  echo "<script>console.log( 'Debug Objects: " . $variables['element']['#name'] . "' );</script>";

  if($variables['element']['#name'] = 'view_name') { // value from console.log
    $suggestions[] = 'suggested_template';
  }
}

Note: The above function is re-worked to function without dpm / kint, as OP had an unrelated debugging error. Usually, the echo "<script..."; code would be replaced with a standard Drupal debug function.

Then you can copy the standard container template file, rename it 'suggested-template', and just remove the outermost wrapping div.

Because that views-element-container is a container around the view, you need to access / alter the code on the container level, instead of the view level. Most likely, even if your unset theme-wrappers code worked, the div would still exist, just without its class.

3
  • Thanks! That is indeed a great suggestion that would work, if I could find what variable value is unique to my view, sadly if I use dpm or kint on the variables, to find the unique value, I get the following error: Error: Cannot access protected property Drupal\Core\Utility\Error::$blacklistFunctions in Drupal\Core\Utility\Error::getLastCaller() (line 116 of core/lib/Drupal/Core/Utility/Error.php). Commented Jun 9, 2017 at 6:09
  • That's quite the odd error, and one I haven't seen before. From a little quick research, it looks like that's an unrelated error that Drupal core has to fix. In the meantime, I updated the code above - hopefully that should help you get the view #name within the container, and get that hook_suggestion working!
    – Lauren G
    Commented Jun 9, 2017 at 13:21
  • The if statement is assigning value instead of comparing.
    – Arie Pinto
    Commented Jan 16, 2023 at 8:00
4

I was only able to solve this after much hair-pulling regarding Drupal not picking up my suggested template. Where I was hung up was that the template suggestion must start with container__ otherwise Drupal will ignore the file and use the next template in the suggestion list (likely the default container.twig.html).

Here is my code:

/**
* Implements hook_theme_suggestions_user_alter().
*/
function myTheme_theme_suggestions_container_alter(array &$suggestions, array $variables) {
  // A list of view names in which to exclude the container markup.
  $exclude_views = [
    'someView',
  ];

  if (in_array($variables['element']['#name'], $exclude_views)) {
    $suggestions[] = 'container__no_wrapper';
  }
}

I then added container.html and container__no_wrapper to my theme at templates/container/.

1
  • Pay attention to the file name: it must be container--no-wrapper.html.twig
    – MXT
    Commented Nov 17, 2022 at 10:47
-1
if($variables['element']['#name'] = 'view_name') { // value from 

should be

if($variables['element']['#name'] == 'view_name') { // value from 

AND

$suggestions[] = 'suggested_template';

should be

$suggestions[] = 'suggested__template';

:)

0

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.