drupal|April 11, 2020|1 min read

Drupal 8 Views - How to merge two fields by hiding another field

TL;DR

Use the 'Rewrite results' option in Views to combine two fields using token replacement, then exclude the secondary field from display.

Drupal 8 Views - How to merge two fields by hiding another field

Introduction

You have a view with 4-5 fields to display. Suppose, there are two fields: payment amount and currency. Where currency can be INR and USD. I want to show fields in a table, don’t want to show these two fields as separate.

I want to show these two fields as one. Example:

ID   Name   Amount
 1   ABC    10 USD
 2   XYZ    700 INR

I hope you get the above example, otherwise if the two fields will be separate. I will have 4 fields in above table.

Views Settings

Lets see how we can configure our view.

  • Goto your view
  • Goto fields settings
  • Click on the field you dont want to display as a separate field. In my example, currency is the field
  • Check on “Exclude from display”. This will be hidden now
  • Now, Rearrange your fields
  • Move that hidden field at top. (This will allow us to show this field value as token in other field configuration)
  • Goto that field with which you want to append our hidden field. In my example, its Amount
  • Click on Rewrite results
  • Override the output of this field with custom text
  • You can see “Replacement Patterns”
  • You can see your hidden field there
  • Put following (my example):
{% raw %}
{{ amount }} {{ currency }}
{% endraw %}

Drupal merge fields

Related Posts

Drupal 8 - How to Theme Form and its Fields with reordering fields

Drupal 8 - How to Theme Form and its Fields with reordering fields

Introduction In this post, we will see how to theme form and its fields…

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Drupal 8 - How to create a Page with admin access and create its menu entry in Reports (No Coding)

Introduction I needed a report page, where I wanted to have some information…

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Drupal: How to detect country and redirect to country specific website by using Cloudflare

Introduction Assume you have a drupal website and using cloudflare. You are…

Drupal - How to rename column of a content type

Drupal - How to rename column of a content type

Introduction You already have a content type with one or more fields in it…

Drupal 8 - How to hide help link About text formats and text format guidelines

Drupal 8 - How to hide help link About text formats and text format guidelines

Problem In drupal textarea field, it was always a pain to see the two links…

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Drupal 8 Smart Image Style - Handle aspect ratio for small and long images

Problem Statement I’ve been using image styles, and heavily used “Scale and crop…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…