Generated with the help of an LLM.
Problem/Motivation
The module can compute a point from a geometry (Geofield: calculate centroid) and can create new entities from contained geometries, but there is no action that looks up an existing entity whose boundary contains a location and stores it in an entity reference field.
The common case is "when a record is saved, pick the district (or neighbourhood, zone, region) it lies in": a node has a location Geofield, a taxonomy vocabulary holds one boundary polygon per district, and the node has a term reference. Today that cannot be built in the ECA UI. The existing Create referencing entities actions create new entities instead of selecting an existing one.
Steps to reproduce
- A vocabulary
districtwith a Geofieldfield_boundary(PostGIS backend) holding polygons. - A node type with a Geofield
field_locationand a term referencefield_districtlimited todistrict. - In ECA, try to build a model on the node's presave event that fills
field_districtfromfield_location.
Expected: an action exists for this. Actual: none of the available actions can select an existing entity by a spatial relationship, so the reference has to be set by hand or by custom code.
Proposed resolution
A new action, Geofield: assign containing entity reference (eca_geofield_assign_containing_reference), configured with:
- the source Geofield on the entity (several values are combined with
ST_Collect); - the entity reference field on the entity that receives the result. Its target entity type and target bundles define where to search, so the action needs no separate entity type or bundle settings;
- the Geofield on the referenced entity type that holds the boundary (PostGIS backend, like the other actions);
- the spatial operator,
ST_Intersectsby default, from the same allow-list as the other actions; - whether to clear the reference when nothing matches (off by default: the existing value is kept).
It is backed by a new public method, PostgisService::findEntityIdByRelation(). When several entities match (a district inside a province), the one with the smallest area wins, then the lowest entity ID. The action does not save the entity; the triggering event (for example presave) does, as with Geofield: calculate centroid, so the two can be chained: centroid first, then this action reading the centroid.
Two limitations are documented in the README: the boundary field must use the PostGIS backend, and the SRID of both sides is ignored, so both must use the same coordinate system.
Alternatives considered: extending Create referencing entities with an "existing entity" mode (it is built around creating entities, so the options would not fit); a separate condition plugin (a condition cannot write the reference).
A patch with the action, the service method, the config schema, a README row and 13 kernel tests is attached; the full module test suite passes (46 tests). I use it on a site where saving a record fills in its district.
Remaining tasks
- Review of the action options and the default for "clear when nothing matches".
- Decide whether the SRID should be honoured instead of ignored.
- Add the action to the site builder and developer guides under
docs/(only the README is updated in the patch). - Turn the attached patch into a merge request.
User interface changes
One new action in the ECA modeller, Geofield: assign containing entity reference, with five fields: source Geofield, entity reference field, target Geofield, spatial operator, and a checkbox to clear the reference when nothing matches. No other UI changes.
API changes
- New action plugin
eca_geofield_assign_containing_referenceand its config schemaaction.configuration.eca_geofield_assign_containing_reference. - New public method
PostgisService::findEntityIdByRelation(string $entityTypeId, array $bundles, string $fieldName, array $values, string $spatialOperator = 'ST_Intersects'): ?int. - No existing signatures change, so there is no backwards compatibility break.
Data model changes
None. No schema, entity or field changes; existing ECA models are not affected.
Issue fork eca_geofield-3638392
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 #6
orkutmuratyilmaz