I would like to print out the header for a view, but not the rest of the content using the Twig Tweak module. I've tried variations on {{ drupal_view('VIEW_NAME', 'PAGE_NAME', 'view_header') }}
, but that prints out the entire view.
2 Answers
I have create simple extension for that:
- add the following to
MYMODULE.services.yml
services:
MYMODULE.twig.render_view_header:
class: Drupal\MYMODULE\Twig\HeaderView
tags:
- { name: twig.extension }
- Create
HeaderView
class insideMYMODULE/src/Twig
. - Copy the code from HeaderView.php
- Clear cache.
- In your twig call the extension like:
{{ render_view_header('VIEW_NAME', 'PAGE_NAME') }}
-
this is awesome! At line 67 in
HeaderView.php
, I'm looking at my xdebug output all all of the info for the view isnull
. There's no values for header, footer, etc. If I go into$view->storage
I can see the values for the view. Going to$view->storage->getDisplay('default')['display_options']['header']['area']['content']['value']
actually gets me the value for the header, but is obviously not good code (also theglobal replacement tokens
no longer work). Any clue on what the function would be to get the values, rather than null? Mar 8, 2019 at 14:18 -
if
$headers = $view->header;
is null so you have no header added to your view, did make sure in the views ui that you have a header added ?– berramouMar 8, 2019 at 14:20 -
yes, I have a header added, and I tried adding a footer and that also didn't show up. Every value for the view is either null or false in my xdebug. It's the correct view, and like I said if I go into storage it shows the correct info, but none of it is available at the
$views
level. Mar 8, 2019 at 14:30 -
imgur.com/ZdJ6HqD here's a small screenshot of my xdebug output. Things like
header
are further down and null as well. Mar 8, 2019 at 14:33 -
the header and footer directly under
ViewExecutable
notViewExecutable-> storage
?– berramouMar 8, 2019 at 14:45
Main twig template for the views is views-view.html.twig
. You can see header, footer, content , attachment variable in this file.So you can print your views in your method {{ drupal_view('VIEW_NAME', 'PAGE_NAME') }}
then rename views-view.html.twig
to views-view--VIEW_NAME--PAGE_NAME.html.twig
and remove unwanted content.
{{ drupal_view('VIEW_NAME', 'PAGE_NAME') }}
print the view with the header.drupal_view
anddrupal_view_result
and no one of the did this, i think you shoud develop you own twig extension.