public function EntityFormBuilder::getForm

public EntityFormBuilder::getForm(EntityInterface $entity, $operation = 'default', array $form_state_additions = array())

Gets the built and processed entity form for the given entity.

The form may also be retrieved from the cache if the form was built in a previous page load. The form is then passed on for processing, validation, and submission if there is proper input.

  $form_state_additions['langcode'] = $langcode;
  $form = \Drupal::service('entity.form_builder')->getForm($entity, 'default', $form_state_additions);

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to be created or edited.

string $operation: (optional) The operation identifying the form variation to be returned. Defaults to 'default'. This is typically used in routing:

  _entity_form: node.book_outline
  

where "book_outline" is the value of $operation. The class name for the form for each operation (edit, delete, etc.) can be found in the form section of the handlers entity annotation. For example:

  handlers = {
    "form" = {
      "delete" = "Drupal\node\Form\NodeDeleteForm",
  

Alternatively, the form class can be set from hook_entity_type_build().

array $form_state_additions: (optional) An associative array used to build the current state of the form. Use this to pass additional information to the form, such as the langcode. Defaults to an empty array.

Return value

array The processed form for the given entity and operation.

Overrides EntityFormBuilderInterface::getForm

See also

\Drupal\Core\Form\FormBuilderInterface::getForm()

\Drupal\Core\Entity\EntityTypeInterface::getFormClass()

\Drupal\Core\Entity\EntityTypeInterface::setFormClass()

system_entity_type_build()

File

core/lib/Drupal/Core/Entity/EntityFormBuilder.php, line 43

Class

EntityFormBuilder
Builds entity forms.

Namespace

Drupal\Core\Entity

Code

public function getForm(EntityInterface $entity, $operation = 'default', array $form_state_additions = array()) {
  $form_object = $this->entityManager->getFormObject($entity->getEntityTypeId(), $operation);
  $form_object->setEntity($entity);

  $form_state = (new FormState())->setFormState($form_state_additions);
  return $this->formBuilder->buildForm($form_object, $form_state);
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityFormBuilder.php/function/EntityFormBuilder::getForm/8.1.x