Quantcast
Viewing all articles
Browse latest Browse all 426

How to build a simple conditional field in Views 7.x-3.x

It's possible to make conditional fields on views without using any additional module like views php.

In this example, we're going make the path for node title automatically link to node path, a url link and a file path, depending on the content type. Note: this is just an illustration of concept which can be changed depending on the requirements.

Example

TypePaths
node path/content/node-title
url pathhttp://www.drupal.org
file path /sites/all/files/this-file.pdf

Content Types

  1. Create 3 content types, ex. News, News URL, News PDF
  2. On News, fields: title, body
  3. On News URL, fields: title, field_url (link field)
  4. On News PDF, fields: title, field_pdf (file field)

Add Sample Contents

  1. On News: Node Title (title), Some Text.. (body)
  2. On News URL: URL Title (title), http://www.drupal.org (field_url)
  3. On News PDF: PDF Title (title), upload a pdf file (field_pdf)

Views Configuration

  1. Create a view, block or page
  2. Add the fields, Content: Title, Content: URL, Content: PDF
  3. Add additional fields, Global: Custom Text, Content: Path
  4. Arrange the above fields to:
    1. Content: PDF
    2. Content: URL
    3. Content: Path
    4. Global: Custom Text
    5. Content: Title

    Fields Configurations

    1. Content: PDF
      1. Exclude from display
      2. Formatter: URL to path
      3. check hide if empty
    2. Content: URL
      1. Exclude from display
      2. Formatter: URL, as plain text
      3. No Result Behavior, put [field_pdf]
    3. Content: Path
      1. Exclude from display
      2. Rewrite Results, check Use absolute link (begins with "http://")
    4. Global: Custom Text
      1. Exclude from display
      2. Text: put [field_url]
      3. No Result Behavior, put [path]
    5. Content: Title
      1. uncheck, Link this field to the original piece of content
      2. Rewrite Results: check Output this field as a link, on path, put [nothing], check Use absolute path

    The output should be:

    1. Node Title
    2. URL Title
    3. PDF Title

    Viewing all articles
    Browse latest Browse all 426

    Trending Articles