Problem/Motivation
Let's assume you have some old data somewhere in the database, pointing to a non existing entity.
ERR doesn't protect against that case, and it fatals:
Recoverable fatal error: Argument 1 passed to Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase::checkAccess() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in
core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php on line 55 and defined in Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase->checkAccess() (line 182 of core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php).
Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase->checkAccess(NULL) (Line: 55)
Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase->getEntitiesToView(Object, 'en') (Line: 124)
Drupal\entity_reference_revisions\Plugin\Field\FieldFormatter\EntityReferenceRevisionsEntityFormatter->viewElements(Object, 'en') (Line: 80)
Drupal\Core\Field\FormatterBase->view(Object, NULL) (Line: 76)
Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase->view(Object, NU
Proposed resolution
Add some kind of protection.
Remaining tasks
User interface changes
API changes
Data model changes
Comments
Comment #2
dawehnerMaybe something like this would work
Comment #3
dawehnerThis time also with a test.
Comment #8
miro_dietikerYeah i could easily imagine this to happen if ERR is used for non-paragraphs use case - such as pointing to node targets.
For Paragraphs, it's not a release blocker, because there is no UI to list and delete the paragraphs.
Comment #9
dawehnerI'm still trying to reproduce those failures on my local system. In case someone has an insight/vision or just a miracle I would be super happy about it.
Comment #10
eric heydrichMaybe this Error-Log could help you:
The website encountered an unexpected error. Please try again later.
TypeError: Argument 1 passed to Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase::checkAccess() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in /www/htdocs/w01418d9/drupal8/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php on line 55 in Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase->checkAccess() (line 182 of core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php).
Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase->checkAccess(NULL) (Line: 55)
Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase->getEntitiesToView(Object, 'de') (Line: 131)
Drupal\entity_reference_revisions\Plugin\Field\FieldFormatter\EntityReferenceRevisionsEntityFormatter->viewElements(Object, 'de') (Line: 80)
Drupal\Core\Field\FormatterBase->view(Object, 'de') (Line: 76)
Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase->view(Object, NULL) (Line: 259)
Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple(Array) (Line: 303)
Drupal\Core\Entity\EntityViewBuilder->buildComponents(Array, Array, Array, 'vorschaubild') (Line: 24)
Drupal\node\NodeViewBuilder->buildComponents(Array, Array, Array, 'vorschaubild') (Line: 246)
Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 203)
Drupal\Core\Entity\EntityViewBuilder->build(Array)
call_user_func(Array, Array) (Line: 381)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 226)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 144)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 98)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 77)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 628)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Comment #11
berdirThe change was done on purpose, see the comment above.
Doing this breaks things like previewing paragraphs. Looks like we have no tests here, but I'm sure paragraph tests would fail with this change.
If we need something like this, then I think we can simply do this:
if ($item->entity) {
$item->_loaded = TRUE;
}
We can only do single-load anyway. We should slightly update the comment above then, to say way access it to trigger lazy loading and making sure it exists.
The test fails for me as well, with phpunit and run-tests.sh:
1) Drupal\Tests\entity_reference_revisions\Kernel\EntityReferenceRevisionsFormatterTest::testFormatterWithDeletedReference
Invalid permission administer entity_test composite relationship.
The reason it's not failing for you is because OSX is stupid :)
tests/modules/entity_composite_relationship_test/Entity_composite_relationship_test.permissions.yml
Can you spot the error?
Comment #12
dawehnerIs this something you think looks more like you expect it to look like?
Comment #13
johnchqueI just tested the last patch with latest Paragraphs dev and it breaks ParagraphsPreviewTest, it seems that in preview some Paragraphs are not displayed even if they have content.
Comment #14
dawehnerWell my usecase is not paragraphs to be honest, so I'm wondering whether we should block this patch, just because they continue to not work ...
Comment #15
berdir@dawehner: No, you misunderstood. Paragraphs tests pass right now. This change breaks them.
It might not be your use case, but we need to find a way to fix this without breaking something that works right now.
Comment #16
dawehnerI tried the following, but it didn't resolve the problem yet.
Comment #17
johnchqueDiscussed with @Berdir, this change shouldn't break Paragraphs either. Just to know, in Paragraphs we were testing that preview is shown when Paragraphs are not saved yet, the change was made to avoid the check for new entities.
Comment #19
johnchqueUnrelated test fail.
Comment #20
berdirYay.
Comment #21
johnchqueTest fail fix: #2770705: Fix test fails related with diff.
Comment #23
dawehnerCool!
Comment #24
miro_dietikerFixed the related head fail first, retested, committed. Thx all! :-)