0

I have implemented hook_field_widget_form_alter() to add a new #process array item. E.g.

 hook_field_widget_form_alter() {
   $element['#process'][] = ['custom_process_function'];
 }

This part works fine to add in a new submit button that will save the contents of the current form send the user off to another page. E.g.\

custom_process_function(&$element) {
  $element['new_button'] = [
      '#type'                   => 'submit',
      '#value'                  => t('Upload new'),
      '#submit'                 => [
        [
          "Drupal\inline_entity_form\ElementSubmit",
          "trigger",
        ],
        [
          AddNewSubmitHandler::class,
          'handle',
        ],
      ],
      '#trigger'                => $this->fieldName,
      '#name'                   => $this->fieldName . '_add_new' . '_' . md5(json_encode($this->fieldParents)),
      '#ief_submit_trigger'     => TRUE,
      '#ief_submit_trigger_all' => TRUE,
      '#referenceType'          => $this->fieldReferenceType,
      '#referenceBundles'       => $this->fieldReferenceBundles,
      '#referenceEntityType'    => $this->entityType,
      '#referenceEntityBundle'  => $this->bundle,
      '#allowedFileTypes'       => $this->allowedFileTypes,
      '#validate'               => ['_custom_validate_function'],
    ]; 
} 

However, the problem lies when it is loaded into Inline Entity Form field and then an ajax event is triggered. In my case, I am using an Entity Browser field within this form and when I click the Remove button and then click my custom button, the $formState->getTriggeringElement() is set to the next submit button that was on the original form, not even the next one in the Inline Entity Form.

Can anyone shed any light on why this is happening or how to fix it?

1 Answer 1

0

The inline entity form element is not rendered at the moment the triggering element is being set to the $form_state. See \Drupal\Core\Form\FormBuilder::doBuildForm for more details.

  // If a form contains a single textfield, and the ENTER key is pressed
  // within it, Internet Explorer submits the form with no POST data
  // identifying any submit button. Other browsers submit POST data as
  // though the user clicked the first button. Therefore, to be as
  // consistent as we can be across browsers, if no 'triggering_element' has
  // been identified yet, default it to the first button.
  $buttons = $form_state->getButtons();
  if (!$form_state->isProgrammed() && !$form_state->getTriggeringElement() && !empty($buttons)) {
    $form_state->setTriggeringElement($buttons[0]);
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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