Loading core/modules/layout_builder/src/Plugin/Block/FieldBlock.php +7 −2 Changes for core/modules/layout_builder/src/Plugin/Block/FieldBlock.php: 7 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContextAwarePluginInterface; use Drupal\Core\Render\Element; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\field\FieldConfigInterface; Loading Loading @@ -154,13 +155,17 @@ protected function getEntity() { * {@inheritdoc} */ public function build() { $cacheability = CacheableMetadata::createFromObject($this); $display_settings = $this->getConfiguration()['formatter']; $display_settings['third_party_settings']['layout_builder']['view_mode'] = $this->getContextValue('view_mode'); $entity = $this->getEntity(); try { $build = []; $view = $entity->get($this->fieldName)->view($display_settings); if ($view) { if (Element::isEmpty($view)) { $cacheability->addCacheableDependency(CacheableMetadata::createFromRenderArray($view)); } else { $build = [$view]; } } Loading @@ -175,7 +180,7 @@ public function build() { '%error' => $e->getMessage(), ]); } CacheableMetadata::createFromRenderArray($build)->addCacheableDependency($this)->applyTo($build); $cacheability->applyTo($build); return $build; } Loading core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/Field/FieldFormatter/BlankFormatter.php +31 Changes for core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/Field/FieldFormatter/BlankFormatter.php: 31 added lines, 0 removed lines. Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\layout_builder_test\Plugin\Field\FieldFormatter; use Drupal\Core\Field\Attribute\FieldFormatter; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; use Drupal\Core\StringTranslation\TranslatableMarkup; /** * A blank field formatter. */ #[FieldFormatter( id: 'blank', label: new TranslatableMarkup('Blank'), field_types: [ 'string', ], )] final class BlankFormatter extends FormatterBase { /** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode): array { return []; } } core/modules/layout_builder/tests/src/Functional/LayoutBuilderBlocksTest.php +29 Changes for core/modules/layout_builder/tests/src/Functional/LayoutBuilderBlocksTest.php: 29 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -250,6 +250,35 @@ public function testBlockPlaceholder(): void { $assert_session->pageTextContains($block_content); } /** * Tests the usage of placeholders for empty field blocks. */ public function testFieldBlockPlaceholder(): void { $assert_session = $this->assertSession(); $page = $this->getSession()->getPage(); $this->drupalLogin($this->drupalCreateUser([ 'configure any layout', 'administer node display', 'administer node fields', ])); $label = 'Field block test'; $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field'; $this->fieldUIAddNewField($field_ui_prefix, 'field_block_test', $label, 'string'); $this->drupalGet("{$field_ui_prefix}/display/default"); $this->submitForm(['layout[enabled]' => TRUE], 'Save'); $this->drupalGet("$field_ui_prefix/display/default/layout"); $this->clickLink('Add block'); $this->clickLink($label); $page->fillField('settings[formatter][type]', 'blank'); $page->pressButton('Add block'); $assert_session->pageTextContains("Placeholder for the \"{$label}\" field"); } /** * Tests the ability to use a specified block label for field blocks. */ Loading Loading
core/modules/layout_builder/src/Plugin/Block/FieldBlock.php +7 −2 Changes for core/modules/layout_builder/src/Plugin/Block/FieldBlock.php: 7 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContextAwarePluginInterface; use Drupal\Core\Render\Element; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\field\FieldConfigInterface; Loading Loading @@ -154,13 +155,17 @@ protected function getEntity() { * {@inheritdoc} */ public function build() { $cacheability = CacheableMetadata::createFromObject($this); $display_settings = $this->getConfiguration()['formatter']; $display_settings['third_party_settings']['layout_builder']['view_mode'] = $this->getContextValue('view_mode'); $entity = $this->getEntity(); try { $build = []; $view = $entity->get($this->fieldName)->view($display_settings); if ($view) { if (Element::isEmpty($view)) { $cacheability->addCacheableDependency(CacheableMetadata::createFromRenderArray($view)); } else { $build = [$view]; } } Loading @@ -175,7 +180,7 @@ public function build() { '%error' => $e->getMessage(), ]); } CacheableMetadata::createFromRenderArray($build)->addCacheableDependency($this)->applyTo($build); $cacheability->applyTo($build); return $build; } Loading
core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/Field/FieldFormatter/BlankFormatter.php +31 Changes for core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/Field/FieldFormatter/BlankFormatter.php: 31 added lines, 0 removed lines. Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\layout_builder_test\Plugin\Field\FieldFormatter; use Drupal\Core\Field\Attribute\FieldFormatter; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; use Drupal\Core\StringTranslation\TranslatableMarkup; /** * A blank field formatter. */ #[FieldFormatter( id: 'blank', label: new TranslatableMarkup('Blank'), field_types: [ 'string', ], )] final class BlankFormatter extends FormatterBase { /** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode): array { return []; } }
core/modules/layout_builder/tests/src/Functional/LayoutBuilderBlocksTest.php +29 Changes for core/modules/layout_builder/tests/src/Functional/LayoutBuilderBlocksTest.php: 29 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -250,6 +250,35 @@ public function testBlockPlaceholder(): void { $assert_session->pageTextContains($block_content); } /** * Tests the usage of placeholders for empty field blocks. */ public function testFieldBlockPlaceholder(): void { $assert_session = $this->assertSession(); $page = $this->getSession()->getPage(); $this->drupalLogin($this->drupalCreateUser([ 'configure any layout', 'administer node display', 'administer node fields', ])); $label = 'Field block test'; $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field'; $this->fieldUIAddNewField($field_ui_prefix, 'field_block_test', $label, 'string'); $this->drupalGet("{$field_ui_prefix}/display/default"); $this->submitForm(['layout[enabled]' => TRUE], 'Save'); $this->drupalGet("$field_ui_prefix/display/default/layout"); $this->clickLink('Add block'); $this->clickLink($label); $page->fillField('settings[formatter][type]', 'blank'); $page->pressButton('Add block'); $assert_session->pageTextContains("Placeholder for the \"{$label}\" field"); } /** * Tests the ability to use a specified block label for field blocks. */ Loading