Problem/Motivation

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

This will fail.

There is a @todo comment in InlineBlockEntityOperations::handleEntityDelete():

  public function handleEntityDelete(EntityInterface $entity) {
    // @todo In https://www.drupal.org/node/3008943 call
    //   \Drupal\layout_builder\LayoutEntityHelperTrait::isLayoutCompatibleEntity().
    $this->usage->removeByLayoutEntity($entity);
  }

This comment was added in #3026434: Ensure that Layout Builder Inline Blocks doesn't assume section storage internals to deal with how a call isLayoutCompatibleEntity() on the entity being deleted eventually causes an error in \Drupal\Core\Entity\Plugin\Validation\Constraint\ValidReferenceConstraintValidator::validate() (loading entities in the validator 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
Since $this->usage->removeByLayoutEntity($entity) just performs a simple update query, loading entities to check whether they can be deleted likely does not offer any benefit or performance enhancement, so the isLayoutCompatibleEntity is unnecessary.

This ticket is now a clean up to remove the todo comment in InlineBlockEntityOperations::handleEntityDelete()

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:

Comments

tim.plunkett created an issue. See original summary.

tim.plunkett’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new1.84 KB
new2.97 KB

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
StatusFileSize
new144 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
StatusFileSize
new3.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

StatusFileSize
new3.07 KB
new1.06 KB

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

amateescu’s picture

Status: Needs work » Needs review

Hiding the patches because the MR seems to work well :)

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.

brandonlira made their first commit to this issue’s fork.

brandonlira’s picture

Status: Needs work » Needs review

Hi all,
I removed the isLayoutCompatibleEntity($entity) check before calling removeByLayoutEntity($entity), as suggested.

Tests performed:

PHPUnit: All tests passed successfully, with no errors found. Only pre-existing skipped tests remained unchanged.

The change has been committed and submitted in the Merge Request. Awaiting feedback for any necessary adjustments!

Thank you!

smustgrave’s picture

Status: Needs review » Needs work

MR is 1800+ commits back.

brandonlira’s picture

Hi @smustgrave

I have successfully rebased the branch with the latest changes from 11.x. The MR !7106 is now up to date, and the previous changes remain intact.

Additionally, I noticed that one test failed, but it does not appear to be related to the changes made in this MR.

Please let me know if you need any additional adjustments.

Thanks!

brandonlira’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

So reading the issue summary and title

update InlineBlockEntityOperations::handleEntityDelete() to use isLayoutCompatibleEntity()

Still seems to be needed.

brandonlira’s picture

Hi @smustgrave,

Apologies if I might be misunderstanding something here, but I just want to clarify the intent behind this change to ensure I'm following the best approach. I'm still getting started with contributing to Drupal, and I really appreciate your guidance.

The issue summary suggests that handleEntityDelete() should be updated to use isLayoutCompatibleEntity(), but after reviewing the implementation, I noticed that removeByLayoutEntity($entity) does not fail if the entity is not layout-compatible. Instead, it simply attempts to clean up inline_block_usage, setting layout_entity_id and layout_entity_type to NULL if applicable. If no matching records exist, nothing happens, which makes me wonder if the extra check is truly necessary.

I just want to make sure I'm not missing something here:

  • Was there a specific issue that required adding isLayoutCompatibleEntity()?
  • Is there any scenario where calling removeByLayoutEntity() directly could cause unintended side effects?

If the check is needed for a reason I’m not seeing, I’ll be happy to update it accordingly. Otherwise, removing it might help simplify the logic while still ensuring the cleanup happens.

Again, thanks for your patience and feedback—I really appreciate learning from this process!

smustgrave’s picture

No worries. So might help to look at the reference issue this was created from.

Not a layout builder expert to say what’s needed or not but the summary needs to match the MR. Helps committers quickly understand a ticket

If it’s determined nothing is needed then the summary could include your findings and just a simple solution of remove the todo

smustgrave’s picture

Status: Needs work » Needs review

Can put back into review for more eyes but before RTBC summary will have to match

godotislate’s picture

Title: Clean up todo in InlineBlockEntityOperations::handleEntityDelete() and use isLayoutCompatibleEntity() » Clean up todo in InlineBlockEntityOperations::handleEntityDelete()
Issue summary: View changes
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks. then believe this ticket may be ready.

xjm’s picture

Thanks @smustgrave and @brandonlira for digging into the issue. The issue summary was indeed out of date, as the scope of this issue changed when old code was removed. See #34 and #35.

Since a field subsystem maintainer created this MR, that gives me confidence that this @todo is invalid and should be removed.

Saving issue credits.

  • xjm committed 79b32d1a on 11.x
    Issue #3008943 by tim.plunkett, amateescu, brandonlira, sahil.goyal,...

  • xjm committed 5e6531d5 on 11.2.x
    Issue #3008943 by tim.plunkett, amateescu, brandonlira, sahil.goyal,...

  • xjm committed a3fce20e on 10.6.x
    Issue #3008943 by tim.plunkett, amateescu, brandonlira, sahil.goyal,...

  • xjm committed b58d1232 on 10.5.x
    Issue #3008943 by tim.plunkett, amateescu, brandonlira, sahil.goyal,...
xjm’s picture

Status: Reviewed & tested by the community » Fixed

Since it's a non-disruptive cleanup of bad, actually wrong inline docs, I backported it all the way to 10.5.x. (Branch parity for things like tests and docs is sort of a gray area for maintenance minors; it's not officially listed but for some things we can keep parity.)

Thanks everyone!

Status: Fixed » Closed (fixed)

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