Remove an item from $form_state in an ajax callback

I’m using this form api example: AjaxAddMore. I want to add a “remove button” for each indiviaul element, in order to remove an item but not the latest. Thouse are my changes:

...   $form['names_fieldset'] = [       '#type' => 'fieldset',       '#title' => $this->t('People coming to picnic'),       '#prefix' => '<div id="names-fieldset-wrapper">',       '#suffix' => '</div>',       '#attributes' => array('class'=>array('container-inline')),     ];      for ($i = 0; $i < $num_names; $i++) {        $form['names_fieldset']['fields'][$i] = [         '#type' => 'fieldset',         '#name' => 'id_'.$i       ];        $form['names_fieldset']['fields'][$i]['ids'] = [         '#type' => 'textfield',         '#title' => $this->t('ID'),         '#default_value' => $i,         '#name' => 'id_'.$i       ];        $form['names_fieldset']['fields'][$i]['name'] = [         '#type' => 'textfield',         '#title' => $this->t('Name')       ];        $form['names_fieldset']['fields'][$i]['remove'] = [         '#type' => 'submit',         '#name' => 'remove_'.$i,         '#value' => $this->t('Remove this'),         '#submit' => ['::removethisCallback'],         '#ajax' => [           'callback' => '::addmoreCallback',           'wrapper' => 'names-fieldset-wrapper',         ],       ];     } .... 

It works good and I can add several records. But I can’t remove an item other than the latest one. This is my callback function where I’ve tried to unset the item I want to remove:

public function removethisCallback(array &$form, FormStateInterface $form_state) {     $button_clicked = $form_state->getTriggeringElement()['#name'];     $id = substr($button_clicked,7);     $values = $form_state->getValue(['names_fieldset']);     unset($values[$id]);     $form_state->setValues(['names_fieldset'], array_values($values));        $name_field = $form_state->get('num_names');     if ($name_field > 1) {         $remove_button = $name_field - 1;         $form_state->set('num_names', $remove_button);     }        $form_state->setRebuild(); } 

But It doesn’t work. How could I remove or update an item from $form_state?

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Remove an item from $form_state in an ajax callback

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.