Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / inline_entity_form / src / Element / InlineEntityForm.php
1 <?php
2
3 namespace Drupal\inline_entity_form\Element;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Render\Element;
8 use Drupal\Core\Render\Element\RenderElement;
9 use Drupal\inline_entity_form\ElementSubmit;
10 use Drupal\inline_entity_form\TranslationHelper;
11
12 /**
13  * Provides an inline entity form element.
14  *
15  * Usage example:
16  * @code
17  * $form['article'] = [
18  *   '#type' => 'inline_entity_form',
19  *   '#entity_type' => 'node',
20  *   '#bundle' => 'article',
21  *   // If the #default_value is NULL, a new entity will be created.
22  *   '#default_value' => $loaded_article,
23  * ];
24  * @endcode
25  * To access the entity in validation or submission callbacks, use
26  * $form['article']['#entity']. Due to Drupal core limitations the entity
27  * can't be accessed via $form_state->getValue('article').
28  *
29  * @RenderElement("inline_entity_form")
30  */
31 class InlineEntityForm extends RenderElement {
32
33   /**
34    * {@inheritdoc}
35    */
36   public function getInfo() {
37     $class = get_class($this);
38     return [
39       '#ief_id' => '',
40       '#entity_type' => NULL,
41       '#bundle' => NULL,
42       '#langcode' => NULL,
43       // Instance of \Drupal\Core\Entity\EntityInterface. If NULL, a new
44       // entity will be created.
45       '#default_value' => NULL,
46       // The form mode used to display the entity form.
47       '#form_mode' => 'default',
48       // Will save entity on submit if set to TRUE.
49       '#save_entity' => TRUE,
50       // 'add' or 'edit'. If NULL, determined by whether the entity is new.
51       '#op' => NULL,
52       '#process' => [
53         [$class, 'processEntityForm'],
54       ],
55       '#element_validate' => [
56         [$class, 'validateEntityForm'],
57       ],
58       '#ief_element_submit' => [
59         [$class, 'submitEntityForm'],
60       ],
61       '#theme_wrappers' => ['container'],
62       // Allow inline forms to use the #fieldset key.
63       '#pre_render' => [
64         [$class, 'addFieldsetMarkup'],
65       ],
66     ];
67   }
68
69   /**
70    * Builds the entity form using the inline form handler.
71    *
72    * @param array $entity_form
73    *   The entity form.
74    * @param \Drupal\Core\Form\FormStateInterface $form_state
75    *   The current state of the form.
76    * @param array $complete_form
77    *   The complete form structure.
78    *
79    * @throws \InvalidArgumentException
80    *   Thrown when the #entity_type or #bundle properties are empty, or when
81    *   the #default_value property is not an entity.
82    *
83    * @return array
84    *   The built entity form.
85    */
86   public static function processEntityForm($entity_form, FormStateInterface $form_state, &$complete_form) {
87     if (empty($entity_form['#entity_type'])) {
88       throw new \InvalidArgumentException('The inline_entity_form element requires the #entity_type property.');
89     }
90     if (isset($entity_form['#default_value']) && !($entity_form['#default_value'] instanceof EntityInterface)) {
91       throw new \InvalidArgumentException('The inline_entity_form #default_value property must be an entity object.');
92     }
93
94     if (empty($entity_form['#ief_id'])) {
95       $entity_form['#ief_id'] = \Drupal::service('uuid')->generate();
96     }
97     if (isset($entity_form['#default_value'])) {
98       // Transfer the #default_value to #entity, as expected by inline forms.
99       $entity_form['#entity'] = $entity_form['#default_value'];
100     }
101     else {
102       // This is an add operation, create a new entity.
103       $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_form['#entity_type']);
104       $storage = \Drupal::entityTypeManager()->getStorage($entity_form['#entity_type']);
105       $values = [];
106       if ($langcode_key = $entity_type->getKey('langcode')) {
107         if (!empty($entity_form['#langcode'])) {
108           $values[$langcode_key] = $entity_form['#langcode'];
109         }
110       }
111       if ($bundle_key = $entity_type->getKey('bundle')) {
112         $values[$bundle_key] = $entity_form['#bundle'];
113       }
114       $entity_form['#entity'] = $storage->create($values);
115     }
116     if (!isset($entity_form['#op'])) {
117       $entity_form['#op'] = $entity_form['#entity']->isNew() ? 'add' : 'edit';
118     }
119     // Prepare the entity form and the entity itself for translating.
120     $entity_form['#entity'] = TranslationHelper::prepareEntity($entity_form['#entity'], $form_state);
121     $entity_form['#translating'] = TranslationHelper::isTranslating($form_state) && $entity_form['#entity']->isTranslatable();
122
123     $inline_form_handler = static::getInlineFormHandler($entity_form['#entity_type']);
124     $entity_form = $inline_form_handler->entityForm($entity_form, $form_state);
125     // The form element can't rely on inline_entity_form_form_alter() calling
126     // ElementSubmit::attach() since form alters run before #process callbacks.
127     ElementSubmit::attach($complete_form, $form_state);
128
129     return $entity_form;
130   }
131
132   /**
133    * Validates the entity form using the inline form handler.
134    *
135    * @param array $entity_form
136    *   The entity form.
137    * @param \Drupal\Core\Form\FormStateInterface $form_state
138    *   The current state of the form.
139    */
140   public static function validateEntityForm(&$entity_form, FormStateInterface $form_state) {
141     $inline_form_handler = static::getInlineFormHandler($entity_form['#entity_type']);
142     $inline_form_handler->entityFormValidate($entity_form, $form_state);
143   }
144
145   /**
146    * Handles the submission of the entity form using the inline form handler.
147    *
148    * @param array $entity_form
149    *   The entity form.
150    * @param \Drupal\Core\Form\FormStateInterface $form_state
151    *   The current state of the form.
152    */
153   public static function submitEntityForm(&$entity_form, FormStateInterface $form_state) {
154     $inline_form_handler = static::getInlineFormHandler($entity_form['#entity_type']);
155     $inline_form_handler->entityFormSubmit($entity_form, $form_state);
156     if ($entity_form['#save_entity']) {
157       $inline_form_handler->save($entity_form['#entity']);
158     }
159   }
160
161   /**
162    * Gets the inline form handler for the given entity type.
163    *
164    * @param string $entity_type
165    *   The entity type id.
166    *
167    * @throws \InvalidArgumentException
168    *   Thrown when the entity type has no inline form handler defined.
169    *
170    * @return \Drupal\inline_entity_form\InlineFormInterface
171    *   The inline form handler.
172    */
173   public static function getInlineFormHandler($entity_type) {
174     $inline_form_handler = \Drupal::entityTypeManager()->getHandler($entity_type, 'inline_form');
175     if (empty($inline_form_handler)) {
176       throw new \InvalidArgumentException(sprintf('The %s entity type has no inline form handler.', $entity_type));
177     }
178
179     return $inline_form_handler;
180   }
181
182   /**
183    * Pre-render callback for the #fieldset form property.
184    *
185    * Inline forms use #tree = TRUE to keep their values in a hierarchy for
186    * easier storage. Moving the form elements into fieldsets during form
187    * building would break up that hierarchy, so it's not an option for entity
188    * fields. Therefore, we wait until the pre_render stage, where any changes
189    * we make affect presentation only and aren't reflected in $form_state.
190    *
191    * @param array $entity_form
192    *   The entity form.
193    *
194    * @return array
195    *   The modified entity form.
196    */
197   public static function addFieldsetMarkup($entity_form) {
198     $sort = [];
199     foreach (Element::children($entity_form) as $key) {
200       $element = $entity_form[$key];
201       if (isset($element['#fieldset']) && isset($entity_form[$element['#fieldset']])) {
202         $entity_form[$element['#fieldset']][$key] = $element;
203         // Remove the original element this duplicates.
204         unset($entity_form[$key]);
205         // Mark the fieldset for sorting.
206         if (!in_array($key, $sort)) {
207           $sort[] = $element['#fieldset'];
208         }
209       }
210     }
211
212     // Sort all fieldsets, so that element #weight stays respected.
213     foreach ($sort as $key) {
214       uasort($entity_form[$key], '\Drupal\Component\Utility\SortArray::sortByWeightProperty');
215     }
216
217     return $entity_form;
218   }
219
220 }