drupal|April 04, 2020|3 min read

Drupal - How to add multiple form fields with a single button click (No Coding Required)

TL;DR

Use the Paragraphs module to group multiple fields together so users can add entire field sets with a single button click, no coding required.

Drupal - How to add multiple form fields with a single button click (No Coding Required)

Introduction

Drupal is an awesome CMS. Drupal content type form, allows you to add fields with unlimited occurence. This is how it used to happen already in drupal (Assumming you already having a content type)

  • Add a new field, Example text field
  • You configure it with some default value or help description text
  • Now, Drupal gives you the option to have multiple fields of same type in the form.
  • Under “Field Settings”, it asks for: Allowed Number of Values. You can set this to Unlimited.

Now, what this unlimited means is that when your form renders. It gives you a button which allows you to add more of this field on each click.

Now, lets take an example content type. Assume you are having a products review page. And, you would want to show multiple products. And for each product, you want to have 3 different fields:

  • Name
  • Description
  • Link

Lets take a look how this form will look like:

Drupal Multiple fields example

Problem

Now you see, for each of multi-value field, you are seeing a separate button for “Add More”. And, what we want is to have a set of fields and expand them separately by a single button click.

The Solution

Drupal has an awesome out of the box module for this kind of problem named: Paragraphs.

Installation of Paragraphs module

Lets install this module:

composer require drupal/paragraphs

It also installs a dependency module (Entity Reference Revisions). After this command, lets enable this from modules section in drupal.

Adding a new Paragraph Type

After this, goto: /admin/structure/paragraphs_type Note, we want those three fields to be treated as a set. And, a single button for adding that set multiple times.

In paragraph types, we will create a new Paragraph Type. It is almost similar to adding a new content type and its fields.

Drupal Paragraphs

Now, add those three fields in this paragraph type just like in a new content type. Most important, For each field in this paragraph type. Set its “Allowed Number of values” to just 1. Not unlimited.

Drupal Paragraphs

After adding all fields, it will look like:

Drupal Paragraphs

Edit your Content type

Now, go back to your content type and Manage fields. Assumming you don’t have any existing content for this content type.

  • Delete those three fields from this content type.
  • Click on Add fields
  • You will see a field Paragraph, under Reference Revisions
  • Give a name to this field.
  • Now, important setting here that you need to set “Allowed number of values” to “Unlimited”
  • In next page, it will ask you to select the paragraph type. In Paragraph Types, select your paragraph type you just created.

Finally, in your content type. You are left with:

  • Title
  • Body
  • Paragraph

Goto your add content page

And, you can see your expected form.

Drupal Paragraphs

Bonus

I hope you are getting the expected output. If you want your form to look more awesome. You can also install another module Field Groups. It will allow you to visibally group your set of fields. And, your form will look more real.

Summary

I hope above steps will solve your needs. I’m repeating above steps:

  • Install Paragraphs module
  • Add a new Paragraph type with your fields that you want as a set.
  • Set their maximum allowed value as 1
  • Add or edit your content type now.
  • Remove your old fields, that you just added them in paragraph type.
  • Add this paragraph as a field, and set maximum allowed value to be Unlimited.
  • Thats it.

I hope you enjoy reading it.

Related Posts

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 - Using Field Groups for Visually Separating multiple field set (No Coding Required)

Drupal - Using Field Groups for Visually Separating multiple field set (No Coding Required)

Introduction You are having a form having multiple fields. When you render a…

Drupal 8 - Advanced usage of Paragraphs module - Add nested set of fields and single Add more button (No Coding Required)

Drupal 8 - Advanced usage of Paragraphs module - Add nested set of fields and single Add more button (No Coding Required)

Introduction In my previous article, I explained How to have set of fields and…

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…

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…