You are here

public function NestedEntityTestForm::buildForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field/tests/modules/field_test/src/Form/NestedEntityTestForm.php \Drupal\field_test\Form\NestedEntityTestForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/modules/field/tests/modules/field_test/src/Form/NestedEntityTestForm.php, line 28

Class

NestedEntityTestForm
Provides a form for field_test routes.

Namespace

Drupal\field_test\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $entity_1 = NULL, EntityInterface $entity_2 = NULL) {

  // First entity.
  $form_state
    ->set('entity_1', $entity_1);
  $form_display_1 = EntityFormDisplay::collectRenderDisplay($entity_1, 'default');
  $form_state
    ->set('form_display_1', $form_display_1);
  $form_display_1
    ->buildForm($entity_1, $form, $form_state);

  // Second entity.
  $form_state
    ->set('entity_2', $entity_2);
  $form_display_2 = EntityFormDisplay::collectRenderDisplay($entity_2, 'default');
  $form_state
    ->set('form_display_2', $form_display_2);
  $form['entity_2'] = [
    '#type' => 'details',
    '#title' => t('Second entity'),
    '#tree' => TRUE,
    '#parents' => [
      'entity_2',
    ],
    '#weight' => 50,
    '#attributes' => [
      'class' => [
        'entity-2',
      ],
    ],
  ];
  $form_display_2
    ->buildForm($entity_2, $form['entity_2'], $form_state);
  if ($entity_2 instanceof EntityChangedInterface) {

    // Changed must be sent to the client, for later overwrite error checking.
    // @see \Drupal\Tests\field\Functional\NestedFormTest::testNestedEntityFormEntityLevelValidation()
    $form['entity_2']['changed'] = [
      '#type' => 'hidden',
      '#default_value' => $entity_1
        ->getChangedTime(),
    ];
  }
  $form['save'] = [
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 100,
  ];
  return $form;
}