diff --git a/core/modules/layout_builder/src/Plugin/Block/FieldBlock.php b/core/modules/layout_builder/src/Plugin/Block/FieldBlock.php index dbd7a0da504..645a09488ef 100644 --- a/core/modules/layout_builder/src/Plugin/Block/FieldBlock.php +++ b/core/modules/layout_builder/src/Plugin/Block/FieldBlock.php @@ -14,6 +14,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FormatterInterface; use Drupal\Core\Field\FormatterPluginManager; +use Drupal\Core\Form\EnforcedResponseException; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContextAwarePluginInterface; @@ -155,6 +156,10 @@ class FieldBlock extends BlockBase implements ContextAwarePluginInterface, Conta try { $build = $entity->get($this->fieldName)->view($display_settings); } + // @todo Remove in https://www.drupal.org/project/drupal/issues/2367555. + catch (EnforcedResponseException $e) { + throw $e; + } catch (\Exception $e) { $build = []; $this->logger->warning('The field "%field" failed to render with the error of "%error".', ['%field' => $this->fieldName, '%error' => $e->getMessage()]); diff --git a/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php b/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php index de9542f8bc9..4675046a89e 100644 --- a/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php +++ b/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php @@ -10,6 +10,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterPluginManager; +use Drupal\Core\Form\EnforcedResponseException; use Drupal\Core\Plugin\Context\EntityContextDefinition; use Drupal\Core\Session\AccountInterface; use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; @@ -20,6 +21,7 @@ use Prophecy\Promise\ReturnPromise; use Prophecy\Promise\ThrowPromise; use Prophecy\Prophecy\ProphecyInterface; use Psr\Log\LoggerInterface; +use Symfony\Component\HttpFoundation\Response; /** * @coversDefaultClass \Drupal\layout_builder\Plugin\Block\FieldBlock @@ -285,4 +287,21 @@ class FieldBlockTest extends EntityKernelTestBase { return $data; } + /** + * Tests a field block that throws a form exception. + * + * @todo Remove in https://www.drupal.org/project/drupal/issues/2367555. + */ + public function testBuildWithFormException() { + $field = $this->prophesize(FieldItemListInterface::class); + $field->view(Argument::type('array'))->willThrow(new EnforcedResponseException(new Response())); + + $entity = $this->prophesize(FieldableEntityInterface::class); + $entity->get('the_field_name')->willReturn($field->reveal()); + + $block = $this->getTestBlock($entity); + $this->setExpectedException(EnforcedResponseException::class); + $block->build(); + } + }