86e021e38c2cd11ba79c93bb3dcf3841c8c50173
[yaffs-website] / web / core / modules / config / tests / config_test / src / ConfigTestForm.php
1 <?php
2
3 namespace Drupal\config_test;
4
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Language\LanguageInterface;
8
9 /**
10  * Form controller for the test config edit forms.
11  *
12  * @internal
13  */
14 class ConfigTestForm extends EntityForm {
15
16   /**
17    * {@inheritdoc}
18    */
19   public function form(array $form, FormStateInterface $form_state) {
20     $form = parent::form($form, $form_state);
21
22     $entity = $this->entity;
23     $form['label'] = [
24       '#type' => 'textfield',
25       '#title' => 'Label',
26       '#default_value' => $entity->label(),
27       '#required' => TRUE,
28     ];
29     $form['id'] = [
30       '#type' => 'machine_name',
31       '#default_value' => $entity->id(),
32       '#required' => TRUE,
33       '#machine_name' => [
34         'exists' => [$this, 'exists'],
35         'replace_pattern' => '[^a-z0-9_.]+',
36       ],
37     ];
38     $form['weight'] = [
39       '#type' => 'weight',
40       '#title' => 'Weight',
41       '#default_value' => $entity->get('weight'),
42     ];
43     $form['style'] = [
44       '#type' => 'select',
45       '#title' => 'Image style',
46       '#options' => [],
47       '#default_value' => $entity->get('style'),
48       '#access' => FALSE,
49     ];
50     if ($this->moduleHandler->moduleExists('image')) {
51       $form['style']['#access'] = TRUE;
52       $form['style']['#options'] = image_style_options();
53     }
54
55     // The main premise of entity forms is that we get to work with an entity
56     // object at all times instead of checking submitted values from the form
57     // state.
58     $size = $entity->get('size');
59
60     $form['size_wrapper'] = [
61       '#type' => 'container',
62       '#attributes' => [
63         'id' => 'size-wrapper',
64       ],
65     ];
66     $form['size_wrapper']['size'] = [
67       '#type' => 'select',
68       '#title' => 'Size',
69       '#options' => [
70         'custom' => 'Custom',
71       ],
72       '#empty_option' => '- None -',
73       '#default_value' => $size,
74       '#ajax' => [
75         'callback' => '::updateSize',
76         'wrapper' => 'size-wrapper',
77       ],
78     ];
79     $form['size_wrapper']['size_submit'] = [
80       '#type' => 'submit',
81       '#value' => t('Change size'),
82       '#attributes' => [
83         'class' => ['js-hide'],
84       ],
85       '#submit' => [[get_class($this), 'changeSize']],
86     ];
87     $form['size_wrapper']['size_value'] = [
88       '#type' => 'select',
89       '#title' => 'Custom size value',
90       '#options' => [
91         'small' => 'Small',
92         'medium' => 'Medium',
93         'large' => 'Large',
94       ],
95       '#default_value' => $entity->get('size_value'),
96       '#access' => !empty($size),
97     ];
98
99     $form['langcode'] = [
100       '#type' => 'language_select',
101       '#title' => t('Language'),
102       '#languages' => LanguageInterface::STATE_ALL,
103       '#default_value' => $entity->language()->getId(),
104     ];
105
106     $form['actions'] = ['#type' => 'actions'];
107     $form['actions']['submit'] = [
108       '#type' => 'submit',
109       '#value' => 'Save',
110     ];
111     $form['actions']['delete'] = [
112       '#type' => 'submit',
113       '#value' => 'Delete',
114     ];
115
116     return $form;
117   }
118
119   /**
120    * Ajax callback for the size selection element.
121    */
122   public static function updateSize(array $form, FormStateInterface $form_state) {
123     return $form['size_wrapper'];
124   }
125
126   /**
127    * Element submit handler for non-JS testing.
128    */
129   public static function changeSize(array $form, FormStateInterface $form_state) {
130     $form_state->setRebuild();
131   }
132
133   /**
134    * {@inheritdoc}
135    */
136   public function save(array $form, FormStateInterface $form_state) {
137     $entity = $this->entity;
138     $status = $entity->save();
139
140     if ($status === SAVED_UPDATED) {
141       $this->messenger()->addStatus(format_string('%label configuration has been updated.', ['%label' => $entity->label()]));
142     }
143     else {
144       $this->messenger()->addStatus(format_string('%label configuration has been created.', ['%label' => $entity->label()]));
145     }
146
147     $form_state->setRedirectUrl($this->entity->urlInfo('collection'));
148   }
149
150   /**
151    * Determines if the entity already exists.
152    *
153    * @param string|int $entity_id
154    *   The entity ID.
155    * @param array $element
156    *   The form element.
157    * @param \Drupal\Core\Form\FormStateInterface $form_state
158    *   The current state of the form.
159    *
160    * @return bool
161    *   TRUE if the entity exists, FALSE otherwise.
162    */
163   public function exists($entity_id, array $element, FormStateInterface $form_state) {
164     /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
165     $entity = $form_state->getFormObject()->getEntity();
166     return (bool) $this->entityTypeManager->getStorage($entity->getEntityTypeId())
167       ->getQuery()
168       ->condition($entity->getEntityType()->getKey('id'), $entity_id)
169       ->execute();
170   }
171
172 }