Issue #2995893 by tim.plunkett, Dylan Donkersgoed: Layout builder chokes on form exceptions that are part of natural form processing

merge-requests/1119/head
Lee Rowlands 2019-03-28 07:51:02 +10:00
parent 01efa20b62
commit d9e9f0c461
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
2 changed files with 24 additions and 0 deletions

View File

@ -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()]);

View File

@ -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();
}
}