I have a custom block with a field collection structure like the following:

  • Field collection (unlimited)
    • Text field
    • Field collection (unlimited)
      • Text field
      • Text field

I can create a block instance, but when I try to delete it I get the following error:

Drupal\Core\Entity\EntityStorageException: Unable to save a field collection item without a valid reference to a host entity. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->delete() (line 705 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Drupal\field_collection\Entity\FieldCollectionItem->deleteHostEntityReference()
Drupal\field_collection\Entity\FieldCollectionItem->delete()
Drupal\field_collection\Plugin\Field\FieldType\FieldCollection->delete()
Drupal\Core\Field\FieldItemList->delegateMethod('delete')
Drupal\Core\Field\FieldItemList->delete()
Drupal\Core\Entity\ContentEntityStorageBase->invokeFieldMethod('delete', Object)
Drupal\Core\Entity\ContentEntityStorageBase->doDelete(Array)
Drupal\Core\Entity\EntityStorageBase->delete(Array)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->delete(Array)
Drupal\Core\Entity\Entity->delete()
Drupal\field_collection\Entity\FieldCollectionItem->delete()
Drupal\field_collection\Plugin\Field\FieldType\FieldCollection->delete()
Drupal\Core\Field\FieldItemList->delegateMethod('delete')
Drupal\Core\Field\FieldItemList->delete()
Drupal\Core\Entity\ContentEntityStorageBase->invokeFieldMethod('delete', Object)
Drupal\Core\Entity\ContentEntityStorageBase->doDelete(Array)
Drupal\Core\Entity\EntityStorageBase->delete(Array)
Drupal\Core\Entity\Sql\SqlContentEntityStorage->delete(Array)
Drupal\Core\Entity\Entity->delete()
Drupal\block_content\Entity\BlockContent->delete()
Drupal\Core\Entity\ContentEntityDeleteForm->submitForm(Array, Object)
call_user_func_array(Array, Array)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object)
Drupal\Core\Form\FormBuilder->processForm('block_content_nir_faq_delete_form', Array, Object)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1)
Stack\StackedHttpKernel->handle(Object, 1, 1)
Drupal\Core\DrupalKernel->handle(Object)

I'm guessing that what's happening is that the top level field collection entities are getting deleted before the field collection entities that belong to it, with the result that those nested entities have a broken entity reference.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TravisCarden created an issue. See original summary.

  • tim.plunkett committed e984358 on 8.x-1.x
    Issue #2614434 by tim.plunkett: Deleting an entity with nested field...
tim.plunkett’s picture

Status: Active » Fixed

This was handled in D7, copied the code and it worked.

arknoll’s picture

Status: Fixed » Needs work

This does not appear to be fixed. I can reproduce the same error mentioned above with the latest dev codebase.

juampynr’s picture

I can reproduce the error. Debugging...

juampynr’s picture

Status: Needs work » Needs review
FileSize
2.72 KB
39.53 KB

Here is a patch that fixes the issue. It contains the following fixes:

* Sets the deleting flag to FieldCollectionItem instead of FieldCollection. The need of this change is that when a nested field collection executes getHost(), it receives the FieldCollectionItem.
* The host entity was being reset just before checking for the flag, so the flag was lost.

For testing purposes, here is the scenario that I used to trigger the reported error:

balsama’s picture

Status: Needs review » Reviewed & tested by the community

Confirmed. Using nir_faq as configuration (https://www.drupal.org/sandbox/balsama/2630548) deleting custom blocks of type FAQ fails with EntityStorageException. After applying this patch, I'm able to delete FAQ blocks. (FAQ blocks as defined in nir_faq have nested field collections)

Marking as RTBC. Thanks!

tim.plunkett’s picture

Status: Reviewed & tested by the community » Fixed

Committed, thanks!

  • tim.plunkett committed 83e9cf5 on 8.x-1.x authored by juampynr
    Issue #2614434 by juampynr, tim.plunkett: Deleting an entity with nested...

Status: Fixed » Closed (fixed)

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