diff -u b/core/modules/ckeditor/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php b/core/modules/ckeditor/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php --- b/core/modules/ckeditor/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php +++ b/core/modules/ckeditor/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php @@ -65,10 +65,7 @@ 'field_storage' => $field_storage, 'bundle' => 'page', 'label' => 'Body', - 'settings' => [ - 'display_summary' => TRUE, - 'allowed_formats' => [], - ], + 'settings' => ['display_summary' => TRUE], 'required' => TRUE, ])->save(); @@ -76,7 +73,10 @@ 'field_storage' => $field_storage, 'bundle' => 'page', 'label' => 'Body', - 'settings' => ['display_summary' => TRUE], + 'settings' => [ + 'display_summary' => TRUE, + 'allowed_formats' => [], + ], 'required' => TRUE, ])->save(); diff -u b/core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php b/core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php --- b/core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php +++ b/core/modules/text/src/Plugin/Field/FieldType/TextItemBase.php @@ -31,15 +31,29 @@ $element['allowed_formats'] = [ '#type' => 'checkboxes', '#title' => $this->t('Allowed text formats'), - '#options' => $this->getProperties()['format']->getPossibleOptions(), + '#options' => $this->get('format')->getPossibleOptions(), '#default_value' => $settings['allowed_formats'], '#description' => $this->t('Select the allowed text formats. If no formats are selected, all available text formats will be displayed to the user.'), + '#element_validate' => [[static::class, 'validateAllowedFormats']] ]; return $element; } /** + * Render API callback: Processes the allowed formats value. + * + * Ensure the element's value is an indexed array of selected format IDs. + * + * This function is assigned as an #element_validate callback in + * ::fieldSettingsForm(). + */ + public static function validateAllowedFormats(array &$element, FormStateInterface $form_state) { + $value = array_values(array_filter($element['#value'])); + $form_state->setValueForElement($element, $value); + } + + /** * {@inheritdoc} */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { diff -u b/core/modules/text/src/Plugin/Field/FieldWidget/TextareaWidget.php b/core/modules/text/src/Plugin/Field/FieldWidget/TextareaWidget.php --- b/core/modules/text/src/Plugin/Field/FieldWidget/TextareaWidget.php +++ b/core/modules/text/src/Plugin/Field/FieldWidget/TextareaWidget.php @@ -34,18 +34,15 @@ */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $main_widget = parent::formElement($items, $delta, $element, $form, $form_state); - $allowed_formats_setting = $this->getFieldSetting('allowed_formats'); + $allowed_formats = $this->getFieldSetting('allowed_formats'); $element = $main_widget['value']; $element['#type'] = 'text_format'; $element['#format'] = $items[$delta]->format; $element['#base_type'] = $main_widget['value']['#type']; - if (is_array($allowed_formats_setting)) { - $allowed_formats = array_filter($allowed_formats_setting); - if (!empty($allowed_formats) && !$this->isDefaultValueWidget($form_state)) { - $element['#allowed_formats'] = $allowed_formats; - } + if ($allowed_formats && !$this->isDefaultValueWidget($form_state)) { + $element['#allowed_formats'] = $allowed_formats; } return $element; diff -u b/core/modules/text/src/Plugin/Field/FieldWidget/TextfieldWidget.php b/core/modules/text/src/Plugin/Field/FieldWidget/TextfieldWidget.php --- b/core/modules/text/src/Plugin/Field/FieldWidget/TextfieldWidget.php +++ b/core/modules/text/src/Plugin/Field/FieldWidget/TextfieldWidget.php @@ -32,7 +32,7 @@ $element['#format'] = isset($items[$delta]->format) ? $items[$delta]->format : NULL; $element['#base_type'] = $main_widget['value']['#type']; - if (!empty($allowed_formats) && !$this->isDefaultValueWidget($form_state)) { + if ($allowed_formats && !$this->isDefaultValueWidget($form_state)) { $element['#allowed_formats'] = $allowed_formats; } diff -u b/core/modules/text/tests/src/Functional/TextFieldTest.php b/core/modules/text/tests/src/Functional/TextFieldTest.php --- b/core/modules/text/tests/src/Functional/TextFieldTest.php +++ b/core/modules/text/tests/src/Functional/TextFieldTest.php @@ -216,7 +216,8 @@ $this->drupalLogin($this->webUser); $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[0][value]", NULL, 'Widget is displayed'); - $this->assertFieldByName("{$field_name}[0][format]", NULL, 'Format selector is displayed'); + $this->assertSession()->optionExists("{$field_name}[0][format]", $format1->id()); + $this->assertSession()->optionExists("{$field_name}[0][format]", $format2->id()); // Change field to allow only one format. $field->setSetting('allowed_formats', [$format1->id()]); @@ -225,6 +226,15 @@ $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[0][value]", NULL, 'Widget is displayed'); $this->assertNoFieldByName("{$field_name}[0][format]", NULL, 'Format selector is not displayed'); + + // Change field to allow all formats by configuring none as allowed. + $field->setSetting('allowed_formats', []); + $field->save(); + $this->drupalGet('entity_test/add'); + // We should see the 'format' selector again. + $this->assertFieldByName("{$field_name}[0][value]", NULL, 'Widget is displayed'); + $this->assertSession()->optionExists("{$field_name}[0][format]", $format1->id()); + $this->assertSession()->optionExists("{$field_name}[0][format]", $format2->id()); } /**