diff --git a/core/config/schema/core.entity.schema.yml b/core/config/schema/core.entity.schema.yml index 174925d..e846ca4 100644 --- a/core/config/schema/core.entity.schema.yml +++ b/core/config/schema/core.entity.schema.yml @@ -374,7 +374,7 @@ field.formatter.settings.entity_reference_inline_settings: type: string settings: label: 'Formatter settings' - type: field.formatter.settings.[%parent.type] + type: field.formatter.settings.[type] label: label: 'Target field label visibility setting' type: string diff --git a/core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php b/core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php index 1473375..1938781 100644 --- a/core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php +++ b/core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php @@ -4,8 +4,10 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableMetadata; +use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter; +use Drupal\entity_test\Entity\EntityTest; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait; @@ -60,6 +62,9 @@ class EntityReferenceFormatterTest extends EntityKernelTestBase { */ protected $unsavedReferencedEntity; + /** + * {@inheritdoc} + */ protected function setUp() { parent::setUp(); @@ -402,6 +407,129 @@ public function testLabelFormatter() { } /** + * Tests entity_reference_inline_settings formatter output. + * + * @param string|null $label_option + * The value for the "label" formatter option. + * @param string $expected_output + * The expected output. + * + * @dataProvider providerTestRenderInlineSettingsFormatter + */ + public function testRenderInlineSettingsFormatter($label_option, $expected_output) { + + $field_storage = FieldStorageConfig::create([ + 'field_name' => 'test_er_field', + 'entity_type' => 'entity_test', + 'type' => 'entity_reference', + 'settings' => [ + 'target_type' => 'entity_test', + ], + ]); + $field_storage->save(); + + $field_config = FieldConfig::create([ + 'field_name' => 'test_er_field', + 'entity_type' => 'entity_test', + 'bundle' => 'entity_test', + ]); + $field_config->save(); + + $parent_entity_view_display = EntityViewDisplay::load('entity_test.entity_test.default'); + $parent_entity_view_display->setComponent('test_er_field', [ + 'type' => 'entity_reference_inline_settings', + 'settings' => [ + 'field_name' => 'name', + 'type' => 'string', + 'settings' => [], + 'label' => $label_option, + 'show_basefields' => FALSE, + ], + ])->save(); + + $child_entity = EntityTest::create([ + 'name' => ['child name'], + ]); + $child_entity->save(); + + $entity = EntityTest::create([ + 'test_er_field' => [[ + 'target_id' => $child_entity->id(), + ], + ], + ]); + $entity->save(); + + $build = $parent_entity_view_display->build($entity); + + $this->container->get('renderer')->renderRoot($build); + + $this->assertEquals($expected_output, $build['test_er_field']['#markup']); + } + + /** + * Data provider for ::testRenderInlineSettingsFormatter(). + */ + public function providerTestRenderInlineSettingsFormatter() { + $output_with_label = << +
test_er_field
+
+
+
Name
+
child name
+
+
+ + +EXPECTED; + $output_with_label_inline = << +
test_er_field
+
+
+
Name
+
child name
+
+
+ + +EXPECTED; + $output_label_hidden = << +
test_er_field
+
+
child name
+
+ + +EXPECTED; + $output_label_visually_hidden = << +
test_er_field
+
+
+
Name
+
child name
+
+
+ + +EXPECTED; + + return [ + ['above', $output_with_label], + ['inline', $output_with_label_inline], + ['hidden', $output_label_hidden], + ['visually_hidden', $output_label_visually_hidden], + ]; + } + + /** * Sets field values and returns a render array as built by * \Drupal\Core\Field\FieldItemListInterface::view(). *