3 namespace Drupal\inline_entity_form\Element;
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;
13 * Provides an inline entity form element.
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,
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').
29 * @RenderElement("inline_entity_form")
31 class InlineEntityForm extends RenderElement {
36 public function getInfo() {
37 $class = get_class($this);
40 '#entity_type' => 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.
53 [$class, 'processEntityForm'],
55 '#element_validate' => [
56 [$class, 'validateEntityForm'],
58 '#ief_element_submit' => [
59 [$class, 'submitEntityForm'],
61 '#theme_wrappers' => ['container'],
62 // Allow inline forms to use the #fieldset key.
64 [$class, 'addFieldsetMarkup'],
70 * Builds the entity form using the inline form handler.
72 * @param array $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.
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.
84 * The built entity form.
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.');
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.');
94 if (empty($entity_form['#ief_id'])) {
95 $entity_form['#ief_id'] = \Drupal::service('uuid')->generate();
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'];
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']);
106 if ($langcode_key = $entity_type->getKey('langcode')) {
107 if (!empty($entity_form['#langcode'])) {
108 $values[$langcode_key] = $entity_form['#langcode'];
111 if ($bundle_key = $entity_type->getKey('bundle')) {
112 $values[$bundle_key] = $entity_form['#bundle'];
114 $entity_form['#entity'] = $storage->create($values);
116 if (!isset($entity_form['#op'])) {
117 $entity_form['#op'] = $entity_form['#entity']->isNew() ? 'add' : 'edit';
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();
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);
133 * Validates the entity form using the inline form handler.
135 * @param array $entity_form
137 * @param \Drupal\Core\Form\FormStateInterface $form_state
138 * The current state of the form.
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);
146 * Handles the submission of the entity form using the inline form handler.
148 * @param array $entity_form
150 * @param \Drupal\Core\Form\FormStateInterface $form_state
151 * The current state of the form.
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']);
162 * Gets the inline form handler for the given entity type.
164 * @param string $entity_type
165 * The entity type id.
167 * @throws \InvalidArgumentException
168 * Thrown when the entity type has no inline form handler defined.
170 * @return \Drupal\inline_entity_form\InlineFormInterface
171 * The inline form handler.
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));
179 return $inline_form_handler;
183 * Pre-render callback for the #fieldset form property.
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.
191 * @param array $entity_form
195 * The modified entity form.
197 public static function addFieldsetMarkup($entity_form) {
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'];
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');