3 namespace Drupal\inline_entity_form;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\inline_entity_form\Element\InlineEntityForm;
9 * Performs widget submission.
11 * Widgets don't save changed entities, nor do they delete removed entities.
12 * Instead, they flag them so that changes are only applied when the main form
18 * Attaches the widget submit functionality to the given form.
22 * @param \Drupal\Core\Form\FormStateInterface $form_state
25 public static function attach(&$form, FormStateInterface $form_state) {
26 // $form['#ief_element_submit'] runs after the #ief_element_submit
27 // callbacks of all subelements, which means that doSubmit() has
28 // access to the final IEF $form_state.
29 $form['#ief_element_submit'][] = [get_called_class(), 'doSubmit'];
33 * Submits the widget elements, saving and deleted entities where needed.
37 * @param \Drupal\Core\Form\FormStateInterface $form_state
40 public static function doSubmit(array $form, FormStateInterface $form_state) {
41 foreach ($form_state->get('inline_entity_form') as &$widget_state) {
42 $widget_state += ['entities' => [], 'delete' => []];
44 foreach ($widget_state['entities'] as $delta => $entity_item) {
45 if (!empty($entity_item['entity']) && !empty($entity_item['needs_save'])) {
46 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
47 $entity = $entity_item['entity'];
48 $handler = InlineEntityForm::getInlineFormHandler($entity->getEntityTypeId());
49 $handler->save($entity);
50 $widget_state['entities'][$delta]['needs_save'] = FALSE;
54 /** @var \Drupal\Core\Entity\ContentEntityInterface $entities */
55 foreach ($widget_state['delete'] as $entity) {
58 unset($widget_state['delete']);