Updated all the contrib modules to their latest versions.
[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', 'edit' or 'duplicate'.
51       '#op' => NULL,
52       '#process' => [
53         // Core's #process for groups, don't remove it.
54         [$class, 'processGroup'],
55
56         // InlineEntityForm's #process must run after the above ::processGroup
57         // in case any new elements (like groups) were added in alter hooks.
58         [$class, 'processEntityForm'],
59       ],
60       '#element_validate' => [
61         [$class, 'validateEntityForm'],
62       ],
63       '#ief_element_submit' => [
64         [$class, 'submitEntityForm'],
65       ],
66       '#theme_wrappers' => ['container'],
67
68       '#pre_render' => [
69         // Core's #pre_render for groups, don't remove it.
70         [$class, 'preRenderGroup'],
71       ],
72     ];
73   }
74
75   /**
76    * Builds the entity form using the inline form handler.
77    *
78    * @param array $entity_form
79    *   The entity form.
80    * @param \Drupal\Core\Form\FormStateInterface $form_state
81    *   The current state of the form.
82    * @param array $complete_form
83    *   The complete form structure.
84    *
85    * @throws \InvalidArgumentException
86    *   Thrown when the #entity_type or #bundle properties are empty, or when
87    *   the #default_value property is not an entity.
88    *
89    * @return array
90    *   The built entity form.
91    */
92   public static function processEntityForm($entity_form, FormStateInterface $form_state, &$complete_form) {
93     if (empty($entity_form['#entity_type'])) {
94       throw new \InvalidArgumentException('The inline_entity_form element requires the #entity_type property.');
95     }
96     if (isset($entity_form['#default_value']) && !($entity_form['#default_value'] instanceof EntityInterface)) {
97       throw new \InvalidArgumentException('The inline_entity_form #default_value property must be an entity object.');
98     }
99
100     if (empty($entity_form['#ief_id'])) {
101       $entity_form['#ief_id'] = \Drupal::service('uuid')->generate();
102     }
103     if (isset($entity_form['#default_value'])) {
104       // Transfer the #default_value to #entity, as expected by inline forms.
105       $entity_form['#entity'] = $entity_form['#default_value'];
106     }
107     else {
108       // This is an add operation, create a new entity.
109       $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_form['#entity_type']);
110       $storage = \Drupal::entityTypeManager()->getStorage($entity_form['#entity_type']);
111       $values = [];
112       if ($langcode_key = $entity_type->getKey('langcode')) {
113         if (!empty($entity_form['#langcode'])) {
114           $values[$langcode_key] = $entity_form['#langcode'];
115         }
116       }
117       if ($bundle_key = $entity_type->getKey('bundle')) {
118         $values[$bundle_key] = $entity_form['#bundle'];
119       }
120       $entity_form['#entity'] = $storage->create($values);
121     }
122     if (!isset($entity_form['#op'])) {
123       // When duplicating entities, the entity is new, but already has a UUID.
124       if ($entity_form['#entity']->isNew() && $entity_form['#entity']->uuid()) {
125         $entity_form['#op'] = 'duplicate';
126       }
127       else {
128         $entity_form['#op'] = $entity_form['#entity']->isNew() ? 'add' : 'edit';
129       }
130     }
131     // Prepare the entity form and the entity itself for translating.
132     $entity_form['#entity'] = TranslationHelper::prepareEntity($entity_form['#entity'], $form_state);
133     $entity_form['#translating'] = TranslationHelper::isTranslating($form_state) && $entity_form['#entity']->isTranslatable();
134
135     $inline_form_handler = static::getInlineFormHandler($entity_form['#entity_type']);
136     $entity_form = $inline_form_handler->entityForm($entity_form, $form_state);
137     // The form element can't rely on inline_entity_form_form_alter() calling
138     // ElementSubmit::attach() since form alters run before #process callbacks.
139     ElementSubmit::attach($complete_form, $form_state);
140
141     return $entity_form;
142   }
143
144   /**
145    * Validates the entity form using the inline form handler.
146    *
147    * @param array $entity_form
148    *   The entity form.
149    * @param \Drupal\Core\Form\FormStateInterface $form_state
150    *   The current state of the form.
151    */
152   public static function validateEntityForm(&$entity_form, FormStateInterface $form_state) {
153     $inline_form_handler = static::getInlineFormHandler($entity_form['#entity_type']);
154     $inline_form_handler->entityFormValidate($entity_form, $form_state);
155   }
156
157   /**
158    * Handles the submission of the entity form using the inline form handler.
159    *
160    * @param array $entity_form
161    *   The entity form.
162    * @param \Drupal\Core\Form\FormStateInterface $form_state
163    *   The current state of the form.
164    */
165   public static function submitEntityForm(&$entity_form, FormStateInterface $form_state) {
166     $inline_form_handler = static::getInlineFormHandler($entity_form['#entity_type']);
167     $inline_form_handler->entityFormSubmit($entity_form, $form_state);
168     if ($entity_form['#save_entity']) {
169       $inline_form_handler->save($entity_form['#entity']);
170     }
171   }
172
173   /**
174    * Gets the inline form handler for the given entity type.
175    *
176    * @param string $entity_type
177    *   The entity type id.
178    *
179    * @throws \InvalidArgumentException
180    *   Thrown when the entity type has no inline form handler defined.
181    *
182    * @return \Drupal\inline_entity_form\InlineFormInterface
183    *   The inline form handler.
184    */
185   public static function getInlineFormHandler($entity_type) {
186     $inline_form_handler = \Drupal::entityTypeManager()->getHandler($entity_type, 'inline_form');
187     if (empty($inline_form_handler)) {
188       throw new \InvalidArgumentException(sprintf('The %s entity type has no inline form handler.', $entity_type));
189     }
190
191     return $inline_form_handler;
192   }
193
194 }