4c9fbe2c8c2a93906cbd3080959b56638ce86bfd
[yaffs-website] / web / core / modules / system / tests / modules / form_test / src / Controller / FormTestController.php
1 <?php
2
3 namespace Drupal\form_test\Controller;
4
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Form\FormState;
7 use Drupal\Core\Language\LanguageInterface;
8 use Symfony\Component\HttpFoundation\JsonResponse;
9
10 /**
11  * Controller routines for form_test routes.
12  */
13 class FormTestController extends ControllerBase {
14
15   /**
16    * Returns two instances of the node form.
17    *
18    * @return string
19    *   A HTML-formatted string with the double node form page content.
20    */
21   public function twoFormInstances() {
22     $user = $this->currentUser();
23     $values = [
24       'uid' => $user->id(),
25       'name' => $user->getUsername(),
26       'type' => 'page',
27       'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
28     ];
29     $node1 = $this->entityManager()->getStorage('node')->create($values);
30     $node2 = clone($node1);
31     $return['node_form_1'] = $this->entityFormBuilder()->getForm($node1);
32     $return['node_form_2'] = $this->entityFormBuilder()->getForm($node2);
33     return $return;
34   }
35
36   /**
37    * Emulate legacy AHAH-style ajax callback.
38    *
39    * Drupal 6 AHAH callbacks used to operate directly on forms retrieved using
40    * \Drupal::formBuilder()->getCache() and stored using
41    * \Drupal::formBuilder()->setCache() after manipulation. This callback helps
42    * testing whether \Drupal::formBuilder()->setCache() prevents resaving of
43    * immutable forms.
44    */
45   public function storageLegacyHandler($form_build_id) {
46     $form_state = new FormState();
47     $form = $this->formBuilder()->getCache($form_build_id, $form_state);
48     $result = [
49       'form' => $form,
50       'form_state' => $form_state,
51     ];
52     $form['#poisoned'] = TRUE;
53     $form_state->set('poisoned', TRUE);
54     $this->formBuilder()->setCache($form_build_id, $form, $form_state);
55     return new JsonResponse($result);
56   }
57
58 }