~/drupal/drupal-how-add-multiple-fields-single-button.md

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

3 min readApril 04, 2020by Gorav Singal#drupal#drupal-8#forms
// 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.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

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 the Paragraphs Module

Drupal 8 - Advanced Usage of the Paragraphs Module

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

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…