reverted: --- b/core/modules/text/src/Plugin/Field/FieldType/TextWithSummaryItem.php.orig +++ /dev/null @@ -1,128 +0,0 @@ - 0, - 'required_summary' => FALSE, - ] + parent::defaultFieldSettings(); - } - - /** - * {@inheritdoc} - */ - public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { - $properties = parent::propertyDefinitions($field_definition); - - $properties['summary'] = DataDefinition::create('string') - ->setLabel(new TranslatableMarkup('Summary')); - - $properties['summary_processed'] = DataDefinition::create('string') - ->setLabel(new TranslatableMarkup('Processed summary')) - ->setDescription(new TranslatableMarkup('The summary text with the text format applied.')) - ->setComputed(TRUE) - ->setClass('\Drupal\text\TextProcessed') - ->setSetting('text source', 'summary'); - - return $properties; - } - - /** - * {@inheritdoc} - */ - public static function schema(FieldStorageDefinitionInterface $field_definition) { - return [ - 'columns' => [ - 'value' => [ - 'type' => 'text', - 'size' => 'big', - ], - 'summary' => [ - 'type' => 'text', - 'size' => 'big', - ], - 'format' => [ - 'type' => 'varchar_ascii', - 'length' => 255, - ], - ], - 'indexes' => [ - 'format' => ['format'], - ], - ]; - } - - /** - * {@inheritdoc} - */ - public function isEmpty() { - $value = $this->get('summary')->getValue(); - return parent::isEmpty() && ($value === NULL || $value === ''); - } - - /** - * {@inheritdoc} - */ - public function fieldSettingsForm(array $form, FormStateInterface $form_state) { - $element = []; - $settings = $this->getSettings(); - - $element['display_summary'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Summary input'), - '#default_value' => $settings['display_summary'], - '#description' => $this->t('This allows authors to input an explicit summary, to be displayed instead of the automatically trimmed text when using the "Summary or trimmed" display type.'), - ]; - - $element['required_summary'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Require summary'), - '#description' => $this->t('The summary will also be visible when marked as required.'), - '#default_value' => $settings['required_summary'], - ]; - - return $element; - } - - /** - * {@inheritdoc} - */ - public function getConstraints() { - $constraints = parent::getConstraints(); - if ($this->getSetting('required_summary')) { - $manager = $this->getTypedDataManager()->getValidationConstraintManager(); - $constraints[] = $manager->create('ComplexData', [ - 'summary' => [ - 'NotNull' => [ - 'message' => $this->t('The summary field is required for @name', ['@name' => $this->getFieldDefinition()->getLabel()]), - ], - ], - ]); - } - return $constraints; - } - -} 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 @@ -7,6 +7,7 @@ use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\filter\Entity\FilterFormat; +use Drupal\filter\Render\FilteredMarkup; use Drupal\Tests\field\Functional\String\StringFieldTest; use Drupal\Tests\TestFileCreationTrait; @@ -22,9 +23,7 @@ } /** - * Modules to enable. - * - * @var array + * {@inheritdoc} */ protected static $modules = ['entity_test', 'file', 'field_ui']; @@ -183,15 +182,31 @@ $format2 = FilterFormat::create([ 'format' => mb_strtolower($this->randomMachineName()), 'name' => $this->randomMachineName(), + 'filters' => [ + 'filter_html' => [ + 'status' => 1, + 'settings' => [ + 'allowed_html' => '', + ], + ], + ], ]); $format2->save(); + // Create a third text format. + $format3 = FilterFormat::create([ + 'format' => mb_strtolower($this->randomMachineName()), + 'name' => $this->randomMachineName(), + ]); + $format3->save(); + // Grant access to both formats to the user. $roles = $this->webUser->getRoles(); $rid = $roles[0]; user_role_grant_permissions($rid, [ $format1->getPermissionName(), $format2->getPermissionName(), + $format3->getPermissionName(), ]); // Create a field with multiple formats allowed. @@ -227,6 +242,24 @@ $this->assertSession()->fieldExists("{$field_name}[0][value]", NULL); $this->assertSession()->optionExists("{$field_name}[0][format]", $format1->id()); $this->assertSession()->optionExists("{$field_name}[0][format]", $format2->id()); + $this->assertSession()->optionExists("{$field_name}[0][format]", $format3->id()); + + $filtered_markup = FilteredMarkup::create('
Hello World
'); + $edit = [ + "{$field_name}[0][value]" => $filtered_markup, + ]; + $this->submitForm($edit, 'Save'); + preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match); + $id = $match[1]; + $this->assertSession()->pageTextContains('entity_test ' . $id . ' has been created.'); + + // Display the entity. + $entity = EntityTest::load($id); + $display = $entity_display_repository->getViewDisplay($entity->getEntityTypeId(), $entity->bundle(), 'full'); + $content = $display->build($entity); + $rendered_entity = \Drupal::service('renderer')->renderRoot($content); + $this->assertStringContainsString('
', (string) $rendered_entity); + // Log back in as admin. $this->drupalLogin($this->adminUser); // Change field to allow only one format. @@ -241,6 +274,23 @@ $this->assertSession()->fieldExists("{$field_name}[0][value]", NULL); $this->assertSession()->fieldNotExists("{$field_name}[0][format]"); + // Retest the entity renders fine even though filter2 is disabled. + $entity = EntityTest::load($id); + $display = $entity_display_repository->getViewDisplay($entity->getEntityTypeId(), $entity->bundle(), 'full'); + $content = $display->build($entity); + $rendered_entity = \Drupal::service('renderer')->renderRoot($content); + $this->assertStringContainsString('
', (string) $rendered_entity); + + // Test when 2 of 3 formats are selected. + $field->setSetting('allowed_formats', [$format1->id(), $format2->id()]); + $field->save(); + $this->drupalGet('entity_test/add'); + // We should see the 'format' selector again. + $this->assertSession()->fieldExists("{$field_name}[0][value]", NULL); + $this->assertSession()->optionExists("{$field_name}[0][format]", $format1->id()); + $this->assertSession()->optionExists("{$field_name}[0][format]", $format2->id()); + $this->assertSession()->optionNotExists("{$field_name}[0][format]", $format3->id()); + // Change field to allow all formats by configuring none as allowed. $field->setSetting('allowed_formats', []); $field->save(); @@ -249,6 +299,7 @@ $this->assertSession()->fieldExists("{$field_name}[0][value]", NULL); $this->assertSession()->optionExists("{$field_name}[0][format]", $format1->id()); $this->assertSession()->optionExists("{$field_name}[0][format]", $format2->id()); + $this->assertSession()->optionExists("{$field_name}[0][format]", $format3->id()); } /** reverted: --- b/core/modules/text/tests/src/Kernel/TextWithSummaryItemTest.php +++ a/core/modules/text/tests/src/Kernel/TextWithSummaryItemTest.php @@ -67,7 +67,7 @@ $entity = $storage->create(); $entity->summary_field->value = $value = $this->randomMachineName(); $entity->summary_field->summary = $summary = $this->randomMachineName(); + $entity->summary_field->format = NULL; - $entity->summary_field->format = 'plain_text'; $entity->name->value = $this->randomMachineName(); $entity->save(); @@ -76,7 +76,7 @@ $this->assertInstanceOf(FieldItemInterface::class, $entity->summary_field[0]); $this->assertEquals($value, $entity->summary_field->value); $this->assertEquals($summary, $entity->summary_field->summary); + $this->assertNull($entity->summary_field->format); - $this->assertEquals('plain_text', $entity->summary_field->format); // Even if no format is given, if text processing is enabled, the default // format is used. $this->assertEquals("

{$value}

\n", $entity->summary_field->processed); @@ -115,9 +115,6 @@ $this->field = FieldConfig::create([ 'field_storage' => $this->fieldStorage, 'bundle' => $entity_type, - 'settings' => [ - 'allowed_formats' => ['plain_text'], - ], ]); $this->field->save(); } diff -u b/core/modules/text/text.post_update.php b/core/modules/text/text.post_update.php --- b/core/modules/text/text.post_update.php +++ b/core/modules/text/text.post_update.php @@ -27,7 +27,7 @@ ->update($sandbox, 'field_config', function (FieldConfigInterface $field_config) { $class = get_class($field_config); // Deal only with text fields and descendants. - if ($class == TextItemBase::class || is_subclass_of($class, TextItemBase::class)) { + if (is_a($class, TextItemBase::class, TRUE)) { // Get the existing allowed_formats setting. $allowed_formats = $field_config->get('settings.allowed_formats'); if (!is_array($allowed_formats) && empty($allowed_formats)) {