Rewrite Results in Views.

As part of a custom content type I was asked to set up an ‘Author Information’ block which would contain two pieces of information from a user’s profile which would not normally show up using one of the existing Drupal modules.  The first was the user’s email address and the second was a public location field (city, state) which is part of the user’s Profile2 info.  The snag was that we wanted the user to choose if this info would appear in the info block on a node by node basis.

Here is an example block.

Example Block

So, as part of the content type, I set up two fields:

  • Display my email address
  • Display my public location

Both fields are lists with values of 1|Yes and 0|No (I could have used Booleans - you’ll see why in a minute).

I set up a View to display the block - the big trick here is to set up the Contextual Filters, to pull in the User and Node info, and to set up the Relationships to get the Profile info.  

Here is the View - click the thumbnail

View

For the Fields, adding the Author’s email address is simple and it is set to exclude from display and to link with a mailto:. Adding the Public Location requires pulling the info from the node Contextual Filter which is a little more involved, so I will skip it.

Email Field

You also need to add the ‘Display my email address’ field from the (nodes) Content selector. Selecting the Formatter as a key is important - I want the 1 or 0 here, not the Yes or No. I also want to rewrite the results here and insert the email address using a token: - [mail] .

Display my email address

I also want to set the ‘No Results Behavior’ including the ‘Count the number 0 as empty’, hence my pseudo Boolean text field. I also hide the field and the rewrite. Click the thumbnail.

No Results Behaviour

So while its a bit complicated, it produces the result I wanted…