Loading core/modules/text/src/Plugin/Field/FieldWidget/TextareaWithSummaryWidget.php +4 −1 Original line number Diff line number Diff line Loading @@ -100,8 +100,11 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen */ public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) { $element = parent::errorElement($element, $violation, $form, $form_state); if ($element === FALSE) { return FALSE; } $property_path_array = explode('.', $violation->getPropertyPath()); return ($element === FALSE) ? FALSE : $element[$property_path_array[1]]; return count($property_path_array) > 1 ? $element[$property_path_array[1]] : $element; } } core/modules/text/tests/modules/text_test/src/Hook/TextTestHooks.php 0 → 100644 +29 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\text_test\Hook; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Hook\Attribute\Hook; use Drupal\Core\State\StateInterface; /** * Hook implementations for text_test. */ class TextTestHooks { public function __construct(protected readonly StateInterface $state) {} /** * Implements hook_entity_bundle_field_info_alter(). */ #[Hook('entity_bundle_field_info_alter')] public function entityBundleFieldInfoAlter(&$fields, EntityTypeInterface $entity_type, $bundle): void { if (($field_name = $this->state->get('field_test_constraint', FALSE)) && $entity_type->id() == 'node') { /** @var \Drupal\field\Entity\FieldConfig[] $fields */ $fields[$field_name]->addConstraint('UniqueField'); } } } core/modules/text/tests/modules/text_test/text_test.info.yml 0 → 100644 +7 −0 Original line number Diff line number Diff line name: 'Text test' type: module description: 'Provides test hook implementations for text tests' package: Testing version: VERSION dependencies: - drupal:text core/modules/text/tests/src/Functional/TextareaWithSummaryTest.php 0 → 100644 +93 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\text\Functional; use Drupal\Tests\BrowserTestBase; use PHPUnit\Framework\Attributes\Group; /** * Tests the functionality of the text_textarea_with_summary widget. */ #[Group('text')] class TextareaWithSummaryTest extends BrowserTestBase { /** * {@inheritdoc} */ protected static $modules = ['text_test', 'node']; /** * {@inheritdoc} */ protected $defaultTheme = 'stark'; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); $this->drupalCreateContentType(['type' => 'page']); $account = $this->drupalCreateUser([ 'create page content', 'edit own page content', ]); $this->drupalLogin($account); } /** * Tests validation constraints for a field with delta-specific violations. * * @see \Drupal\text_test\Hook\TextTestHooks::entityBundleFieldInfoAlter() */ public function testTextAreaWithSummaryValidation(): void { // Create a field for validation testing. $entity_type_id = 'node'; $field_name = $this->randomMachineName(); $entity_type_manager = $this->container->get('entity_type.manager'); $field_storage = $entity_type_manager->getStorage('field_storage_config')->create([ 'field_name' => $field_name, 'entity_type' => $entity_type_id, 'type' => 'text_with_summary', 'cardinality' => 2, ]); $field_storage->save(); $bundle_id = 'page'; $field_label = $this->randomMachineName() . '_label'; $entity_type_manager->getStorage('field_config')->create([ 'field_storage' => $field_storage, 'bundle' => $bundle_id, 'label' => $field_label, 'settings' => [ 'display_summary' => TRUE, 'required_summary' => FALSE, ], ])->save(); // Add the created field to the form. $this->container ->get('entity_display.repository') ->getFormDisplay($entity_type_id, $bundle_id) ->setComponent($field_name, ['type' => 'text_textarea_with_summary']) ->save(); // Enable delta-specific validation for the field. $this->container->get('state')->set('field_test_constraint', $field_name); $this->container->get('entity_field.manager')->clearCachedFieldDefinitions(); // Create a node to verify that validation works. $value = $this->randomMachineName(); $this->drupalGet('node/add/page'); $edit = [ 'title[0][value]' => $this->randomMachineName(), "{$field_name}[0][value]" => $value, "{$field_name}[1][value]" => $value, ]; $this->submitForm($edit, 'Save'); $this->assertSession()->statusMessageContains("A content item with $field_label $value already exists."); } } Loading
core/modules/text/src/Plugin/Field/FieldWidget/TextareaWithSummaryWidget.php +4 −1 Original line number Diff line number Diff line Loading @@ -100,8 +100,11 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen */ public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) { $element = parent::errorElement($element, $violation, $form, $form_state); if ($element === FALSE) { return FALSE; } $property_path_array = explode('.', $violation->getPropertyPath()); return ($element === FALSE) ? FALSE : $element[$property_path_array[1]]; return count($property_path_array) > 1 ? $element[$property_path_array[1]] : $element; } }
core/modules/text/tests/modules/text_test/src/Hook/TextTestHooks.php 0 → 100644 +29 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\text_test\Hook; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Hook\Attribute\Hook; use Drupal\Core\State\StateInterface; /** * Hook implementations for text_test. */ class TextTestHooks { public function __construct(protected readonly StateInterface $state) {} /** * Implements hook_entity_bundle_field_info_alter(). */ #[Hook('entity_bundle_field_info_alter')] public function entityBundleFieldInfoAlter(&$fields, EntityTypeInterface $entity_type, $bundle): void { if (($field_name = $this->state->get('field_test_constraint', FALSE)) && $entity_type->id() == 'node') { /** @var \Drupal\field\Entity\FieldConfig[] $fields */ $fields[$field_name]->addConstraint('UniqueField'); } } }
core/modules/text/tests/modules/text_test/text_test.info.yml 0 → 100644 +7 −0 Original line number Diff line number Diff line name: 'Text test' type: module description: 'Provides test hook implementations for text tests' package: Testing version: VERSION dependencies: - drupal:text
core/modules/text/tests/src/Functional/TextareaWithSummaryTest.php 0 → 100644 +93 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\text\Functional; use Drupal\Tests\BrowserTestBase; use PHPUnit\Framework\Attributes\Group; /** * Tests the functionality of the text_textarea_with_summary widget. */ #[Group('text')] class TextareaWithSummaryTest extends BrowserTestBase { /** * {@inheritdoc} */ protected static $modules = ['text_test', 'node']; /** * {@inheritdoc} */ protected $defaultTheme = 'stark'; /** * {@inheritdoc} */ protected function setUp(): void { parent::setUp(); $this->drupalCreateContentType(['type' => 'page']); $account = $this->drupalCreateUser([ 'create page content', 'edit own page content', ]); $this->drupalLogin($account); } /** * Tests validation constraints for a field with delta-specific violations. * * @see \Drupal\text_test\Hook\TextTestHooks::entityBundleFieldInfoAlter() */ public function testTextAreaWithSummaryValidation(): void { // Create a field for validation testing. $entity_type_id = 'node'; $field_name = $this->randomMachineName(); $entity_type_manager = $this->container->get('entity_type.manager'); $field_storage = $entity_type_manager->getStorage('field_storage_config')->create([ 'field_name' => $field_name, 'entity_type' => $entity_type_id, 'type' => 'text_with_summary', 'cardinality' => 2, ]); $field_storage->save(); $bundle_id = 'page'; $field_label = $this->randomMachineName() . '_label'; $entity_type_manager->getStorage('field_config')->create([ 'field_storage' => $field_storage, 'bundle' => $bundle_id, 'label' => $field_label, 'settings' => [ 'display_summary' => TRUE, 'required_summary' => FALSE, ], ])->save(); // Add the created field to the form. $this->container ->get('entity_display.repository') ->getFormDisplay($entity_type_id, $bundle_id) ->setComponent($field_name, ['type' => 'text_textarea_with_summary']) ->save(); // Enable delta-specific validation for the field. $this->container->get('state')->set('field_test_constraint', $field_name); $this->container->get('entity_field.manager')->clearCachedFieldDefinitions(); // Create a node to verify that validation works. $value = $this->randomMachineName(); $this->drupalGet('node/add/page'); $edit = [ 'title[0][value]' => $this->randomMachineName(), "{$field_name}[0][value]" => $value, "{$field_name}[1][value]" => $value, ]; $this->submitForm($edit, 'Save'); $this->assertSession()->statusMessageContains("A content item with $field_label $value already exists."); } }