Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / field / tests / modules / field_test / src / Form / NestedEntityTestForm.php
1 <?php
2
3 namespace Drupal\field_test\Form;
4
5 use Drupal\Core\Entity\EntityChangedInterface;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Form\FormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Entity\Entity\EntityFormDisplay;
10
11 /**
12  * Provides a form for field_test routes.
13  *
14  * @internal
15  */
16 class NestedEntityTestForm extends FormBase {
17
18   /**
19    * {@inheritdoc]
20    */
21   public function getFormId() {
22     return 'field_test_entity_nested_form';
23   }
24
25   /**
26    * {@inheritdoc]
27    */
28   public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $entity_1 = NULL, EntityInterface $entity_2 = NULL) {
29     // First entity.
30     $form_state->set('entity_1', $entity_1);
31     $form_display_1 = EntityFormDisplay::collectRenderDisplay($entity_1, 'default');
32     $form_state->set('form_display_1', $form_display_1);
33     $form_display_1->buildForm($entity_1, $form, $form_state);
34
35     // Second entity.
36     $form_state->set('entity_2', $entity_2);
37     $form_display_2 = EntityFormDisplay::collectRenderDisplay($entity_2, 'default');
38     $form_state->set('form_display_2', $form_display_2);
39     $form['entity_2'] = [
40       '#type' => 'details',
41       '#title' => t('Second entity'),
42       '#tree' => TRUE,
43       '#parents' => ['entity_2'],
44       '#weight' => 50,
45       '#attributes' => ['class' => ['entity-2']],
46     ];
47
48     $form_display_2->buildForm($entity_2, $form['entity_2'], $form_state);
49
50     if ($entity_2 instanceof EntityChangedInterface) {
51       // Changed must be sent to the client, for later overwrite error checking.
52       // @see \Drupal\Tests\field\Functional\NestedFormTest::testNestedEntityFormEntityLevelValidation()
53       $form['entity_2']['changed'] = [
54         '#type' => 'hidden',
55         '#default_value' => $entity_1->getChangedTime(),
56       ];
57     }
58
59     $form['save'] = [
60       '#type' => 'submit',
61       '#value' => t('Save'),
62       '#weight' => 100,
63     ];
64
65     return $form;
66   }
67
68   /**
69    * {@inheritdoc]
70    */
71   public function validateForm(array &$form, FormStateInterface $form_state) {
72     $entity_1 = $form_state->get('entity_1');
73     /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display_1 */
74     $form_display_1 = $form_state->get('form_display_1');
75     $form_display_1->extractFormValues($entity_1, $form, $form_state);
76     $form_display_1->validateFormValues($entity_1, $form, $form_state);
77
78     $entity_2 = $form_state->get('entity_2');
79     /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display_2 */
80     $form_display_2 = $form_state->get('form_display_2');
81     $extracted = $form_display_2->extractFormValues($entity_2, $form['entity_2'], $form_state);
82     // Extract the values of fields that are not rendered through widgets, by
83     // simply copying from top-level form values. This leaves the fields that
84     // are not being edited within this form untouched.
85     // @see \Drupal\Tests\field\Functional\NestedFormTest::testNestedEntityFormEntityLevelValidation()
86     foreach ($form_state->getValues()['entity_2'] as $name => $values) {
87       if ($entity_2->hasField($name) && !isset($extracted[$name])) {
88         $entity_2->set($name, $values);
89       }
90     }
91     $form_display_2->validateFormValues($entity_2, $form['entity_2'], $form_state);
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function submitForm(array &$form, FormStateInterface $form_state) {
98     /** @var \Drupal\Core\Entity\EntityInterface $entity_1 */
99     $entity_1 = $form_state->get('entity_1');
100     $entity_1->save();
101
102     /** @var \Drupal\Core\Entity\EntityInterface $entity_2 */
103     $entity_2 = $form_state->get('entity_2');
104     $entity_2->save();
105
106     $this->messenger()->addStatus($this->t('test_entities @id_1 and @id_2 have been updated.', ['@id_1' => $entity_1->id(), '@id_2' => $entity_2->id()]));
107   }
108
109 }