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

dawehner created an issue. See original summary.

dawehner’s picture

Status: Active » Needs review
StatusFileSize
new975 bytes

Maybe something like this would work

dawehner’s picture

StatusFileSize
new4.75 KB
new5.73 KB

This time also with a test.

The last submitted patch, 3: 2742115-3-fail.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 3: 2742115-3.patch, failed testing.

The last submitted patch, 3: 2742115-3.patch, failed testing.

The last submitted patch, 3: 2742115-3-fail.patch, failed testing.

miro_dietiker’s picture

Yeah 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.

dawehner’s picture

StatusFileSize
new119.96 KB

I'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.

eric heydrich’s picture

Maybe 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)

berdir’s picture

+++ b/src/Plugin/Field/FieldFormatter/EntityReferenceRevisionsFormatterBase.php
@@ -19,7 +19,15 @@ abstract class EntityReferenceRevisionsFormatterBase extends EntityReferenceForm
     foreach ($entities_items as $items) {
       foreach ($items as $item) {
-        $item->_loaded = TRUE;
+
+        if ($this->needsEntityLoad($item)) {
+          $target_type = $this->getFieldSetting('target_type');
+          $entity = \Drupal::entityTypeManager()->getStorage($target_type)->loadRevision($item->target_revision_id);
+          if ($entity) {
+            $item->entity = $entity;
+            $item->_loaded = TRUE;
+          }

The 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.

+++ b/src/Plugin/Field/FieldFormatter/EntityReferenceRevisionsFormatterBase.php
--- /dev/null
+++ b/tests/modules/entity_composite_relationship_test/Entity_composite_relationship_test.permissions.yml

+++ b/tests/src/Kernel/EntityReferenceRevisionsFormatterTest.php
@@ -0,0 +1,106 @@
+
+    $user = $this->createUser(['administer entity_test composite relationship']);
+    \Drupal::currentUser()->setAccount($user);

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?

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new5.99 KB
new1.99 KB

Is this something you think looks more like you expect it to look like?

johnchque’s picture

Status: Needs review » Needs work

I 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.

dawehner’s picture

Well 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 ...

berdir’s picture

@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.

dawehner’s picture

StatusFileSize
new750 bytes

I tried the following, but it didn't resolve the problem yet.

johnchque’s picture

Status: Needs work » Needs review
StatusFileSize
new5.96 KB
new679 bytes

Discussed 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.

Status: Needs review » Needs work

The last submitted patch, 17: 2742115-17.patch, failed testing.

johnchque’s picture

Status: Needs work » Needs review

Unrelated test fail.

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Yay.

johnchque’s picture

dawehner’s picture

Status: Reviewed & tested by the community » Fixed

Cool!

miro_dietiker’s picture

Fixed the related head fail first, retested, committed. Thx all! :-)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.