Need some help with your project? Contact me

How to output together one or more View fields

Rewriting results for a field in Views allows you to join two or more fields for Views to output together. This also allows you to create custom data visualization by giving you the power to insert html markup into the View and use with CSS.

For the purpose of this tutorial, I will focus on a very simple example, and you can take it from there, play around and discover the power of Views in this sense.

Imagine a simple View which displays content containing the First Name and Last Name fields of a Content Type. If you want to follow this example, go ahead and create a Content Type with the two text fields.

In your View, you can display them in a number of ways: formatted/unformatted list, table etc. You can show or hide labels, format a bit the output of the fields and other simple stuff. But in our case we want to show the first name and the last name on the same row.

For this, we rewrite the output of the the fields.

So following my example, you have 2 fields: First Name and Last Name in an unformatted list View format. To show both names on the same row, you need to do the following:

First, you edit the First Name field and tick the box: Exclude from display.

Next, you edit the Last Name field and under the Rewrite Results heading, you tick the box: Rewrite the output of this field. You’ll notice that a text box appears. There you have to write (for our example):

[field_first_name] [field_last_name]

What you wrote there are tokens that call on your two fields. To find these tokens, just scroll a bit down to the Replacement Patterns heading. There you will see all the tokens representing all the fields that precede the field you are currently rewriting the output for. This is why it is important that you rewrite the output for the last field you want included in the token list. If you have another field following the Last Name field, you will not find it in that list.

OK, so there you can also add html, create all sorts of markup and add a bunch of fields etc. Go crazy!

Hope this helps.

Comments

Thx, forgot about the order of fields being important for token usage

thanks a lot for the trick!

Thank you! You saved me from hacking a module and probably wrecking my D7 setup.

Add new comment

You can post comments in Markdown and basic HTML tags.
For code blocks, wrap your code within '~~~'. For example:
~~~
$var = 'my variable';
~~~