1

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.

3
  • just {{ drupal_view('VIEW_NAME', 'PAGE_NAME') }} print the view with the header.
    – berramou
    Mar 7, 2019 at 23:34
  • @berramou – OP wants only the header.
    – leymannx
    Mar 8, 2019 at 7:57
  • twig tweak provide only two extension for views, drupal_view and drupal_view_result and no one of the did this, i think you shoud develop you own twig extension.
    – berramou
    Mar 8, 2019 at 8:58

2 Answers 2

3

I have create simple extension for that:

  1. add the following to MYMODULE.services.yml

    services:
      MYMODULE.twig.render_view_header:
        class: Drupal\MYMODULE\Twig\HeaderView
        tags:
          - { name: twig.extension }

  1. Create HeaderView class inside MYMODULE/src/Twig.
  2. Copy the code from HeaderView.php
  3. Clear cache.
  4. In your twig call the extension like:

{{ render_view_header('VIEW_NAME', 'PAGE_NAME') }}

12
  • this is awesome! At line 67 in HeaderView.php, I'm looking at my xdebug output all all of the info for the view is null. 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 the global 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 ?
    – berramou
    Mar 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 not ViewExecutable-> storage ?
    – berramou
    Mar 8, 2019 at 14:45
-1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.