0

I am altering the field settigns form, in order to add custom settings. I used the example module as a guide and added my code in the following manner:

/**
 * Implements hook_form_FORM_ID_alter() for 'field_config_edit_form'.
 */
function fd_image_form_field_config_edit_form_alter(array &$form, FormStateInterface $form_state) {


  // Gather the number of names in the form already.
  $num_names = $form_state->get('num_names');
  // We have to ensure that there is at least one name field.
  if ($num_names === NULL) {
    $name_field = $form_state->set('num_names', 1);
    $num_names = 1;
  }

  $form['#tree'] = TRUE;
  $form['names_fieldset'] = [
    '#type' => 'fieldset',
    '#title' => t('People coming to picnic'),
    '#prefix' => '<div id="names-fieldset-wrapper">',
    '#suffix' => '</div>',
  ];

  for ($i = 0; $i < $num_names; $i++) {
    $form['names_fieldset']['name'][$i] = [
      '#type' => 'textfield',
      '#title' => t('Name'),
    ];
  }

  $form['names_fieldset']['actions'] = [
    '#type' => 'actions',
  ];
  $form['names_fieldset']['actions']['add_name'] = [
    '#type' => 'submit',
    '#value' => t('Add one more'),
    '#submit' => ['_fd_image_add_one'],
    '#ajax' => [
      'callback' => '_fd_image_add_more_callback',
      'wrapper' => 'names-fieldset-wrapper',
    ],
  ];
  // If there is more than one name, add the remove button.
  if ($num_names >= 1) {
    $form['names_fieldset']['actions']['remove_name'] = [
      '#type' => 'submit',
      '#value' => t('Remove one'),
      '#submit' => ['_fd_image_remove_callback'],
      '#ajax' => [
        'callback' => '_fd_image_add_more_callback',
        'wrapper' => 'names-fieldset-wrapper',
      ],
    ];

    $form_state->setCached(FALSE);
  }
}

I also added my ajax callbacks:

/**
 * Callback for both ajax-enabled buttons.
 *
 * Selects and returns the fieldset with the names in it.
 */
function _fd_image_add_more_callback(array &$form, FormStateInterface $form_state) {
  return $form['names_fieldset'];
}

/**
 * Submit handler for the "add-one-more" button.
 *
 * Increments the max counter and causes a rebuild.
 */
function _fd_image_add_one(array &$form, FormStateInterface $form_state) {
  $name_field = $form_state->get('num_names');
  $add_button = $name_field + 1;
  $form_state->set('num_names', $add_button);
  // Since our buildForm() method relies on the value of 'num_names' to
  // generate 'name' form elements, we have to tell the form to rebuild. If we
  // don't do this, the form builder will not call buildForm().
  $form_state->setRebuild();
}

/**
 * Submit handler for the "remove one" button.
 *
 * Decrements the max counter and causes a form rebuild.
 */
function _fd_image_remove_callback(array &$form, FormStateInterface $form_state) {
  $name_field = $form_state->get('num_names');
  if ($name_field > 1) {
    $remove_button = $name_field - 1;
    $form_state->set('num_names', $remove_button);
  }
  // Since our buildForm() method relies on the value of 'num_names' to
  // generate 'name' form elements, we have to tell the form to rebuild. If we
  // don't do this, the form builder will not call buildForm().
  $form_state->setRebuild();
}

Now when I go to the field settings form, I see my form elements and I do see the add more button. I can click the add more button 1 time and it adds an extra element, but the problem is, I cannot add more than 2. Once the page have 2 elements subsequents clicks on "add more" result in an ajax callback that does nothing.

After debugging it looks like the $num_names is not being correctly stored and increased. I am assuming that is because the Examples module has it's own buildFrom method while in my case I am only altering. What is the correct way of adding an ajax element to an existing form?

  • Try to replace set method with setValue, that might get your code running – Alireza Tabatabaeian Oct 17 '18 at 0:17
  • removing ` $form_state->setCached(FALSE);` fixed the issue. Not sure if it has other ramifications tho. I will submit this as an answer to my question soon. thanks anyway. – awm Oct 17 '18 at 20:05
0

Answering my own question after resolving the issue. Removing $form_state->setCached(FALSE); fixed the issue.

However, I am not sure why it did or whether this have negative consequences. Hope this helps others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.