FormTestController.php in Drupal 10
File
core/modules/system/tests/modules/form_test/src/Controller/FormTestController.php
View source
<?php
namespace Drupal\form_test\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormState;
use Drupal\Core\Language\LanguageInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
class FormTestController extends ControllerBase {
public function twoFormInstances() {
$user = $this
->currentUser();
$values = [
'uid' => $user
->id(),
'name' => $user
->getAccountName(),
'type' => 'page',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
];
$node1 = $this
->entityTypeManager()
->getStorage('node')
->create($values);
$node2 = clone $node1;
$return['node_form_1'] = $this
->entityFormBuilder()
->getForm($node1);
$return['node_form_2'] = $this
->entityFormBuilder()
->getForm($node2);
return $return;
}
public function storageLegacyHandler($form_build_id) {
$form_state = new FormState();
$form = $this
->formBuilder()
->getCache($form_build_id, $form_state);
$result = [
'form' => $form,
'form_state' => $form_state,
];
$form['#poisoned'] = TRUE;
$form_state
->set('poisoned', TRUE);
$this
->formBuilder()
->setCache($form_build_id, $form, $form_state);
return new JsonResponse($result);
}
}