I'm using a AJAX form. This form I build from a sub-class of ContentEntityForm
. The Entity edited by this form has a field MyField. To bring all the field values in a appropriate structure I'm using massageFormValues()
in MyFieldWidget
class (sub-class of WidgetBase
).
The little problem is: the method massageFormValues()
is called twice on every submit. The are no other problems with the form. I can properly edit values on this form.
So it's just the question of understanding: why Drupal does call massageFormValues()
twice on every submit.
The mentioned and my (very simplified) code:
My ajax controller
class AjaxController extends ControllerBase {
...
...
public function myController($eid) {
$myEntity = $this->entityTypeManager()->getStorage('my_entity')->load($eid);
$form = $this->entityFormBuilder()->getForm($myEntity, 'my_mode');
$renderer = \Drupal::service('renderer');
$output = $renderer->renderRoot($form);
$response = new AjaxResponse();
$response->addCommand(new OpenModalDialogCommand($title, $output));
return $response;
}
...
...
}
MyForm
is a sub-class of the ContentEntityForm and it's a mode form for the mode my_mode
of the Entity my_entity
:
class MyForm extends ContentEntityForm {
...
...
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit'] = [
'#type' => 'submit',
'#value' => t('Save'),
'#ajax' => [
'callback' => '::ajaxCallback',
'event' => 'mousedown',
],
];
return $actions;
}
...
...
}
My field widget class:
class MyFieldWidget extends WidgetBase implements ContainerFactoryPluginInterface {
...
...
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
// THIS CODE IS CALLED TWICE ON EVERY SUBMIT OF MY SUBMIT BUTTON DEFINED ABOVE
// BUT WHY TWICE?
return $rightStructureValues;
}
...
...
}