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