![Announcement icon Announcement icon](https://cdn.statically.io/img/www.drupal.org/files/styles/grid-2-2x-square/public/announcements/drupal-evergreen-logo-280X280px%20%281%29_0.jpg?itok=PHpn6rCb)
Problem/Motivation
Suppose you have some arbitrary $entity_type_id and you want to know if there are any entities stored. Sadly:
\Drupal::entityQuery($entity_type_id)->count()->execute()
isn't guaranteed to work, because ContentEntityNullStorage::getQueryServiceName()
throws an exception. Another alternative is to use the countFieldData() method and count the values for the field holding the entity ID:
$entity_manager->getStorage($entity_type_id)->countFieldData($entity_manager->getFieldStorageDefinitions($entity_type_id)[$entity_type->getKey('id')], TRUE);
but that's pretty ugly and only works for fieldable entities.
This is a major bug because:
During a configuration import, the Entity system performs validation to ensure that bundles are not deleted if they are in use on the target site. The current code for this validation assumes that the storage for all validated bundles will return a usable query via the getQuery() method. However, this is not the case for bundles using ContentEntityNullStorage (which appears to be used by contact forms). Such bundles will cause a QueryException to be thrown, breaking the import process.
Proposed resolution
Option 1: Require storage handlers to always implement a working entity query service. Even for NULL storage, which can always return empty/0 as appropriate.
Option 2: Add a hasData()
method or similar to EntityStorageInterface
.
Remaining tasks
- Improve tests
- Review
- Commit
User interface changes
None.
API changes
None.
Suggested commit message
git commit -m 'Issue #2337753 by alexpott, ndewhurst: ContentEntityNullStorage does not implement a query service'
@ndewhurst deserves credit for their work on #2409013: BundleConfigImportValidate Attempts to Query Un-Queryable Storages
Comment | File | Size | Author |
---|---|---|---|
#16 | 2337753.16.patch | 8.23 KB | alexpott |
#16 | 14-16-interdiff.txt | 604 bytes | alexpott |
#14 | 2337753.14.patch | 8.21 KB | alexpott |
#14 | 9-14-interdiff.txt | 6.78 KB | alexpott |
#9 | 2337753.9.test-only.patch | 2.63 KB | alexpott |
![Tag1 logo](https://cdn.statically.io/img/www.drupal.org/files/ads/tag1-issue-sponsorship.png)
Comments
Comment #1
effulgentsia CreditAttribution: effulgentsia commentedI'd prefer option 1, because it seems odd to me for EntityQuery to not be a reliable service to use.
Comment #2
effulgentsia CreditAttribution: effulgentsia commentedComment #3
effulgentsia CreditAttribution: effulgentsia commentedComment #4
effulgentsia CreditAttribution: effulgentsia commentedComment #5
chx CreditAttribution: chx commentedObviously 1.
Comment #6
chx CreditAttribution: chx commentedComment #7
chx CreditAttribution: chx commentedComment #8
BerdirDuplicate of #2409013: BundleConfigImportValidate Attempts to Query Un-Queryable Storages?
Comment #9
alexpottHere is an implementation of a null query service for null storage and a test for #2409013: BundleConfigImportValidate Attempts to Query Un-Queryable Storages which I think we should close in favour of this issue.
Bumping to major because of how this breaks configuration import validation.
Comment #10
alexpottComment #12
alexpottComment #13
chx CreditAttribution: chx as a volunteer commentedreturn FALSE should be return 0 shouldn't it? Otherwise looks great.
Comment #14
alexpottFixing #13 and improving the test coverage.
Comment #15
chx CreditAttribution: chx as a volunteer commented> Tests importing a delete of contact message.
My English parser gave up on this.
Comment #16
alexpottFixing my english.
Comment #17
chx CreditAttribution: chx as a volunteer commentedIn a minor followup we should add a create method to ConfigImporter to avoid writing all those gets like five times in core already.
Comment #18
catchCommitted/pushed to 8.0.x, thanks!