3 namespace Drupal\field_ui\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Field\AllowedTagsXssTrait;
7 use Drupal\Core\Field\FieldFilteredMarkup;
8 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\field\FieldConfigInterface;
11 use Drupal\field_ui\FieldUI;
14 * Provides a form for the field settings form.
18 class FieldConfigEditForm extends EntityForm {
20 use AllowedTagsXssTrait;
23 * The entity being used by this form.
25 * @var \Drupal\field\FieldConfigInterface
32 public function form(array $form, FormStateInterface $form_state) {
33 $form = parent::form($form, $form_state);
35 $field_storage = $this->entity->getFieldStorageDefinition();
36 $bundles = $this->entityManager->getBundleInfo($this->entity->getTargetEntityTypeId());
38 $form_title = $this->t('%field settings for %bundle', [
39 '%field' => $this->entity->getLabel(),
40 '%bundle' => $bundles[$this->entity->getTargetBundle()]['label'],
42 $form['#title'] = $form_title;
44 if ($field_storage->isLocked()) {
46 '#markup' => $this->t('The field %field is locked and cannot be edited.', ['%field' => $this->entity->getLabel()]),
51 // Build the configurable field values.
53 '#type' => 'textfield',
54 '#title' => $this->t('Label'),
55 '#default_value' => $this->entity->getLabel() ?: $field_storage->getName(),
60 $form['description'] = [
61 '#type' => 'textarea',
62 '#title' => $this->t('Help text'),
63 '#default_value' => $this->entity->getDescription(),
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.'),
70 '#type' => 'checkbox',
71 '#title' => $this->t('Required field'),
72 '#default_value' => $this->entity->isRequired(),
76 // Create an arbitrary entity object (used by the 'default value' widget).
78 'entity_type' => $this->entity->getTargetEntityTypeId(),
79 'bundle' => $this->entity->getTargetBundle(),
82 $form['#entity'] = _field_create_entity_from_ids($ids);
83 $items = $form['#entity']->get($this->entity->getName());
84 $item = $items->first() ?: $items->appendItem();
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().
92 $form['settings'] += $item->fieldSettingsForm($form, $form_state);
93 $form['third_party_settings'] = [
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'),
105 '#description' => $this->t('The default value for this field, used when creating new content.'),
108 $form['default_value'] = $element;
117 protected function actions(array $form, FormStateInterface $form_state) {
118 $actions = parent::actions($form, $form_state);
119 $actions['submit']['#value'] = $this->t('Save settings');
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);
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);
133 $actions['delete'] = [
135 '#title' => $this->t('Delete'),
137 '#access' => $this->entity->access('delete'),
139 'class' => ['button', 'button--danger'],
150 public function validateForm(array &$form, FormStateInterface $form_state) {
151 parent::validateForm($form, $form_state);
153 if (isset($form['default_value'])) {
154 $item = $form['#entity']->get($this->entity->getName());
155 $item->defaultValuesFormValidate($form['default_value'], $form, $form_state);
162 public function submitForm(array &$form, FormStateInterface $form_state) {
163 parent::submitForm($form, $form_state);
165 // Handle the 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);
171 $this->entity->setDefaultValue($default_value);
177 public function save(array $form, FormStateInterface $form_state) {
178 $this->entity->save();
180 drupal_set_message($this->t('Saved %label configuration.', ['%label' => $this->entity->getLabel()]));
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);
188 $form_state->setRedirectUrl(FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle()));
193 * The _title_callback for the field settings form.
195 * @param \Drupal\field\FieldConfigInterface $field_config
199 * The label of the field.
201 public function getTitle(FieldConfigInterface $field_config) {
202 return $field_config->label();