Build a simple conditional field in Views 7.x-3.x
Last updated on
25 January 2018
Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites
You can make conditional fields in views without using additional modules like Views PHP.
In this example, we make the path for a node title automatically link to the node path, to a URL link or to a file path, depending on the content type. Note that this is just an illustration of a concept which can be changed depending on the requirements.
Example
| Type | Paths |
|---|---|
| Node path | /content/node-title |
| URL | http://www.drupal.org |
| File path | /sites/all/files/this-file.pdf |
Create the content types
- Create three content types: News, News URL, News PDF
- For each content type, create the following fields:
- News: title, body
- News URL: fields: title, field_url (link field)
- News PDF: fields: title, field_pdf (file field)
Add Sample Contents
- News: Node Title (title), Some Text.. (body)
- News URL: URL Title (title), http://www.drupal.org (field_url)
- News PDF: PDF Title (title), upload a pdf file (field_pdf)
Configure Views
- Create a view, block or page
- Add the following fields: Content: Title, Content: URL, Content: PDF
- Add additional fields: Global: Custom Text, Content: Path
- Arrange the above fields to:
- Content: PDF
- Content: URL
- Content: Path
- Global: Custom Text
- Content: Title
Configure fields
- Content: PDF
- Exclude from display
- Formatter: URL to path
- check hide if empty
- Content: URL
- Exclude from display
- Formatter: URL, as plain text
- No Result Behavior put [field_pdf]
- Content: Path
- Exclude from display
- Rewrite Results, check Use absolute link (begins with "http://")
- Global: Custom Text
- Exclude from display
- Text: put [field_url]
- No Result Behavior put [path]
- Content: Title
- Uncheck Link this field to the original piece of content
- Rewrite Results: check Output this field as a link, on path, put [nothing], check Use absolute path
The output should be:
Help improve this page
Page status: No known problems
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion