diff --git a/src/Plugin/ExtraFieldDisplayFormattedBase.php b/src/Plugin/ExtraFieldDisplayFormattedBase.php index 81b2fad..de89646 100644 --- a/src/Plugin/ExtraFieldDisplayFormattedBase.php +++ b/src/Plugin/ExtraFieldDisplayFormattedBase.php @@ -68,6 +68,7 @@ abstract class ExtraFieldDisplayFormattedBase extends ExtraFieldDisplayBase impl if ($children = Element::children($elements, TRUE)) { $build['#is_multiple'] = TRUE; + $build['#cache'] = !empty($elements['#cache']) ? $elements['#cache'] : []; // Without #children the field will not show up. $build['#children'] = ''; diff --git a/tests/extra_field_test/src/Plugin/ExtraField/Display/MultipleItemsFieldWithCacheDependencyTest.php b/tests/extra_field_test/src/Plugin/ExtraField/Display/MultipleItemsFieldWithCacheDependencyTest.php new file mode 100644 index 0000000..9af646a --- /dev/null +++ b/tests/extra_field_test/src/Plugin/ExtraField/Display/MultipleItemsFieldWithCacheDependencyTest.php @@ -0,0 +1,105 @@ +entityTypeManager = $entity_type_manager; + $this->renderer = $renderer; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager'), + $container->get('renderer') + ); + } + + /** + * {@inheritdoc} + */ + public function viewElements(ContentEntityInterface $entity) { + $build = []; + + $another_node_type_nodes = $this->entityTypeManager->getStorage('node')->loadByProperties(['type' => 'another_node_type']); + foreach ($another_node_type_nodes as $another_node) { + $build[] = ['#markup' => $another_node->label()]; + $this->renderer->addCacheableDependency($build, $another_node); + } + + return $build; + } + + /** + * {@inheritdoc} + */ + public function getLabel() { + return $this->t('Related pages'); + } + + /** + * {@inheritdoc} + */ + public function getLabelDisplay() { + return 'inline'; + } + +} diff --git a/tests/src/Functional/ExtraFieldBrowserTestBase.php b/tests/src/Functional/ExtraFieldBrowserTestBase.php index d50474d..55112a6 100644 --- a/tests/src/Functional/ExtraFieldBrowserTestBase.php +++ b/tests/src/Functional/ExtraFieldBrowserTestBase.php @@ -52,7 +52,7 @@ abstract class ExtraFieldBrowserTestBase extends BrowserTestBase { /** @var \Drupal\Core\Entity\ContentEntityInterface $node */ $node = \Drupal::entityTypeManager()->getStorage('node')->create([ 'type' => $contentType, - 'title' => $this->randomString(), + 'title' => $this->randomMachineName(), ]); $node->save(); diff --git a/tests/src/Functional/ExtraFieldDisplayFieldTest.php b/tests/src/Functional/ExtraFieldDisplayFieldTest.php index 493ed32..6bea5de 100644 --- a/tests/src/Functional/ExtraFieldDisplayFieldTest.php +++ b/tests/src/Functional/ExtraFieldDisplayFieldTest.php @@ -24,6 +24,13 @@ class ExtraFieldDisplayFieldTest extends ExtraFieldBrowserTestBase { */ protected $firstNode; + /** + * A second node. + * + * @var \Drupal\node\Entity\Node + */ + protected $secondNode; + /** * {@inheritdoc} */ @@ -31,6 +38,7 @@ class ExtraFieldDisplayFieldTest extends ExtraFieldBrowserTestBase { parent::setUp(); $this->firstNode = $this->createContent('first_node_type'); + $this->secondNode = $this->createContent('another_node_type'); $this->setupEnableExtraFieldTestModule(); } @@ -54,6 +62,11 @@ class ExtraFieldDisplayFieldTest extends ExtraFieldBrowserTestBase { $this->assertSession()->responseContains('
Noot
'); $this->assertSession()->responseContains('field--name-extra-field-multiple-text-test'); + // Test the output of field with cacheable dependency. + $this->assertSession()->responseContains('
Related pages
'); + $this->assertSession()->responseContains('
' . $this->secondNode->label() . '
'); + $this->assertCacheTag('node:' . $this->secondNode->id()); + // Test the output of field without content. $this->assertSession()->responseNotContains('field--name-extra-field-empty-formatted-test'); }