How to hide multiple fields from display in Drupal 8 using the Twig "without" filter.

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

I'm just posting this here in case anyone is searching for it.

Here is how to remove multiple fields from display in a template file using Twig in Drupal 8:

{{ content|without('field_yourfield', 'field_yourfield2') }}

In this example the without filter is taking two values to exclude them from display. They can be rendered on their own later on like this:

    {{ content.field_yourfield }}
    {{ content.field_yourfield2 }}

In Drupal 7 we would have done something like this:

<?php
      hide
($content['field_yourfield']);
     
hide($content['field_yourfield2']);
      print
render($content);
?>

And later:

  <?php print render ($content['field_yourfield']); ?>
  <?php print render ($content['field_yourfield2']); ?>