I have a pretty complex process that combines two Drupal forms--1 widget form and 1 normal form.
The first form (WidgetForm that extends the ImageWidget form) will allow the user to open a modal to view some images, like Entity Browser but populated via 3rd party API. I render the form using the following snippet:
$modal_form = \Drupal::formBuilder()->getForm('Drupal\my_module\Form\ModalForm');
Simple enough, our extended WidgetForm adds a button to 'Browse' images and if clicked, we get a render array and I shove that into an OpenModalDialogCommand and return that so it opens a modal with my form inside. This works no problem.
The ModalForm form allows the user to browse through images, paginate them, search, etc. However, when I want to update my WidgetForm, i'm having difficulty rebuilding the $form_state. Thus, when I submit the form, it is using an outdated $form_state and doesn't recognize that I've tried to manually insert values into the $form array. I know the following is sufficient data b/c I can populate it on the initial form build with session variables (but the UX is completely messed up if I rely on this process). But since I want this to be all within the same page request, I don't want to rely on refreshing the page.
$element['#value']['fids'] = $fid; $element['#value']['target_id'] = $fid; $element['#display_default'] = TRUE; $element['fids']['#value'] = [ $fid, ];
Above, we insert the $fid into some form fields. If we do this on the initial page load, it works fine (as I mentioned above). When I try to do this in my ajax callbacks, it doesn't update the $form_state. So I've tried to save it as session variables and set
$form_state->setRebuild(TRUE) in the validate/submit callbacks so that it will call my build step again which has logic to check for session variables. If I set
$form_state->setRebuild(TRUE) anywhere in my ModalForm, it will only rebuild the $form_state for my ModalForm, not my WidgetForm. So we arrive at my problem--I cannot seem to find a way to update the $form_state of my WidgetForm.
I've also tried to save the pure $form_state object as a session variable and then update it when necessary but this is not working.
// At the end of my formElement() function on my field widget form $session = new Session(); // Symfony session $session->set('my_module.image_widget_form_state', $form_state); // save the $form_state object // Later in my other form... $image_widget_form_state = $session->get('my_module.image_widget_form_state'); $image_widget_form_state->setValue('fids', [$fid]); // attempt to update $form_state at a later stage from within a different form
So it seems my problem is that I'm trying to update the $form_state of an active form within an ajax callback propagated by a completely different form. In other words, I need to update the $form_state of my WidgetForm from within my ModalForm. Both forms can be 'active' at the same time but ultimately, the WidgetForm needs to be dynamically updated based on actions taken from within the ModalForm.
Also related but not necessarily the main point of this question--despite my best efforts to rebuild the form on pertinent steps, it seems that I'm running into the bug where the #name property disappears and then the Form thinks that every single button is a submit button (when most of the buttons are simply a means to trigger an ajax callback). Would anyone know how to prevent this from happening?