You are here

public static function InlineEntityFormBase::submitSaveEntity in Inline Entity Form 8

Marks created/edited entity with "needs save" flag.

Note that at this point the entity is not yet saved, since the user might still decide to cancel the parent form.

Parameters

$entity_form: The form of the entity being managed inline.

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

File

src/Plugin/Field/FieldWidget/InlineEntityFormBase.php, line 512

Class

InlineEntityFormBase
Inline entity form widget base class.

Namespace

Drupal\inline_entity_form\Plugin\Field\FieldWidget

Code

public static function submitSaveEntity($entity_form, FormStateInterface $form_state) {
  $ief_id = $entity_form['#ief_id'];

  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = $entity_form['#entity'];
  if (in_array($entity_form['#op'], [
    'add',
    'duplicate',
  ])) {

    // Determine the correct weight of the new element.
    $weight = 0;
    $entities = $form_state
      ->get([
      'inline_entity_form',
      $ief_id,
      'entities',
    ]);
    if (!empty($entities)) {
      $weight = max(array_keys($entities)) + 1;
    }

    // Add the entity to form state, mark it for saving, and close the form.
    $entities[] = [
      'entity' => $entity,
      'weight' => $weight,
      'form' => NULL,
      'needs_save' => TRUE,
    ];
    $form_state
      ->set([
      'inline_entity_form',
      $ief_id,
      'entities',
    ], $entities);
  }
  else {
    $delta = $entity_form['#ief_row_delta'];
    $entities = $form_state
      ->get([
      'inline_entity_form',
      $ief_id,
      'entities',
    ]);
    $entities[$delta]['entity'] = $entity;
    $entities[$delta]['needs_save'] = TRUE;
    $form_state
      ->set([
      'inline_entity_form',
      $ief_id,
      'entities',
    ], $entities);
  }
}