Problem/Motivation
OrganizationOwnedInterface / OrganizationOwnedTrait ship in 4.0.0 (#3639236: Add OrganizationInterface and an optional organization on the Context), but nothing adds the organization_id field they read: getOrganization() returns NULL and setOrganization() throws. Parent: #3638816: [META] Commerce 4.x roadmap.
Proposed resolution
OrganizationEntityTypeRepository (commerce)
Modeled on PurchasableEntityTypeRepository. getEntityTypeId() returns the site's organization entity type:
- If the
organization_idfield is installed: its installedtarget_type. From then on this is the source of truth, so installing another module never retargets the field. - Otherwise, discovery of the entity types implementing
OrganizationInterface: none → NULL (no field); one → that one; several → NULL, no field, and ahook_requirements()error listing them. There is no tie-breaker: the site uninstalls all but one.
It reads the installed definitions (entity.definition_update_manager), not entity_field.manager, so hook_entity_base_field_info() can call it without recursing. hook_requirements() also reports when discovery would pick a different type than the installed field. No event to choose between candidates yet (additive later).
Public API: OrganizationEntityTypeRepositoryInterface is how contrib finds the organization entity type, for example a price list's organizations base field targeting getEntityTypeId(). Experimental, but not @internal.
The field
- commerce's
hook_entity_base_field_info()definesorganization_id, targeting that type, on every entity type whose class implementsOrganizationOwnedInterface, only when the repository returns one. Orders first; payment methods, invoices and subscriptions follow when their modules support organizations. OrganizationOwnedTrait::setOrganization()throws if the organization is not of the target type.- The target is fixed once installed. Changing it while entities reference organizations is not supported (no update path); the status report flags a mismatch.
Lifecycle
Core installs field storage on module install only for fields provided by the module being installed. OrganizationFieldInstaller covers the gaps, for organization_id and for any contrib entity reference base field targeting the organization entity type (a price list's organizations): it installs such fields where they are defined but not installed.
- The target appears later (an organization module or the Group bridge installed after orders or price lists exist):
commerce_modules_installed()runs the routine. - A module's class starts implementing
OrganizationOwnedInterface, or a module adds such a field: that module's update hook runs the installer. - The target module is uninstalled: core's content validator blocks it while organizations exist; otherwise
commerce_modules_uninstalled()uninstalls the entity reference base fields left without a definition whose target entity type is gone, and the repository ignores an installed target whose entity type is gone.
Collisions: the hook and the routine skip an entity type where an organization_id from another provider is installed, and hook_requirements() reports it. The site renames its field (an update hook copying the values) and runs the routine again.
Order integration
- Views relationship from the order to the organization entity type.
- The organization shown read-only on the admin order page (commerce_b2b makes it editable).
The commerce_organization entity type moves to commerce_b2b as a submodule (see #3638816: [META] Commerce 4.x roadmap), so core tests use commerce_organization_test. Blocking an organization module's uninstall while orders reference it is that module's job.
Experimental
The organization plumbing is experimental whichever module provides the organization entity type (commerce_b2b's commerce_organization, a Group bridge, a custom entity): the repository and the field installer, cart scoping and organization access may change in a 4.x minor until commerce_b2b exercises them. Documented in the docblocks and the release notes.
Remaining tasks
- Kernel tests: no target means no field; installing the organization entity type later installs it; several candidates; the installed target wins; a contrib reference field is installed and uninstalled; an existing organization_id from another provider is skipped and reported. Done, in
OrganizationFieldTest.
Data model changes
New organization_id column on commerce_order (and later other organization-owned entity types), only when exactly one organization entity type exists. The column follows the target's ID type.
Issue fork commerce-3641080
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
jsacksick commentedComment #5
jsacksick commentedMerged!