3 namespace Drupal\inline_entity_form\Plugin\Field\FieldWidget;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
7 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Extension\ModuleHandlerInterface;
10 use Drupal\Core\Field\FieldDefinitionInterface;
11 use Drupal\Core\Field\FieldItemListInterface;
12 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
14 use Drupal\Core\Render\Element;
15 use Drupal\inline_entity_form\TranslationHelper;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
19 * Complex inline widget.
22 * id = "inline_entity_form_complex",
23 * label = @Translation("Inline entity form - Complex"),
27 * multiple_values = true
30 class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerFactoryPluginInterface {
33 * Module handler service.
35 * @var \Drupal\Core\Extension\ModuleHandlerInterface
37 protected $moduleHandler;
40 * Constructs a InlineEntityFormBase object.
42 * @param array $plugin_id
43 * The plugin_id for the widget.
44 * @param mixed $plugin_definition
45 * The plugin implementation definition.
46 * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
47 * The definition of the field to which the widget is associated.
48 * @param array $settings
49 * The widget settings.
50 * @param array $third_party_settings
51 * Any third party settings.
52 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
53 * The entity type bundle info.
54 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
55 * The entity type manager.
56 * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface
57 * The entity display repository.
58 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
59 * Module handler service.
61 public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager, EntityDisplayRepositoryInterface $entity_display_repository, ModuleHandlerInterface $module_handler) {
62 parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $entity_type_bundle_info, $entity_type_manager, $entity_display_repository);
63 $this->moduleHandler = $module_handler;
69 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
73 $configuration['field_definition'],
74 $configuration['settings'],
75 $configuration['third_party_settings'],
76 $container->get('entity_type.bundle.info'),
77 $container->get('entity_type.manager'),
78 $container->get('entity_display.repository'),
79 $container->get('module_handler')
86 public static function defaultSettings() {
87 $defaults = parent::defaultSettings();
90 'allow_existing' => FALSE,
91 'match_operator' => 'CONTAINS',
100 public function settingsForm(array $form, FormStateInterface $form_state) {
101 $element = parent::settingsForm($form, $form_state);
103 $labels = $this->getEntityTypeLabels();
104 $states_prefix = 'fields[' . $this->fieldDefinition->getName() . '][settings_edit_form][settings]';
105 $element['allow_new'] = [
106 '#type' => 'checkbox',
107 '#title' => $this->t('Allow users to add new @label.', ['@label' => $labels['plural']]),
108 '#default_value' => $this->getSetting('allow_new'),
110 $element['allow_existing'] = [
111 '#type' => 'checkbox',
112 '#title' => $this->t('Allow users to add existing @label.', ['@label' => $labels['plural']]),
113 '#default_value' => $this->getSetting('allow_existing'),
115 $element['match_operator'] = [
117 '#title' => $this->t('Autocomplete matching'),
118 '#default_value' => $this->getSetting('match_operator'),
119 '#options' => $this->getMatchOperatorOptions(),
120 '#description' => $this->t('Select the method used to collect autocomplete suggestions. Note that <em>Contains</em> can cause performance issues on sites with thousands of nodes.'),
123 ':input[name="' . $states_prefix . '[allow_existing]"]' => ['checked' => TRUE],
134 public function settingsSummary() {
135 $summary = parent::settingsSummary();
136 $labels = $this->getEntityTypeLabels();
138 if ($this->getSetting('allow_new')) {
139 $summary[] = $this->t('New @label can be added.', ['@label' => $labels['plural']]);
142 $summary[] = $this->t('New @label can not be created.', ['@label' => $labels['plural']]);
145 $match_operator_options = $this->getMatchOperatorOptions();
146 if ($this->getSetting('allow_existing')) {
147 $summary[] = $this->t('Existing @label can be referenced and are matched with the %operator operator.', [
148 '@label' => $labels['plural'],
149 '%operator' => $match_operator_options[$this->getSetting('match_operator')],
153 $summary[] = $this->t('Existing @label can not be referenced.', ['@label' => $labels['plural']]);
160 * Returns the options for the match operator.
165 protected function getMatchOperatorOptions() {
167 'STARTS_WITH' => $this->t('Starts with'),
168 'CONTAINS' => $this->t('Contains'),
175 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
176 $settings = $this->getSettings();
177 $target_type = $this->getFieldSetting('target_type');
178 // Get the entity type labels for the UI strings.
179 $labels = $this->getEntityTypeLabels();
181 // Build a parents array for this element's values in the form.
182 $parents = array_merge($element['#field_parents'], [
187 // Assign a unique identifier to each IEF widget.
188 // Since $parents can get quite long, sha1() ensures that every id has
189 // a consistent and relatively short length while maintaining uniqueness.
190 $this->setIefId(sha1(implode('-', $parents)));
192 // Get the langcode of the parent entity.
193 $parent_langcode = $items->getEntity()->language()->getId();
195 // Determine the wrapper ID for the entire element.
196 $wrapper = 'inline-entity-form-' . $this->getIefId();
199 '#type' => 'fieldset',
201 '#description' => $this->fieldDefinition->getDescription(),
202 '#prefix' => '<div id="' . $wrapper . '">',
203 '#suffix' => '</div>',
204 '#ief_id' => $this->getIefId(),
206 '#translating' => $this->isTranslating($form_state),
207 '#field_title' => $this->fieldDefinition->getLabel(),
209 [get_class($this), 'removeTranslatabilityClue'],
213 $element['#attached']['library'][] = 'inline_entity_form/widget';
215 $this->prepareFormState($form_state, $items, $element['#translating']);
216 $entities = $form_state->get(['inline_entity_form', $this->getIefId(), 'entities']);
218 // Build the "Multiple value" widget.
219 // TODO - does this belong in #element_validate?
220 $element['#element_validate'][] = [get_class($this), 'updateRowWeights'];
221 // Add the required element marker & validation.
222 if ($element['#required']) {
223 $element['#element_validate'][] = [get_class($this), 'requiredField'];
226 $element['entities'] = [
228 '#theme' => 'inline_entity_form_entity_table',
229 '#entity_type' => $target_type,
232 // Get the fields that should be displayed in the table.
233 $target_bundles = $this->getTargetBundles();
234 $fields = $this->inlineFormHandler->getTableFields($target_bundles);
236 'parent_entity_type' => $this->fieldDefinition->getTargetEntityTypeId(),
237 'parent_bundle' => $this->fieldDefinition->getTargetBundle(),
238 'field_name' => $this->fieldDefinition->getName(),
239 'entity_type' => $target_type,
240 'allowed_bundles' => $target_bundles,
242 $this->moduleHandler->alter('inline_entity_form_table_fields', $fields, $context);
243 $element['entities']['#table_fields'] = $fields;
245 $weight_delta = max(ceil(count($entities) * 1.2), 50);
246 foreach ($entities as $key => $value) {
247 // Data used by theme_inline_entity_form_entity_table().
248 /** @var \Drupal\Core\Entity\EntityInterface $entity */
249 $entity = $value['entity'];
250 $element['entities'][$key]['#label'] = $this->inlineFormHandler->getEntityLabel($value['entity']);
251 $element['entities'][$key]['#entity'] = $value['entity'];
252 $element['entities'][$key]['#needs_save'] = $value['needs_save'];
254 // Handle row weights.
255 $element['entities'][$key]['#weight'] = $value['weight'];
257 // First check to see if this entity should be displayed as a form.
258 if (!empty($value['form'])) {
259 $element['entities'][$key]['title'] = [];
260 $element['entities'][$key]['delta'] = [
262 '#value' => $value['weight'],
265 // Add the appropriate form.
266 if ($value['form'] == 'edit') {
267 $element['entities'][$key]['form'] = [
268 '#type' => 'container',
269 '#attributes' => ['class' => ['ief-form', 'ief-form-row']],
270 'inline_entity_form' => $this->getInlineEntityForm(
275 array_merge($parents, ['inline_entity_form', 'entities', $key, 'form']),
280 $element['entities'][$key]['form']['inline_entity_form']['#process'] = [
281 ['\Drupal\inline_entity_form\Element\InlineEntityForm', 'processEntityForm'],
282 [get_class($this), 'addIefSubmitCallbacks'],
283 [get_class($this), 'buildEntityFormActions'],
286 elseif ($value['form'] == 'remove') {
287 $element['entities'][$key]['form'] = [
288 '#type' => 'container',
289 '#attributes' => ['class' => ['ief-form', 'ief-form-row']],
290 // Used by Field API and controller methods to find the relevant
291 // values in $form_state.
292 '#parents' => array_merge($parents, ['entities', $key, 'form']),
293 // Store the entity on the form, later modified in the controller.
294 '#entity' => $entity,
295 // Identifies the IEF widget to which the form belongs.
296 '#ief_id' => $this->getIefId(),
297 // Identifies the table row to which the form belongs.
298 '#ief_row_delta' => $key,
300 $this->buildRemoveForm($element['entities'][$key]['form']);
304 $row = &$element['entities'][$key];
308 '#delta' => $weight_delta,
309 '#default_value' => $value['weight'],
310 '#attributes' => ['class' => ['ief-entity-delta']],
312 // Add an actions container with edit and delete buttons for the entity.
314 '#type' => 'container',
315 '#attributes' => ['class' => ['ief-entity-operations']],
318 // Make sure entity_access is not checked for unsaved entities.
319 $entity_id = $entity->id();
320 if (empty($entity_id) || $entity->access('update')) {
321 $row['actions']['ief_entity_edit'] = [
323 '#value' => $this->t('Edit'),
324 '#name' => 'ief-' . $this->getIefId() . '-entity-edit-' . $key,
325 '#limit_validation_errors' => [],
327 'callback' => 'inline_entity_form_get_element',
328 'wrapper' => $wrapper,
330 '#submit' => ['inline_entity_form_open_row_form'],
331 '#ief_row_delta' => $key,
332 '#ief_row_form' => 'edit',
336 // If 'allow_existing' is on, the default removal operation is unlink
337 // and the access check for deleting happens inside the controller
338 // removeForm() method.
339 if (empty($entity_id) || $settings['allow_existing'] || $entity->access('delete')) {
340 $row['actions']['ief_entity_remove'] = [
342 '#value' => $this->t('Remove'),
343 '#name' => 'ief-' . $this->getIefId() . '-entity-remove-' . $key,
344 '#limit_validation_errors' => [],
346 'callback' => 'inline_entity_form_get_element',
347 'wrapper' => $wrapper,
349 '#submit' => ['inline_entity_form_open_row_form'],
350 '#ief_row_delta' => $key,
351 '#ief_row_form' => 'remove',
352 '#access' => !$element['#translating'],
358 // When in translation, the widget only supports editing (translating)
359 // already added entities, so there's no need to show the rest.
360 if ($element['#translating']) {
361 if (empty($entities)) {
362 // There are no entities available for translation, hide the widget.
363 $element['#access'] = FALSE;
368 $entities_count = count($entities);
369 $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
370 if ($cardinality > 1) {
371 // Add a visual cue of cardinality count.
372 $message = $this->t('You have added @entities_count out of @cardinality_count allowed @label.', [
373 '@entities_count' => $entities_count,
374 '@cardinality_count' => $cardinality,
375 '@label' => $labels['plural'],
377 $element['cardinality_count'] = [
378 '#markup' => '<div class="ief-cardinality-count">' . $message . '</div>',
381 // Do not return the rest of the form if cardinality count has been reached.
382 if ($cardinality > 0 && $entities_count == $cardinality) {
386 $create_bundles = $this->getCreateBundles();
387 $create_bundles_count = count($create_bundles);
388 $allow_new = $settings['allow_new'] && !empty($create_bundles);
389 $hide_cancel = FALSE;
390 // If the field is required and empty try to open one of the forms.
391 if (empty($entities) && $this->fieldDefinition->isRequired()) {
392 if ($settings['allow_existing'] && !$allow_new) {
393 $form_state->set(['inline_entity_form', $this->getIefId(), 'form'], 'ief_add_existing');
396 elseif ($create_bundles_count == 1 && $allow_new && !$settings['allow_existing']) {
397 $bundle = reset($target_bundles);
399 // The parent entity type and bundle must not be the same as the inline
400 // entity type and bundle, to prevent recursion.
401 $parent_entity_type = $this->fieldDefinition->getTargetEntityTypeId();
402 $parent_bundle = $this->fieldDefinition->getTargetBundle();
403 if ($parent_entity_type != $target_type || $parent_bundle != $bundle) {
404 $form_state->set(['inline_entity_form', $this->getIefId(), 'form'], 'add');
405 $form_state->set(['inline_entity_form', $this->getIefId(), 'form settings'], [
413 // If no form is open, show buttons that open one.
414 $open_form = $form_state->get(['inline_entity_form', $this->getIefId(), 'form']);
416 if (empty($open_form)) {
417 $element['actions'] = [
418 '#attributes' => ['class' => ['container-inline']],
419 '#type' => 'container',
423 // The user is allowed to create an entity of at least one bundle.
425 // Let the user select the bundle, if multiple are available.
426 if ($create_bundles_count > 1) {
428 foreach ($this->entityTypeBundleInfo->getBundleInfo($target_type) as $bundle_name => $bundle_info) {
429 if (in_array($bundle_name, $create_bundles)) {
430 $bundles[$bundle_name] = $bundle_info['label'];
435 $element['actions']['bundle'] = [
437 '#options' => $bundles,
441 $element['actions']['bundle'] = [
443 '#value' => reset($create_bundles),
447 $element['actions']['ief_add'] = [
449 '#value' => $this->t('Add new @type_singular', ['@type_singular' => $labels['singular']]),
450 '#name' => 'ief-' . $this->getIefId() . '-add',
451 '#limit_validation_errors' => [array_merge($parents, ['actions'])],
453 'callback' => 'inline_entity_form_get_element',
454 'wrapper' => $wrapper,
456 '#submit' => ['inline_entity_form_open_form'],
457 '#ief_form' => 'add',
461 if ($settings['allow_existing']) {
462 $element['actions']['ief_add_existing'] = [
464 '#value' => $this->t('Add existing @type_singular', ['@type_singular' => $labels['singular']]),
465 '#name' => 'ief-' . $this->getIefId() . '-add-existing',
466 '#limit_validation_errors' => [array_merge($parents, ['actions'])],
468 'callback' => 'inline_entity_form_get_element',
469 'wrapper' => $wrapper,
471 '#submit' => ['inline_entity_form_open_form'],
472 '#ief_form' => 'ief_add_existing',
477 // There's a form open, show it.
478 if ($form_state->get(['inline_entity_form', $this->getIefId(), 'form']) == 'add') {
480 '#type' => 'fieldset',
481 '#attributes' => ['class' => ['ief-form', 'ief-form-bottom']],
482 'inline_entity_form' => $this->getInlineEntityForm(
484 $this->determineBundle($form_state),
487 array_merge($parents, ['inline_entity_form'])
490 $element['form']['inline_entity_form']['#process'] = [
491 ['\Drupal\inline_entity_form\Element\InlineEntityForm', 'processEntityForm'],
492 [get_class($this), 'addIefSubmitCallbacks'],
493 [get_class($this), 'buildEntityFormActions'],
496 elseif ($form_state->get(['inline_entity_form', $this->getIefId(), 'form']) == 'ief_add_existing') {
498 '#type' => 'fieldset',
499 '#attributes' => ['class' => ['ief-form', 'ief-form-bottom']],
500 // Identifies the IEF widget to which the form belongs.
501 '#ief_id' => $this->getIefId(),
502 // Used by Field API and controller methods to find the relevant
503 // values in $form_state.
504 '#parents' => array_merge($parents),
505 // Pass the current entity type.
506 '#entity_type' => $target_type,
507 // Pass the widget specific labels.
508 '#ief_labels' => $this->getEntityTypeLabels(),
511 $element['form'] += inline_entity_form_reference_form($element['form'], $form_state);
514 // Pre-opened forms can't be closed in order to force the user to
515 // add / reference an entity.
517 if ($open_form == 'add') {
518 $process_element = &$element['form']['inline_entity_form'];
520 elseif ($open_form == 'ief_add_existing') {
521 $process_element = &$element['form'];
523 $process_element['#process'][] = [get_class($this), 'hideCancel'];
526 // No entities have been added. Remove the outer fieldset to reduce
527 // visual noise caused by having two titles.
528 if (empty($entities)) {
529 $element['#type'] = 'container';
539 public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
540 if ($this->isDefaultValueWidget($form_state)) {
541 $items->filterEmptyItems();
544 $triggering_element = $form_state->getTriggeringElement();
545 if (empty($triggering_element['#ief_submit_trigger'])) {
549 $field_name = $this->fieldDefinition->getName();
550 $parents = array_merge($form['#parents'], [$field_name, 'form']);
551 $ief_id = sha1(implode('-', $parents));
552 $this->setIefId($ief_id);
553 $widget_state = &$form_state->get(['inline_entity_form', $ief_id]);
554 foreach ($widget_state['entities'] as $key => $value) {
555 $changed = TranslationHelper::updateEntityLangcode($value['entity'], $form_state);
557 $widget_state['entities'][$key]['entity'] = $value['entity'];
558 $widget_state['entities'][$key]['needs_save'] = TRUE;
562 $values = $widget_state['entities'];
563 // If the inline entity form is still open, then its entity hasn't
564 // been transferred to the IEF form state yet.
565 if (empty($values) && !empty($widget_state['form'])) {
566 // @todo Do the same for reference forms.
567 if ($widget_state['form'] == 'add') {
568 $element = NestedArray::getValue($form, [$field_name, 'widget', 'form']);
569 $entity = $element['inline_entity_form']['#entity'];
570 $values[] = ['entity' => $entity];
573 // Sort values by weight.
574 uasort($values, '\Drupal\Component\Utility\SortArray::sortByWeightElement');
575 // Let the widget massage the submitted values.
576 $values = $this->massageFormValues($values, $form, $form_state);
577 // Assign the values and remove the empty ones.
578 $items->setValue($values);
579 $items->filterEmptyItems();
583 * Adds actions to the inline entity form.
585 * @param array $element
586 * Form array structure.
588 public static function buildEntityFormActions($element) {
589 // Build a delta suffix that's appended to button #name keys for uniqueness.
590 $delta = $element['#ief_id'];
591 if ($element['#op'] == 'add') {
592 $save_label = t('Create @type_singular', ['@type_singular' => $element['#ief_labels']['singular']]);
595 $delta .= '-' . $element['#ief_row_delta'];
596 $save_label = t('Update @type_singular', ['@type_singular' => $element['#ief_labels']['singular']]);
599 // Add action submit elements.
600 $element['actions'] = [
601 '#type' => 'container',
604 $element['actions']['ief_' . $element['#op'] . '_save'] = [
606 '#value' => $save_label,
607 '#name' => 'ief-' . $element['#op'] . '-submit-' . $delta,
608 '#limit_validation_errors' => [$element['#parents']],
609 '#attributes' => ['class' => ['ief-entity-submit']],
611 'callback' => 'inline_entity_form_get_element',
612 'wrapper' => 'inline-entity-form-' . $element['#ief_id'],
615 $element['actions']['ief_' . $element['#op'] . '_cancel'] = [
617 '#value' => t('Cancel'),
618 '#name' => 'ief-' . $element['#op'] . '-cancel-' . $delta,
619 '#limit_validation_errors' => [],
621 'callback' => 'inline_entity_form_get_element',
622 'wrapper' => 'inline-entity-form-' . $element['#ief_id'],
626 // Add submit handlers depending on operation.
627 if ($element['#op'] == 'add') {
628 static::addSubmitCallbacks($element['actions']['ief_add_save']);
629 $element['actions']['ief_add_cancel']['#submit'] = [
630 [get_called_class(), 'closeChildForms'],
631 [get_called_class(), 'closeForm'],
632 'inline_entity_form_cleanup_form_state',
636 $element['actions']['ief_edit_save']['#ief_row_delta'] = $element['#ief_row_delta'];
637 $element['actions']['ief_edit_cancel']['#ief_row_delta'] = $element['#ief_row_delta'];
639 static::addSubmitCallbacks($element['actions']['ief_edit_save']);
640 $element['actions']['ief_edit_save']['#submit'][] = [get_called_class(), 'submitCloseRow'];
641 $element['actions']['ief_edit_cancel']['#submit'] = [
642 [get_called_class(), 'closeChildForms'],
643 [get_called_class(), 'submitCloseRow'],
644 'inline_entity_form_cleanup_row_form_state',
652 * Hides cancel button.
654 * @param array $element
655 * Form array structure.
657 public static function hideCancel($element) {
658 // @todo Name both buttons the same and simplify this logic.
659 if (isset($element['actions']['ief_add_cancel'])) {
660 $element['actions']['ief_add_cancel']['#access'] = FALSE;
662 elseif (isset($element['actions']['ief_reference_cancel'])) {
663 $element['actions']['ief_reference_cancel']['#access'] = FALSE;
670 * Builds remove form.
673 * Form array structure.
675 protected function buildRemoveForm(&$form) {
676 /** @var \Drupal\Core\Entity\EntityInterface $entity */
677 $entity = $form['#entity'];
678 $entity_id = $entity->id();
679 $entity_label = $this->inlineFormHandler->getEntityLabel($entity);
680 $labels = $this->getEntityTypeLabels();
683 $message = $this->t('Are you sure you want to remove %label?', ['%label' => $entity_label]);
686 $message = $this->t('Are you sure you want to remove this %entity_type?', ['%entity_type' => $labels['singular']]);
690 '#theme_wrappers' => ['container'],
691 '#markup' => $message,
694 if (!empty($entity_id) && $this->getSetting('allow_existing') && $entity->access('delete')) {
696 '#type' => 'checkbox',
697 '#title' => $this->t('Delete this @type_singular from the system.', ['@type_singular' => $labels['singular']]),
701 // Build a deta suffix that's appended to button #name keys for uniqueness.
702 $delta = $form['#ief_id'] . '-' . $form['#ief_row_delta'];
704 // Add actions to the form.
706 '#type' => 'container',
709 $form['actions']['ief_remove_confirm'] = [
711 '#value' => $this->t('Remove'),
712 '#name' => 'ief-remove-confirm-' . $delta,
713 '#limit_validation_errors' => [$form['#parents']],
715 'callback' => 'inline_entity_form_get_element',
716 'wrapper' => 'inline-entity-form-' . $form['#ief_id'],
718 '#allow_existing' => $this->getSetting('allow_existing'),
719 '#submit' => [[get_class($this), 'submitConfirmRemove']],
720 '#ief_row_delta' => $form['#ief_row_delta'],
722 $form['actions']['ief_remove_cancel'] = [
724 '#value' => $this->t('Cancel'),
725 '#name' => 'ief-remove-cancel-' . $delta,
726 '#limit_validation_errors' => [],
728 'callback' => 'inline_entity_form_get_element',
729 'wrapper' => 'inline-entity-form-' . $form['#ief_id'],
731 '#submit' => [[get_class($this), 'submitCloseRow']],
732 '#ief_row_delta' => $form['#ief_row_delta'],
737 * Button #submit callback: Closes a row form in the IEF widget.
740 * The complete parent form.
742 * The form state of the parent form.
744 * @see inline_entity_form_open_row_form().
746 public static function submitCloseRow($form, FormStateInterface $form_state) {
747 $element = inline_entity_form_get_element($form, $form_state);
748 $ief_id = $element['#ief_id'];
749 $delta = $form_state->getTriggeringElement()['#ief_row_delta'];
751 $form_state->setRebuild();
752 $form_state->set(['inline_entity_form', $ief_id, 'entities', $delta, 'form'], NULL);
757 * Remove form submit callback.
759 * The row is identified by #ief_row_delta stored on the triggering
761 * This isn't an #element_validate callback to avoid processing the
762 * remove form when the main form is submitted.
765 * The complete parent form.
767 * The form state of the parent form.
769 public static function submitConfirmRemove($form, FormStateInterface $form_state) {
770 $element = inline_entity_form_get_element($form, $form_state);
771 $remove_button = $form_state->getTriggeringElement();
772 $delta = $remove_button['#ief_row_delta'];
774 /** @var \Drupal\Core\Field\FieldDefinitionInterface $instance */
775 $instance = $form_state->get(['inline_entity_form', $element['#ief_id'], 'instance']);
777 /** @var \Drupal\Core\Entity\EntityInterface $entity */
778 $entity = $element['entities'][$delta]['form']['#entity'];
779 $entity_id = $entity->id();
781 $form_values = NestedArray::getValue($form_state->getValues(), $element['entities'][$delta]['form']['#parents']);
782 $form_state->setRebuild();
784 $widget_state = $form_state->get(['inline_entity_form', $element['#ief_id']]);
785 // This entity hasn't been saved yet, we can just unlink it.
786 if (empty($entity_id) || ($remove_button['#allow_existing'] && empty($form_values['delete']))) {
787 unset($widget_state['entities'][$delta]);
790 $widget_state['delete'][] = $entity;
791 unset($widget_state['entities'][$delta]);
793 $form_state->set(['inline_entity_form', $element['#ief_id']], $widget_state);
797 * Determines bundle to be used when creating entity.
799 * @param FormStateInterface $form_state
800 * Current form state.
803 * Bundle machine name.
805 * @TODO - Figure out if can be simplified.
807 protected function determineBundle(FormStateInterface $form_state) {
808 $ief_settings = $form_state->get(['inline_entity_form', $this->getIefId()]);
809 if (!empty($ief_settings['form settings']['bundle'])) {
810 return $ief_settings['form settings']['bundle'];
812 elseif (!empty($ief_settings['bundle'])) {
813 return $ief_settings['bundle'];
816 $target_bundles = $this->getTargetBundles();
817 return reset($target_bundles);
822 * Updates entity weights based on their weights in the widget.
824 public static function updateRowWeights($element, FormStateInterface $form_state, $form) {
825 $ief_id = $element['#ief_id'];
827 // Loop over the submitted delta values and update the weight of the entities
828 // in the form state.
829 foreach (Element::children($element['entities']) as $key) {
830 $form_state->set(['inline_entity_form', $ief_id, 'entities', $key, 'weight'], $element['entities'][$key]['delta']['#value']);
835 * IEF widget #element_validate callback: Required field validation.
837 public static function requiredField($element, FormStateInterface $form_state, $form) {
838 $ief_id = $element['#ief_id'];
839 $children = $form_state->get(['inline_entity_form', $ief_id, 'entities']);
840 $has_children = !empty($children);
841 $form = $form_state->get(['inline_entity_form', $ief_id, 'form']);
842 $form_open = !empty($form);
843 // If the add new / add existing form is open, its validation / submission
844 // will do the job instead (either by preventing the parent form submission
845 // or by adding a new referenced entity).
846 if (!$has_children && !$form_open) {
847 /** @var \Drupal\Core\Field\FieldDefinitionInterface $instance */
848 $instance = $form_state->get(['inline_entity_form', $ief_id, 'instance']);
849 $form_state->setError($element, t('@name field is required.', ['@name' => $instance->getLabel()]));
854 * Button #submit callback: Closes a form in the IEF widget.
857 * The complete parent form.
859 * The form state of the parent form.
861 * @see inline_entity_form_open_form().
863 public static function closeForm($form, FormStateInterface $form_state) {
864 $element = inline_entity_form_get_element($form, $form_state);
865 $ief_id = $element['#ief_id'];
867 $form_state->setRebuild();
868 $form_state->set(['inline_entity_form', $ief_id, 'form'], NULL);
872 * Add common submit callback functions and mark element as a IEF trigger.
876 public static function addSubmitCallbacks(&$element) {
877 $element['#submit'] = [
878 ['\Drupal\inline_entity_form\ElementSubmit', 'trigger'],
879 ['\Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormComplex', 'closeForm'],
881 $element['#ief_submit_trigger'] = TRUE;
885 * Button #submit callback: Closes all open child forms in the IEF widget.
887 * Used to ensure that forms in nested IEF widgets are properly closed
888 * when a parent IEF's form gets submitted or cancelled.
891 * The IEF Form element.
892 * @param FormStateInterface $form_state
893 * The form state of the parent form.
895 public static function closeChildForms($form, FormStateInterface &$form_state) {
896 $element = inline_entity_form_get_element($form, $form_state);
897 inline_entity_form_close_all_forms($element, $form_state);