You are here

public function EntityInlineForm::entityForm in Inline Entity Form 8

Builds the entity form.

Parameters

array $entity_form: The entity form, containing the following basic properties:

  • #entity: The entity for the current entity form.
  • #op: The form operation. 'add' or 'edit'.
  • #form_mode: The form mode used to display the entity form.
  • #parents: Identifies the position of the entity form in the overall parent form, and identifies the location where the field values are placed within $form_state->getValues().

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

Overrides InlineFormInterface::entityForm

File

src/Form/EntityInlineForm.php, line 171

Class

EntityInlineForm
Generic entity inline form handler.

Namespace

Drupal\inline_entity_form\Form

Code

public function entityForm(array $entity_form, FormStateInterface $form_state) {

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $entity_form['#entity'];
  $form_display = $this
    ->getFormDisplay($entity, $entity_form['#form_mode']);
  $form_display
    ->buildForm($entity, $entity_form, $form_state);
  $entity_form['#ief_element_submit'][] = [
    get_class($this),
    'submitCleanFormState',
  ];

  // Inline entities inherit the parent language.
  $langcode_key = $this->entityType
    ->getKey('langcode');
  if ($langcode_key && isset($entity_form[$langcode_key])) {
    $entity_form[$langcode_key]['#access'] = FALSE;
  }
  if (!empty($entity_form['#translating'])) {

    // Hide the non-translatable fields.
    foreach ($entity
      ->getFieldDefinitions() as $field_name => $definition) {
      if (isset($entity_form[$field_name]) && $field_name != $langcode_key) {
        $entity_form[$field_name]['#access'] = $definition
          ->isTranslatable();
      }
    }
  }

  // Hide the log message field for revisionable entity types. It cannot be
  // disabled in UI and does not make sense in inline entity form context.
  if ($this->entityType instanceof ContentEntityTypeInterface) {
    if ($log_message_key = $this->entityType
      ->getRevisionMetadataKey('revision_log_message')) {
      $entity_form[$log_message_key]['#access'] = FALSE;
    }
  }

  // Determine the children of the entity form before it has been altered.
  $children_before = Element::children($entity_form);

  // Allow other modules to alter the form.
  $this->moduleHandler
    ->alter('inline_entity_form_entity_form', $entity_form, $form_state);

  // Determine the children of the entity form after it has been altered.
  $children_after = Element::children($entity_form);

  // Ensure that any new children added have #tree, #parents, #array_parents
  // and handle setting the proper #group if it's referencing a local element.
  // Note: the #tree, #parents and #array_parents code is a direct copy from
  // \Drupal\Core\Form\FormBuilder::doBuildForm.
  $children_diff = array_diff($children_after, $children_before);
  foreach ($children_diff as $child) {

    // Don't squash an existing tree value.
    if (!isset($entity_form[$child]['#tree'])) {
      $entity_form[$child]['#tree'] = $entity_form['#tree'];
    }

    // Don't squash existing parents value.
    if (!isset($entity_form[$child]['#parents'])) {

      // Check to see if a tree of child elements is present. If so,
      // continue down the tree if required.
      $entity_form[$child]['#parents'] = $entity_form[$child]['#tree'] && $entity_form['#tree'] ? array_merge($entity_form['#parents'], [
        $child,
      ]) : [
        $child,
      ];
    }

    // Ensure #array_parents follows the actual form structure.
    $array_parents = $entity_form['#array_parents'];
    $array_parents[] = $child;
    $entity_form[$child]['#array_parents'] = $array_parents;

    // Detect if there is a #group and it specifies a local element. If so,
    // change it to use the proper local element's #parents group name.
    if (isset($entity_form[$child]['#group']) && isset($entity_form[$entity_form[$child]['#group']])) {
      $entity_form[$child]['#group'] = implode('][', $entity_form[$entity_form[$child]['#group']]['#parents']);
    }
  }
  return $entity_form;
}