Problem/Motivation

function foo_entity_delete(EntityInterface $entity) {
  $context = EntityContext::fromEntity($entity);
}

This will fail.

Introduced in #2791269: Allow saving pre-existing references to inaccessible items

Proposed resolution

If \Drupal\Core\Entity\Plugin\Validation\Constraint\ValidReferenceConstraintValidator::validate() can't load the unchanged entity from the DB (because it no longer exists), use the one we have

This ticket is now a clean up of a todo and update InlineBlockEntityOperations::handleEntityDelete() to use isLayoutCompatibleEntity()

Remaining tasks

N/A

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Issue fork drupal-3008943

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tim.plunkett created an issue. See original summary.

tim.plunkett’s picture

The last submitted patch, 2: 3008943-context_delete-2-FAIL.patch, failed testing. View results

amateescu’s picture

I wonder what's the use case for validating an entity after it has been deleted from the storage. The issue title says that the entity is being deleted, but that's not really accurate, the entity is already gone.

tim.plunkett’s picture

It's not actually the validating I'm hitting, it's EntityContext::fromEntity($entity), which is easily triggered that way.

When a custom block is deleted, Layout Builder has to find all of the usages of that block in order to remove the reference to it. See #2957425: Allow the inline creation of non-reusable Custom Blocks in the layout builder, and layout_builder_entity_delete() / \Drupal\layout_builder\InlineBlockEntityOperations::handleEntityDelete().

Because TypedData, everything all the way down the chain, even the now-deleted block, is instantiated.

phenaproxima’s picture

I think the test could benefit from a comment explaining why we're calling validate(), but otherwise this looks good and is ready to go.

tim.plunkett’s picture

Nah, let's just be explicit about what we're testing. No need to mess with ::validate()

tim.plunkett’s picture

Here's a pass/fail version, to prove we're actually still testing it without the validate() part

phenaproxima’s picture

Status: Needs review » Reviewed & tested by the community

I like it. RTBC on the assumption that it will look like Christmas in an hour.

The last submitted patch, 8: 3008943-context_delete-8-FAIL.patch, failed testing. View results

amateescu’s picture

I'd like to take another look at the patch as well, hopefully there's no rush to commit it..

tim.plunkett’s picture

Assigned: Unassigned » amateescu
Status: Reviewed & tested by the community » Needs review

Let's do this, as I definitely want your input.

amateescu’s picture

Assigned: amateescu » Unassigned

I've been staring for a while at \Drupal\layout_builder\InlineBlockEntityOperations::handleEntityDelete() and all its possible code paths, and I can't find what would trigger a call to EntityContext::fromEntity($entity) in there..

Is there some other part of the code that I should also be looking at? :)

amateescu’s picture

Assigned: Unassigned » amateescu

Oops, didn't want to un-assign, I'm still looking into it.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

alexpott’s picture

+++ b/core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraintValidator.php
@@ -124,7 +124,7 @@ public function validate($value, Constraint $constraint) {
-        $existing_entity = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id());
+        $existing_entity = $this->entityTypeManager->getStorage($entity->getEntityTypeId())->loadUnchanged($entity->id()) ?: $entity;

This looks very reasonable - but perhaps a code comment might be a good idea - so people know when we might use the passed in entity instead of the unchanged one.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

jhedstrom’s picture

Status: Needs review » Needs work

Setting to NW for the code comment mentioned in #16.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Tagging to get some eyes to add the comment

yogeshmpawar’s picture

Updated patch with reroll diff & addressed #16

yogeshmpawar’s picture

Status: Needs work » Needs review

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Rishabh Vishwakarma’s picture

Status: Needs work » Needs review
FileSize
3.05 KB

Adding patch against 10.0.1

ameymudras’s picture

Status: Needs review » Needs work

Did a brief code review and the following comment doesn't make sense to me, I think we should reword it. Otherwise, the code looks good
// We might use the passed entity instead of the unchanged one.

sahil.goyal’s picture

Made change to the comment as per #30 so the comment is now quite relevant what it supposed to do.

sahil.goyal’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Attempted to replicate this by running the code in the issue summary and I don't get any error in 10.1

Can someone please confirm if it's still an issue and update the issue summary with additional steps

Thanks!

tim.plunkett’s picture

Assigned: amateescu » Unassigned
Status: Needs work » Closed (outdated)

I think the codepath described by the test case (introduced in #7) was at one time relevant to how Layout Builder was working, but is not reproducible anymore in core. And I can't think of a reason why you'd need to do it in custom code either.

amateescu’s picture

Version: 10.1.x-dev » 11.x-dev
Status: Closed (outdated) » Active

There's still a reference in code to this issue in \Drupal\layout_builder\InlineBlockEntityOperations::handleEntityDelete(), let's see if calling isLayoutCompatibleEntity() is possible now.

amateescu’s picture

Status: Active » Needs review

Opened a MR, let's see how it goes.

needs-review-queue-bot’s picture

Status: Needs review » Needs work

The Needs Review Queue Bot tested this issue.

While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

smustgrave’s picture

Title: Creating an context object for an entity that is being deleted causes a fatal error » Clean up todo in InlineBlockEntityOperations::handleEntityDelete() and use isLayoutCompatibleEntity()
Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Updated title and solution to mention this seems to be cleaning up a todo and use isLayoutCompatibleEntity().

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

Added a question the MR.

smustgrave’s picture

Status: Needs review » Needs work

Moving to NW for someone to test that proposal.