You are here

public static function InlineEntityForm::processEntityForm in Inline Entity Form 8

Builds the entity form using the inline form handler.

Parameters

array $entity_form: The entity form.

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

array $complete_form: The complete form structure.

Return value

array The built entity form.

Throws

\InvalidArgumentException Thrown when the #entity_type or #bundle properties are empty, or when the #default_value property is not an entity.

File

src/Element/InlineEntityForm.php, line 94

Class

InlineEntityForm
Provides an inline entity form element.

Namespace

Drupal\inline_entity_form\Element

Code

public static function processEntityForm($entity_form, FormStateInterface $form_state, &$complete_form) {
  if (empty($entity_form['#entity_type'])) {
    throw new \InvalidArgumentException('The inline_entity_form element requires the #entity_type property.');
  }
  if (isset($entity_form['#default_value']) && !$entity_form['#default_value'] instanceof EntityInterface) {
    throw new \InvalidArgumentException('The inline_entity_form #default_value property must be an entity object.');
  }
  $entity_type = \Drupal::entityTypeManager()
    ->getDefinition($entity_form['#entity_type']);
  if (empty($entity_form['#ief_id'])) {
    $entity_form['#ief_id'] = \Drupal::service('uuid')
      ->generate();
  }
  if (isset($entity_form['#default_value'])) {

    // Transfer the #default_value to #entity, as expected by inline forms.
    $entity_form['#entity'] = $entity_form['#default_value'];
  }
  else {

    // This is an add operation, create a new entity.
    $storage = \Drupal::entityTypeManager()
      ->getStorage($entity_form['#entity_type']);
    $values = [];
    if ($langcode_key = $entity_type
      ->getKey('langcode')) {
      if (!empty($entity_form['#langcode'])) {
        $values[$langcode_key] = $entity_form['#langcode'];
      }
    }
    if ($bundle_key = $entity_type
      ->getKey('bundle')) {
      $values[$bundle_key] = $entity_form['#bundle'];
    }
    $entity_form['#entity'] = $storage
      ->create($values);
  }
  if (!isset($entity_form['#op'])) {

    // When duplicating entities, the entity is new, but already has a UUID.
    if ($entity_form['#entity']
      ->isNew() && $entity_form['#entity']
      ->uuid()) {
      $entity_form['#op'] = 'duplicate';
    }
    else {
      $entity_form['#op'] = $entity_form['#entity']
        ->isNew() ? 'add' : 'edit';
    }
  }

  // Prepare the entity form and the entity itself for translating.
  $entity_form['#entity'] = TranslationHelper::prepareEntity($entity_form['#entity'], $form_state);
  $entity_form['#translating'] = TranslationHelper::isTranslating($form_state) && $entity_form['#entity']
    ->isTranslatable();

  // Handle revisioning if the entity supports it.
  if ($entity_type
    ->isRevisionable() && $entity_form['#revision']) {
    $entity_form['#entity']
      ->setNewRevision($entity_form['#revision']);

    // @see \Drupal\Core\Entity\ContentEntityForm::buildEntity
    if ($entity_form['#entity'] instanceof RevisionLogInterface) {
      $entity_form['#entity']
        ->setRevisionUserId(\Drupal::currentUser()
        ->id());
      $entity_form['#entity']
        ->setRevisionCreationTime(\Drupal::time()
        ->getRequestTime());
    }
  }
  $inline_form_handler = static::getInlineFormHandler($entity_form['#entity_type']);
  $entity_form = $inline_form_handler
    ->entityForm($entity_form, $form_state);

  // The form element can't rely on inline_entity_form_form_alter() calling
  // ElementSubmit::attach() since form alters run before #process callbacks.
  ElementSubmit::attach($complete_form, $form_state);
  return $entity_form;
}