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', 'edit' or 'duplicate'.
53 // Core's #process for groups, don't remove it.
54 [$class, 'processGroup'],
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'],
60 '#element_validate' => [
61 [$class, 'validateEntityForm'],
63 '#ief_element_submit' => [
64 [$class, 'submitEntityForm'],
66 '#theme_wrappers' => ['container'],
69 // Core's #pre_render for groups, don't remove it.
70 [$class, 'preRenderGroup'],
76 * Builds the entity form using the inline form handler.
78 * @param array $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.
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.
90 * The built entity form.
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.');
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.');
100 if (empty($entity_form['#ief_id'])) {
101 $entity_form['#ief_id'] = \Drupal::service('uuid')->generate();
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'];
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']);
112 if ($langcode_key = $entity_type->getKey('langcode')) {
113 if (!empty($entity_form['#langcode'])) {
114 $values[$langcode_key] = $entity_form['#langcode'];
117 if ($bundle_key = $entity_type->getKey('bundle')) {
118 $values[$bundle_key] = $entity_form['#bundle'];
120 $entity_form['#entity'] = $storage->create($values);
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';
128 $entity_form['#op'] = $entity_form['#entity']->isNew() ? 'add' : 'edit';
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();
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);
145 * Validates the entity form using the inline form handler.
147 * @param array $entity_form
149 * @param \Drupal\Core\Form\FormStateInterface $form_state
150 * The current state of the form.
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);
158 * Handles the submission of the entity form using the inline form handler.
160 * @param array $entity_form
162 * @param \Drupal\Core\Form\FormStateInterface $form_state
163 * The current state of the form.
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']);
174 * Gets the inline form handler for the given entity type.
176 * @param string $entity_type
177 * The entity type id.
179 * @throws \InvalidArgumentException
180 * Thrown when the entity type has no inline form handler defined.
182 * @return \Drupal\inline_entity_form\InlineFormInterface
183 * The inline form handler.
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));
191 return $inline_form_handler;