You are here

public function AbstractEntityFormModesFactory::getForm in Form mode manager 8.2

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

This method are very similar to EntityFormBuilderInterface::getForm, for this module we need to add two form handler by form mode eg : form_mode_1 => EntityFormClass edit_form_mode_1 => EntityFormClass to provide ability to define different EntityForm class for form, for add/edit (or others) contexts. Actually EntityFormBuilderInterface::getForm are designed to only have, one operation (form mode) by action (add/edit/xxxx).

In that method we use $operation parameter to retrieve the correct, FormObject with our context prefixed by 'edit_' or not and in next step we, set the correct Operation form with only the form mode name, with ->setOperation() method onto FormObject.

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:.

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 entity Form.

Throws

\Drupal\Core\Form\FormAjaxException Thrown when a form is triggered via an AJAX submission. It will be handled by \Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber.

\Drupal\Core\Form\EnforcedResponseException Thrown when a form builder returns a response directly, usually a \Symfony\Component\HttpFoundation\RedirectResponse. It will be handled by \Drupal\Core\EventSubscriber\EnforcedFormResponseSubscriber.

2 calls to AbstractEntityFormModesFactory::getForm()
AbstractEntityFormModesFactory::entityAdd in src/AbstractEntityFormModesFactory.php
Provides the entity add submission form.
AbstractEntityFormModesFactory::entityEdit in src/AbstractEntityFormModesFactory.php
Provides the entity 'edit' form.

File

src/AbstractEntityFormModesFactory.php, line 364

Class

AbstractEntityFormModesFactory
Abstract Factory to generate object used by routing of Form Mode Manager.

Namespace

Drupal\form_mode_manager

Code

public function getForm(EntityInterface $entity, $operation = 'default', array $form_state_additions = []) {
  $form_object = $this->entityTypeManager
    ->getFormObject($entity
    ->getEntityTypeId(), $operation);
  $form_object
    ->setEntity($entity)
    ->setOperation($this
    ->getFormModeOperationName($operation));
  $form_state = (new FormState())
    ->setFormState($form_state_additions);
  return $this->formBuilder
    ->buildForm($form_object, $form_state);
}