You are here

function inline_entity_form_reference_form in Inline Entity Form 8

Same name and namespace in other branches
  1. 7 inline_entity_form.module \inline_entity_form_reference_form()

Provides the form for adding existing entities through an autocomplete field.

Parameters

array $reference_form: The form array that will receive the form.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the parent form.

Return value

array The form array containing the embedded form.

1 call to inline_entity_form_reference_form()
InlineEntityFormComplex::formElement in src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
Returns the form for a single field widget.

File

./inline_entity_form.module, line 73
Provides a widget for inline management (creation, modification, removal) of referenced entities. The primary use case is the parent -> children one (for example, order -> line items), where the child entities are never managed outside the…

Code

function inline_entity_form_reference_form($reference_form, FormStateInterface &$form_state) {
  $labels = $reference_form['#ief_labels'];
  $ief_id = $reference_form['#ief_id'];

  /** @var \Drupal\field\Entity\FieldConfig $instance */
  $instance = $form_state
    ->get([
    'inline_entity_form',
    $ief_id,
    'instance',
  ]);
  $selection_settings = [
    'match_operator' => $reference_form['#match_operator'],
  ] + $instance
    ->getSetting('handler_settings');
  $reference_form['#title'] = t('Add existing @type_singular', [
    '@type_singular' => $labels['singular'],
  ]);
  $reference_form['entity_id'] = [
    '#type' => 'entity_autocomplete',
    // @todo Use bundle defined singular/plural labels as soon as
    //   https://www.drupal.org/node/2765065 is committed.
    // @see https://www.drupal.org/node/2765065
    '#title' => t('@label', [
      '@label' => ucfirst($labels['singular']),
    ]),
    '#target_type' => $instance
      ->getSetting('target_type'),
    '#selection_handler' => $instance
      ->getSetting('handler'),
    '#selection_settings' => $selection_settings,
    '#required' => TRUE,
    '#maxlength' => 255,
  ];

  // Add the actions
  $reference_form['actions'] = [
    '#type' => 'container',
    '#weight' => 100,
  ];
  $reference_form['actions']['ief_reference_save'] = [
    '#type' => 'submit',
    '#value' => t('Add @type_singular', [
      '@type_singular' => $labels['singular'],
    ]),
    '#name' => 'ief-reference-submit-' . $reference_form['#ief_id'],
    '#limit_validation_errors' => [
      $reference_form['#parents'],
    ],
    '#attributes' => [
      'class' => [
        'ief-entity-submit',
      ],
    ],
    '#ajax' => [
      'callback' => 'inline_entity_form_get_element',
      'wrapper' => 'inline-entity-form-' . $reference_form['#ief_id'],
    ],
  ];
  InlineEntityFormComplex::addSubmitCallbacks($reference_form['actions']['ief_reference_save']);
  $reference_form['actions']['ief_reference_cancel'] = [
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#name' => 'ief-reference-cancel-' . $reference_form['#ief_id'],
    '#limit_validation_errors' => [],
    '#ajax' => [
      'callback' => 'inline_entity_form_get_element',
      'wrapper' => 'inline-entity-form-' . $reference_form['#ief_id'],
    ],
    '#submit' => [
      [
        '\\Drupal\\inline_entity_form\\Plugin\\Field\\FieldWidget\\InlineEntityFormComplex',
        'closeForm',
      ],
    ],
  ];
  $reference_form['#element_validate'][] = 'inline_entity_form_reference_form_validate';
  $reference_form['#ief_element_submit'][] = 'inline_entity_form_reference_form_submit';

  // Allow other modules to alter the form.
  \Drupal::moduleHandler()
    ->alter('inline_entity_form_reference_form', $reference_form, $form_state);
  return $reference_form;
}