Question

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.

Was it helpful?

Solution

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') }}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top