Simple ways to rewrite the output of Drupal 8 Views fields

Jun 05, 2019
admin
Ways to rewrite the output of Drupal 8 Views fields

The unique Drupal Views module allows you to pull the data from the database and display it in any way you wish. As an example, we discussed creating photo galleries with Drupal 8 Views. Drupal’s flexibility is unlimited, so it offers additional ways to customize the content presentation and meet the customer’s precise requirements. One of them is to rewrite the output of Drupal Views fields. Let’s explore this a little bit using the simplest of cases.

Why rewrite Drupal 8 Views field output?

It’s possible to shape your Views like Legos when using the field-based format in it. You can add only the desired fields of a content type or other entity type. It’s easy to rearrange the fields in any order, hide or show the field labels, configure the fields, and so on.

However, there are cases when you just need the fields to display differently. The customer may want to merge two fields into one, use the values of one field in another, link fields to specific pages, replace links with icons, and so on — the sky's the limit.

The solution depends on the case. In more complicated cases, expert Drupal development teams create custom field formatters. In simpler ones, they just rewrite the output of Drupal Views fields via the built-in Views dashboard capabilities.

Main principles of rewriting the output of Drupal 8 Views fields

When rewriting Views fields, we need to take into account the main principles:

  • The Views format should be configured as field-based.
  • Every field has a “rewrite results” section in settings, which has 6 options:

Rewriting Drupal 8 Views field output

  • There are replacement patterns that allow us to use tokens and display dynamic values (for example, the ID of the current node).

Replacement patterns for Views fields

  • We can add fields to Views, hide them from display, and use their values in other fields.
  • The hidden fields whose values we want to use should always be positioned above the ones we want to rewrite in the Views list of fields.

A simple example on how to rewrite Drupal 8 Views fields

Let’s create a view that displays all nodes of a content type. It uses fields and shows content as a table.

Field-based Drupal 8 view

In our rewrite example, we want to:

  • merge the content title and body into the same Views table column
  • add a content editing link that will be displayed as a pretty edit icon with a pencil.

1) Merging two fields into the same column

We will overwrite the title field by adding the body field values to it. We don’t need the body field to be shown — we just need to grab its values. So we create the body field and exclude it from display.

Exclude a Drupal 8 field from display

And we rearrange the fields so the body field is above the title field.

Rearranging Drupal 8 fields

In the title field settings, we choose “Rewrite Results” — “Override the output of this field with custom text.” And then we open “Replacement Patterns” and see the tokens for the title and the body.

Rewriting output of Drupal Views as custom text

We grab the tokens and put them into the text box.

Using replacement tokens in Drupal Views fields

And now our view shows them in the same column!

Merging title and body fields in Drupal Views

2) Adding an edit icon that leads to the edit page

We add “The edit icon” field to our content type, and attach the icon itself as a default image. We can move this field to “Disabled.”

Adding default image to a Drupal field

We then return to our view, find this field, add it to the view, and rewrite it so it leads to the content editing page. For this, we select “Output the field with custom link,” go to Replacement Patterns, grab the “node ID” ({{ nid }}) token, and shape our custom link with it:

Rewriting output of Drupal 8 Views fields

Done! We have a pretty edit icon that leads directly to the node editing page.

Drupal 8 view with field output rewrite

Let’s rewrite your Views fields exactly as you need

This has been just an elementary example of how to rewrite the output of Drupal 8 Views fields. Your fields will show exactly the way you want. It can be done with the built-in Views dashboard options, or with custom field formatters for more serious cases.

All you need is to contact our Drupal support and development team!

 Get new blog posts by email