Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / inline_entity_form / src / ElementSubmit.php
1 <?php
2
3 namespace Drupal\inline_entity_form;
4
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Render\Element;
8
9 /**
10  * Provides #ief_element_submit, the submit version of #element_validate.
11  *
12  * #ief_element_submit callbacks are invoked by a #submit callback added
13  * to the form's main submit button.
14  */
15 class ElementSubmit {
16
17   /**
18    * Attaches the #ief_element_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     // attach() is called for each IEF form element, but the callbacks only
27     // need to be added once per form build.
28     if (isset($form['#ief_element_submit_attached'])) {
29       return;
30     }
31     $form['#ief_element_submit_attached'] = TRUE;
32
33     // Entity form actions.
34     foreach (['submit', 'publish', 'unpublish'] as $action) {
35       if (!empty($form['actions'][$action])) {
36         self::addCallback($form['actions'][$action], $form);
37       }
38     }
39     // Generic submit button.
40     if (!empty($form['submit'])) {
41       self::addCallback($form['submit'], $form);
42     }
43   }
44
45   /**
46    * Adds the trigger callback to the given submit element.
47    *
48    * @param array $element
49    *   The submit element.
50    * @param array $complete_form
51    *   The complete form.
52    */
53   public static function addCallback(&$element, $complete_form) {
54     if (empty($element['#submit'])) {
55       // Drupal runs either the button-level callbacks or the form-level ones.
56       // Having no button-level callbacks indicates that the form has relied
57       // on form-level callbacks, which now need to be transferred.
58       $element['#submit'] = $complete_form['#submit'];
59     }
60
61     $element['#submit'] = array_merge([[get_called_class(), 'trigger']], $element['#submit']);
62     // Used to distinguish between an inline form submit and main form submit.
63     $element['#ief_submit_trigger']  = TRUE;
64     $element['#ief_submit_trigger_all'] = TRUE;
65   }
66
67   /**
68    * Button #submit callback: Triggers submission of element forms.
69    *
70    * @param array $form
71    *   The form.
72    * @param \Drupal\Core\Form\FormStateInterface $form_state
73    *   The form state.
74    */
75   public static function trigger($form, FormStateInterface $form_state) {
76     $triggered_element = $form_state->getTriggeringElement();
77     if (!empty($triggered_element['#ief_submit_trigger_all'])) {
78       // The parent form was submitted, process all IEFs and their children.
79       static::doSubmit($form, $form_state);
80     }
81     else {
82       // A specific element was submitted, process it and all of its children.
83       $array_parents = $triggered_element['#array_parents'];
84       $array_parents = array_slice($array_parents, 0, -2);
85       $element = NestedArray::getValue($form, $array_parents);
86       static::doSubmit($element, $form_state);
87     }
88   }
89
90   /**
91    * Submits elements by calling their #ief_element_submit callbacks.
92    *
93    * @param array $element
94    *   The element.
95    * @param \Drupal\Core\Form\FormStateInterface $form_state
96    *   The form state.
97    */
98   public static function doSubmit($element, FormStateInterface $form_state) {
99     // Recurse through all children.
100     foreach (Element::children($element) as $key) {
101       if (!empty($element[$key])) {
102         static::doSubmit($element[$key], $form_state);
103       }
104     }
105
106     // If there are callbacks on this level, run them.
107     if (!empty($element['#ief_element_submit'])) {
108       foreach ($element['#ief_element_submit'] as $callback) {
109         call_user_func_array($callback, [&$element, &$form_state]);
110       }
111     }
112   }
113
114 }