Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Increment or Decrement number of Fields Dynamically using Form API and Ajax

By editor , 28 March, 2018
Question

How to Increment or Decrement number of fields dynamically using Ajax Form API ?

In this example, we will use a field to get tags list, the number of tags dynamic.

1. Create a custom form (See the tutorial)

2. Add tags field

$form['tags'] = [
  '#type' => 'details',
  '#title' => "Tags",
  '#open' => TRUE,
  '#description' => "The description of the field.",
];
$form['tags']['addtag'] = [
  '#type' => 'submit',
  '#value' => t('Add one more'),
  '#submit' => ['::addOneTag'],
  '#weight' => 100,
  '#ajax' => [
    'callback' => '::updateTagCallback',
    'wrapper' => 'tagfields-wrapper',
    'method' => 'replace',
  ],
];
$form['tags']['remtag'] = [
  '#type' => 'submit',
  '#value' => t('Remove the last'),
  '#submit' => ['::remOneTag'],
  '#weight' => 100,
  '#ajax' => [
    'callback' => '::updateTagCallback',
    'wrapper' => 'tagfields-wrapper',
    'method' => 'replace',
  ],
];
$form['tags']['tag_values'] = [
  '#type' => 'container',
  '#tree' => TRUE,
  '#prefix' => '<div id="tagfields-wrapper">',
  '#suffix' => '</div>',
];
$number_of_tags = $form_state->get('number_of_tags');
if (empty($number_of_tags)) {
  $number_of_tags = 1;
  $form_state->set('number_of_tags', $number_of_tags);
}
for ($i = 0; $i < $number_of_tags; $i++) {
  $form['tags']['tag_values'][$i] = [
    '#type' => 'entity_autocomplete',
    '#target_type' => 'taxonomy_term',
  ];
}


3. Add Callback methods:

  /**
   * Increment number of tags.
   */
  public function addOneTag(array &$form, FormStateInterface $form_state) {
    $number_of_tags = $form_state->get('number_of_tags');
    $form_state->set('number_of_tags', $number_of_tags + 1);
    $form_state->setRebuild(TRUE);
  }
  /**
   * Decrement number of tags.
   */
  public function remOneTag(array &$form, FormStateInterface $form_state) {
    $number_of_tags = $form_state->get('number_of_tags');
    $form_state->set('number_of_tags', $number_of_tags - 1);
    $form_state->setRebuild(TRUE);
  }
  /**
   * Return the tag list (Form)
   */
  public function updateTagCallback(array &$form, FormStateInterface $form_state) {
    return $form['tags']['tag_values'];
  }


Now you can add or remove tag fields dynamically

Tags

  • Drupal 8
  • Code
  • Form
  • JavaScript
  • Add new comment
Create a Form with Drupal 8 using Form API
Ajax Form
Add Remove Fields Dynamically using Form API and Ajax

Comments2

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

RichardRoger (not verified)

8 years 4 months ago

RE:

In view of the "Add-more button" case accessible in Drupal examples module I have made a shape that can include fields progressively utilizing AJAX, and now I need to add a different Remove catch to every one of those fields so I can evacuate singular fields. Custom Essay Writing Service

  • Reply

tony (not verified)

8 years 4 months ago

post

These are incredible drills and sources which has some genuine good amount of things I have been being asked the several times about many different things which are genuinely a good thing to see like this as do my essay for me have helped me by being here.

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer