diff --git a/src/Element/InlineEntityForm.php b/src/Element/InlineEntityForm.php index b636edc..8537089 100644 --- a/src/Element/InlineEntityForm.php +++ b/src/Element/InlineEntityForm.php @@ -40,7 +40,6 @@ class InlineEntityForm extends RenderElement { '#display_actions' => FALSE, // Will save entity on submit if set to TRUE. '#save_entity' => TRUE, - '#ief_element_validate' => [], '#ief_element_submit' => [], // Needs to be set to FALSE if one wants to implement it's own submit logic. '#handle_submit' => TRUE, @@ -128,8 +127,7 @@ class InlineEntityForm extends RenderElement { // Attach submit callbacks to main submit buttons. if ($element['#handle_submit']) { - static::attachMainSubmit($complete_form, '#submit'); - static::attachMainSubmit($complete_form, '#validate'); + static::attachMainSubmit($complete_form); } return $element; @@ -141,20 +139,16 @@ class InlineEntityForm extends RenderElement { * @param array $complete_form * Form structure. */ - public static function attachMainSubmit(&$complete_form, $type) { + public static function attachMainSubmit(&$complete_form) { $submit_attached = FALSE; - - $trigger_callback = $type == '#submit' ? 'triggerIefSubmit' : 'triggerIefValidate'; - - $submit = array_merge([[get_called_class(), $trigger_callback]], $complete_form[$type]); - + $submit = array_merge([[get_called_class(), 'triggerIefSubmit']], $complete_form['#submit']); if (!empty($complete_form['submit'])) { - if (empty($complete_form['submit'][$type])) { - $complete_form['submit'][$type] = $submit; + if (empty($complete_form['submit']['#submit'])) { + $complete_form['submit']['#submit'] = $submit; } else { - $complete_form['submit'][$type] = array_merge([[get_called_class(), $trigger_callback]], $complete_form['submit'][$type]); + $complete_form['submit']['#submit'] = array_merge([[get_called_class(), 'triggerIefSubmit']], $complete_form['submit']['#submit']); } $complete_form['submit']['#ief_submit_all'] = TRUE; $submit_attached = TRUE; @@ -162,11 +156,11 @@ class InlineEntityForm extends RenderElement { foreach (['submit', 'publish', 'unpublish'] as $action) { if (!empty($complete_form['actions'][$action])) { - if (empty($complete_form['actions'][$action][$type])) { - $complete_form['actions'][$action][$type] = $submit; + if (empty($complete_form['actions'][$action]['#submit'])) { + $complete_form['actions'][$action]['#submit'] = $submit; } else { - $complete_form['actions'][$action][$type] = array_merge([[get_called_class(), $trigger_callback]], $complete_form['actions'][$action][$type]); + $complete_form['actions'][$action]['#submit'] = array_merge([[get_called_class(), 'triggerIefSubmit']], $complete_form['actions'][$action]['#submit']); } $complete_form['actions'][$action]['#ief_submit_all'] = TRUE; $submit_attached = TRUE; @@ -176,7 +170,7 @@ class InlineEntityForm extends RenderElement { // If we didn't attach submit to one of the most common buttons let's search // the form for any submit with #button_type == primary and attach to that. if (!$submit_attached) { - static::recurseAttachMainSubmit($complete_form, $submit, $type); + static::recurseAttachMainSubmit($complete_form, $submit); } } @@ -195,14 +189,14 @@ class InlineEntityForm extends RenderElement { * @return bool * TRUE if appropriate element was found. FALSE otherwise. */ - public static function recurseAttachMainSubmit(&$element, $submit_callbacks, $type) { + public static function recurseAttachMainSubmit(&$element, $submit_callbacks) { foreach (Element::children($element) as $child) { if (!empty($element[$child]['#type']) && $element[$child]['#type'] == 'submit' && $element[$child]['#button_type'] == 'preview') { - $element[$child][$type] = empty($element[$child]['#submit']) ? $submit_callbacks : array_merge($submit_callbacks, $element[$child][$type]); + $element[$child]['#submit'] = empty($element[$child]['#submit']) ? $submit_callbacks : array_merge($submit_callbacks, $element[$child]['#submit']); $element[$child]['#ief_submit_all'] = TRUE; return TRUE; } - elseif (static::recurseAttachMainSubmit($element[$child], $submit_callbacks, $type)) { + elseif (static::recurseAttachMainSubmit($element[$child], $submit_callbacks)) { return TRUE; } } @@ -232,21 +226,6 @@ class InlineEntityForm extends RenderElement { } } - public static function triggerIefValidate($form, FormStateInterface $form_state) { - $triggered_element = $form_state->getTriggeringElement(); - if (!empty($triggered_element['#ief_submit_all'])) { - // The parent form was submitted, process all IEFs and their children. - static::iefValidate($form, $form_state); - } - else { - // A specific entity form was submitted, process it and all of its children. - $array_parents = $triggered_element['#array_parents']; - $array_parents = array_slice($array_parents, 0, -2); - $element = NestedArray::getValue($form, $array_parents); - static::iefValidate($element, $form_state); - } - } - /** * Submits entity forms by calling their #ief_element_submit callbacks. * @@ -273,20 +252,4 @@ class InlineEntityForm extends RenderElement { } } - public static function iefValidate($elements, FormStateInterface $form_state) { - // Recurse through all children. - foreach (Element::children($elements) as $key) { - if (!empty($elements[$key])) { - static::iefValidate($elements[$key], $form_state); - } - } - - // If there are callbacks on this level, run them. - if (!empty($elements['#ief_element_validate'])) { - foreach ($elements['#ief_element_validate'] as $function) { - $function($elements, $form_state); - } - } - } - } diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php b/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php index 8f91545..43e173a 100644 --- a/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormBase.php @@ -314,12 +314,8 @@ abstract class InlineEntityFormBase extends WidgetBase implements ContainerFacto * @param array $element * Form array structure. */ - public static function addIefValidateCallbacks($element) { - $element['#ief_element_validate'][] = [get_called_class(), 'validateSaveEntity']; - return $element; - } - public static function addIefSubmitCallbacks($element) { + $element['#element_validate'][] = [get_called_class(), 'validateSaveEntity']; $element['#ief_element_submit'][] = [get_called_class(), 'submitSaveEntity']; return $element; } @@ -372,6 +368,7 @@ abstract class InlineEntityFormBase extends WidgetBase implements ContainerFacto */ public static function validateSaveEntity($entity_form, FormStateInterface $form_state) { $ief_id = $entity_form['#ief_id']; + /** @var \Drupal\Core\Entity\EntityInterface $entity */ $entity = $entity_form['#entity']; if ($entity_form['#op'] == 'add') { diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php index 0d83dd9..c8ebc0d 100644 --- a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php @@ -318,7 +318,7 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF $element['entities'][$key]['form']['inline_entity_form']['#process'] = [ ['\Drupal\inline_entity_form\Element\InlineEntityForm', 'processEntityForm'], - [get_class($this), 'addIefValidateCallbacks'], + [get_class($this), 'addIefSubmitCallbacks'], [get_class($this), 'buildEntityFormActions'], ]; } @@ -515,7 +515,6 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF ]; $element['form']['inline_entity_form']['#process'] = [ ['\Drupal\inline_entity_form\Element\InlineEntityForm', 'processEntityForm'], - [get_class($this), 'addIefValidateCallbacks'], [get_class($this), 'addIefSubmitCallbacks'], [get_class($this), 'buildEntityFormActions'], ];