34868add47965c6719192e0022eff96778a7f681
[yaffs-website] / web / core / modules / field_ui / src / Form / FieldConfigEditForm.php
1 <?php
2
3 namespace Drupal\field_ui\Form;
4
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Field\AllowedTagsXssTrait;
7 use Drupal\Core\Field\FieldFilteredMarkup;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Url;
10 use Drupal\field\FieldConfigInterface;
11 use Drupal\field_ui\FieldUI;
12
13 /**
14  * Provides a form for the field settings form.
15  *
16  * @internal
17  */
18 class FieldConfigEditForm extends EntityForm {
19
20   use AllowedTagsXssTrait;
21
22   /**
23    * The entity being used by this form.
24    *
25    * @var \Drupal\field\FieldConfigInterface
26    */
27   protected $entity;
28
29   /**
30    * {@inheritdoc}
31    */
32   public function form(array $form, FormStateInterface $form_state) {
33     $form = parent::form($form, $form_state);
34
35     $field_storage = $this->entity->getFieldStorageDefinition();
36     $bundles = $this->entityManager->getBundleInfo($this->entity->getTargetEntityTypeId());
37
38     $form_title = $this->t('%field settings for %bundle', [
39       '%field' => $this->entity->getLabel(),
40       '%bundle' => $bundles[$this->entity->getTargetBundle()]['label'],
41     ]);
42     $form['#title'] = $form_title;
43
44     if ($field_storage->isLocked()) {
45       $form['locked'] = [
46         '#markup' => $this->t('The field %field is locked and cannot be edited.', ['%field' => $this->entity->getLabel()]),
47       ];
48       return $form;
49     }
50
51     // Build the configurable field values.
52     $form['label'] = [
53       '#type' => 'textfield',
54       '#title' => $this->t('Label'),
55       '#default_value' => $this->entity->getLabel() ?: $field_storage->getName(),
56       '#required' => TRUE,
57       '#weight' => -20,
58     ];
59
60     $form['description'] = [
61       '#type' => 'textarea',
62       '#title' => $this->t('Help text'),
63       '#default_value' => $this->entity->getDescription(),
64       '#rows' => 5,
65       '#description' => $this->t('Instructions to present to the user below this field on the editing form.<br />Allowed HTML tags: @tags', ['@tags' => FieldFilteredMarkup::displayAllowedTags()]) . '<br />' . $this->t('This field supports tokens.'),
66       '#weight' => -10,
67     ];
68
69     $form['required'] = [
70       '#type' => 'checkbox',
71       '#title' => $this->t('Required field'),
72       '#default_value' => $this->entity->isRequired(),
73       '#weight' => -5,
74     ];
75
76     // Create an arbitrary entity object (used by the 'default value' widget).
77     $ids = (object) [
78       'entity_type' => $this->entity->getTargetEntityTypeId(),
79       'bundle' => $this->entity->getTargetBundle(),
80       'entity_id' => NULL
81     ];
82     $form['#entity'] = _field_create_entity_from_ids($ids);
83     $items = $form['#entity']->get($this->entity->getName());
84     $item = $items->first() ?: $items->appendItem();
85
86     // Add field settings for the field type and a container for third party
87     // settings that modules can add to via hook_form_FORM_ID_alter().
88     $form['settings'] = [
89       '#tree' => TRUE,
90       '#weight' => 10,
91     ];
92     $form['settings'] += $item->fieldSettingsForm($form, $form_state);
93     $form['third_party_settings'] = [
94       '#tree' => TRUE,
95       '#weight' => 11,
96     ];
97
98     // Add handling for default value.
99     if ($element = $items->defaultValuesForm($form, $form_state)) {
100       $element = array_merge($element, [
101         '#type' => 'details',
102         '#title' => $this->t('Default value'),
103         '#open' => TRUE,
104         '#tree' => TRUE,
105         '#description' => $this->t('The default value for this field, used when creating new content.'),
106       ]);
107
108       $form['default_value'] = $element;
109     }
110
111     return $form;
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   protected function actions(array $form, FormStateInterface $form_state) {
118     $actions = parent::actions($form, $form_state);
119     $actions['submit']['#value'] = $this->t('Save settings');
120
121     if (!$this->entity->isNew()) {
122       $target_entity_type = $this->entityManager->getDefinition($this->entity->getTargetEntityTypeId());
123       $route_parameters = [
124         'field_config' => $this->entity->id(),
125       ] + FieldUI::getRouteBundleParameter($target_entity_type, $this->entity->getTargetBundle());
126       $url = new Url('entity.field_config.' . $target_entity_type->id() . '_field_delete_form', $route_parameters);
127
128       if ($this->getRequest()->query->has('destination')) {
129         $query = $url->getOption('query');
130         $query['destination'] = $this->getRequest()->query->get('destination');
131         $url->setOption('query', $query);
132       }
133       $actions['delete'] = [
134         '#type' => 'link',
135         '#title' => $this->t('Delete'),
136         '#url' => $url,
137         '#access' => $this->entity->access('delete'),
138         '#attributes' => [
139           'class' => ['button', 'button--danger'],
140         ],
141       ];
142     }
143
144     return $actions;
145   }
146
147   /**
148    * {@inheritdoc}
149    */
150   public function validateForm(array &$form, FormStateInterface $form_state) {
151     parent::validateForm($form, $form_state);
152
153     if (isset($form['default_value'])) {
154       $item = $form['#entity']->get($this->entity->getName());
155       $item->defaultValuesFormValidate($form['default_value'], $form, $form_state);
156     }
157   }
158
159   /**
160    * {@inheritdoc}
161    */
162   public function submitForm(array &$form, FormStateInterface $form_state) {
163     parent::submitForm($form, $form_state);
164
165     // Handle the default value.
166     $default_value = [];
167     if (isset($form['default_value'])) {
168       $items = $form['#entity']->get($this->entity->getName());
169       $default_value = $items->defaultValuesFormSubmit($form['default_value'], $form, $form_state);
170     }
171     $this->entity->setDefaultValue($default_value);
172   }
173
174   /**
175    * {@inheritdoc}
176    */
177   public function save(array $form, FormStateInterface $form_state) {
178     $this->entity->save();
179
180     drupal_set_message($this->t('Saved %label configuration.', ['%label' => $this->entity->getLabel()]));
181
182     $request = $this->getRequest();
183     if (($destinations = $request->query->get('destinations')) && $next_destination = FieldUI::getNextDestination($destinations)) {
184       $request->query->remove('destinations');
185       $form_state->setRedirectUrl($next_destination);
186     }
187     else {
188       $form_state->setRedirectUrl(FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle()));
189     }
190   }
191
192   /**
193    * The _title_callback for the field settings form.
194    *
195    * @param \Drupal\field\FieldConfigInterface $field_config
196    *   The field.
197    *
198    * @return string
199    *   The label of the field.
200    */
201   public function getTitle(FieldConfigInterface $field_config) {
202     return $field_config->label();
203   }
204
205 }