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.
16 class FieldConfigEditForm extends EntityForm {
18 use AllowedTagsXssTrait;
21 * The entity being used by this form.
23 * @var \Drupal\field\FieldConfigInterface
30 public function form(array $form, FormStateInterface $form_state) {
31 $form = parent::form($form, $form_state);
33 $field_storage = $this->entity->getFieldStorageDefinition();
34 $bundles = $this->entityManager->getBundleInfo($this->entity->getTargetEntityTypeId());
36 $form_title = $this->t('%field settings for %bundle', [
37 '%field' => $this->entity->getLabel(),
38 '%bundle' => $bundles[$this->entity->getTargetBundle()]['label'],
40 $form['#title'] = $form_title;
42 if ($field_storage->isLocked()) {
44 '#markup' => $this->t('The field %field is locked and cannot be edited.', ['%field' => $this->entity->getLabel()]),
49 // Build the configurable field values.
51 '#type' => 'textfield',
52 '#title' => $this->t('Label'),
53 '#default_value' => $this->entity->getLabel() ?: $field_storage->getName(),
58 $form['description'] = [
59 '#type' => 'textarea',
60 '#title' => $this->t('Help text'),
61 '#default_value' => $this->entity->getDescription(),
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.'),
68 '#type' => 'checkbox',
69 '#title' => $this->t('Required field'),
70 '#default_value' => $this->entity->isRequired(),
74 // Create an arbitrary entity object (used by the 'default value' widget).
76 'entity_type' => $this->entity->getTargetEntityTypeId(),
77 'bundle' => $this->entity->getTargetBundle(),
80 $form['#entity'] = _field_create_entity_from_ids($ids);
81 $items = $form['#entity']->get($this->entity->getName());
82 $item = $items->first() ?: $items->appendItem();
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().
90 $form['settings'] += $item->fieldSettingsForm($form, $form_state);
91 $form['third_party_settings'] = [
96 // Add handling for default value.
97 if ($element = $items->defaultValuesForm($form, $form_state)) {
98 $element = array_merge($element, [
100 '#title' => $this->t('Default value'),
103 '#description' => $this->t('The default value for this field, used when creating new content.'),
106 $form['default_value'] = $element;
115 protected function actions(array $form, FormStateInterface $form_state) {
116 $actions = parent::actions($form, $form_state);
117 $actions['submit']['#value'] = $this->t('Save settings');
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);
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);
131 $actions['delete'] = [
133 '#title' => $this->t('Delete'),
135 '#access' => $this->entity->access('delete'),
137 'class' => ['button', 'button--danger'],
148 public function validateForm(array &$form, FormStateInterface $form_state) {
149 parent::validateForm($form, $form_state);
151 if (isset($form['default_value'])) {
152 $item = $form['#entity']->get($this->entity->getName());
153 $item->defaultValuesFormValidate($form['default_value'], $form, $form_state);
160 public function submitForm(array &$form, FormStateInterface $form_state) {
161 parent::submitForm($form, $form_state);
163 // Handle the 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);
169 $this->entity->setDefaultValue($default_value);
175 public function save(array $form, FormStateInterface $form_state) {
176 $this->entity->save();
178 drupal_set_message($this->t('Saved %label configuration.', ['%label' => $this->entity->getLabel()]));
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);
186 $form_state->setRedirectUrl(FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle()));
191 * The _title_callback for the field settings form.
193 * @param \Drupal\field\FieldConfigInterface $field_config
197 * The label of the field.
199 public function getTitle(FieldConfigInterface $field_config) {
200 return $field_config->label();