Problem/Motivation

I am creating this issue as a place to start organizing thoughts/considerations around the future of our custom hook_farm_entity_bundle_field_info() and how that will need to be refactored if/when Drupal core adds more official support for code-defined bundle field definitions.

Note that we also started using BundleFieldDefinition fields for Consumer entity fields in #3207716: Use BundleFieldDefinition for additional Consumer fields.

Related core issue: #2346347: Finalize API for creating, overriding, and altering code-defined bundle fields

Relevant farmOS issues/threads:

Proposed resolution

TBD

Remaining tasks

TBD

User interface changes

(Hopefully none.)

API changes

(Hopefully none.)

Data model changes

(Hopefully none, other than the mechanism by which bundle fields are defined.)

Comments

m.stenta created an issue. See original summary.

m.stenta’s picture

As @paul121 noticed in #3192245: Storage is not created for fields created with hook_farm_entity_bundle_field_info until a new entity of that type is installed, the FarmFieldFactory class currently uses FieldDefinitionInterface for all type-hinting, which leads to IDEs complaining because the code references properties/methods that are present on BaseFieldDefinition classes, but not on the FieldDefinitionInterface interface.

Explanation (copied from that issue thread):

The reason I used FieldDefinitionInterface is because FarmFieldFactory actually has to produce two different output object classes: BaseFieldDefinition and BundleFieldDefinition. So by using FieldDefinitionInterface it covers both. I agree this isn't perfect, and the IDE type-hinting issue you mention is a side-effect of that. But it may be a necessary evil until (if) core adopts a unified field definition class for base and bundle fields.

  • m.stenta committed 5a10539 on 2.x
    Issue #3194206: Add some @todos for future reference.
    
m.stenta’s picture

I pushed a commit that adds some @todo comments linking back to this issue, for future reference.

m.stenta’s picture

the FarmFieldFactory class currently uses FieldDefinitionInterface for all type-hinting

This is no longer the case - see last commit of #3192245: Storage is not created for fields created with hook_farm_entity_bundle_field_info until a new entity of that type is installed. We changed to BaseFieldDefinition for parameter type hinting, which is correct because BundleFieldDefinition is a child class, so even though FarmFieldFactory can return either type, it can still use BaseFieldDefinition for all its type-hinting.

m.stenta’s picture

Title: Refactor hook_farm_entity_bundle_field_info() when Drupal core supports code-defined bundle fields » Refactor bundle fields when Drupal core supports code-defined bundle fields
Issue summary: View changes
Related issues: +#3207716: Use BundleFieldDefinition for additional Consumer fields

Generalizing this issue a bit... we made the Consumer fields in to BundleFieldDefinitions declared in hook_entity_base_field_info() (not hook_farm_entity_bundle_field_info()). These will also need to be refactored if/when core supports bundle fields.

m.stenta’s picture

m.stenta’s picture

Version: 2.x-dev » 3.x-dev
m.stenta’s picture

Version: 3.x-dev » 4.x-dev