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