Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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.
Comment | File | Size | Author |
---|---|---|---|
#6 | testing_scenario.png | 39.53 KB | juampynr |
#6 | field_collection-delete-nested-error-2614434-6.patch | 2.72 KB | juampynr |
|
Comments
Comment #3
tim.plunkettThis was handled in D7, copied the code and it worked.
Comment #4
arknoll CreditAttribution: arknoll commentedThis does not appear to be fixed. I can reproduce the same error mentioned above with the latest dev codebase.
Comment #5
juampynr CreditAttribution: juampynr at Lullabot commentedI can reproduce the error. Debugging...
Comment #6
juampynr CreditAttribution: juampynr at Lullabot commentedHere 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:
Comment #7
balsamaConfirmed. 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!
Comment #8
tim.plunkettCommitted, thanks!