Problem/Motivation
This module has a service called entity_reference_revisions.orphan_purgerand its function is to search for entities (and revisions) that are no longer referenced by another entity, also known as orphan entities. To execute this there is a form in /admin/config/system/delete-orphans.
This works well with parent fields created with "Field Storage Config" but not with fields created with BaseFieldDefinition on custom entities. Consequently, the entities that are related to these types of fields will be eliminated when the service is executed, despite the fact that they are not actually orphaned.
This is what I have to produce the issue:
Host entity class has this in base field definition:
$fields['dimensions'] = BaseFieldDefinition::create('entity_reference_revisions')
->setLabel(t('Dimensions'))
->setRevisionable(TRUE)
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setSetting('target_type', 'record_dimension')
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
Record dimension (child entity)
/**
* Defines the RecordDimension entity.
*
* @ContentEntityType(
* id = "record_dimension",
* ...
* handlers = {
* ...
* },
* ...
* entity_revision_parent_type_field = "parent_type",
* entity_revision_parent_id_field = "parent_id",
* entity_revision_parent_field_name_field = "parent_field_name",
* ...
* )
*/
class RecordDimension extends ChildRecordEntityBase {
...
The ChildRecordEntityBase class has the base field definition for "parent_type", "parent_id" and "parent_field_name". Needed to integrate with entity reference revisions features.
Proposed resolution
In the method EntityReferenceRevisionsOrphanPurger::isValidParent add verification of whether the parent field can be one of the BaseFieldDefinition family too.
Remaining tasks
- Write a patch with new tests
- Review
- Commit
User interface changes
None.
API changes
None.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | interdiff_2_4.txt | 1.76 KB | mnico |
| #4 | purger_service_base_field_definition_compatibility-3158156-4.patch | 17.4 KB | mnico |
| #2 | purger_service_base_field_definition_compatibility-3158156-2.patch | 17.75 KB | mnico |
Comments
Comment #2
mnico commentedI attach a patch that does the modification in the
isValidParentmethod and adds a test that extendsEntityReferenceRevisionsOrphanRemovalTestbut instead of using nodes it changes to a custom entity.Comment #3
berdirNice catch, I'm not sure why this used configurable field storage.
But can't we just use \Drupal\Core\Entity\EntityFieldManager::getFieldStorageDefinitions() instead of checking for base and configureabnle fields in the same way?
Comment #4
mnico commentedWow, you are right ;). I attach a new patch with that.
Comment #5
berdirNice work! Fix looks good, verified that the test is the same as the default orphan removal test, just with the different entity types/field names and so on.
Changing constructors can be a bit tricky, especially adding new arguments in the middle, but this isn't something that's used during updates or so, should be easy to handle with a manual cache clear if people forget to run update.php.
Should be {@inheritdoc}, lowercase and without . at the end.
Fixed that and some other minor coding standard issues on commit.