Added missing modules, including some as submodules.
[yaffs-website] / web / modules / contrib / inline_entity_form / src / WidgetSubmit.php
1 <?php
2
3 namespace Drupal\inline_entity_form;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\inline_entity_form\Element\InlineEntityForm;
7
8 /**
9  * Performs widget submission.
10  *
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
13  * is submitted.
14  */
15 class WidgetSubmit {
16
17   /**
18    * Attaches the widget submit functionality to the given form.
19    *
20    * @param array $form
21    *   The form.
22    * @param \Drupal\Core\Form\FormStateInterface $form_state
23    *   The form state.
24    */
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'];
30   }
31
32   /**
33    * Submits the widget elements, saving and deleted entities where needed.
34    *
35    * @param array $form
36    *   The form.
37    * @param \Drupal\Core\Form\FormStateInterface $form_state
38    *   The form state.
39    */
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' => []];
43
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;
51         }
52       }
53
54       /** @var \Drupal\Core\Entity\ContentEntityInterface $entities */
55       foreach ($widget_state['delete'] as $entity) {
56         $entity->delete();
57       }
58       unset($widget_state['delete']);
59     }
60   }
61
62 }