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

  1. A vocabulary district with a Geofield field_boundary (PostGIS backend) holding polygons.
  2. A node type with a Geofield field_location and a term reference field_district limited to district.
  3. In ECA, try to build a model on the node's presave event that fills field_district from field_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_Intersects by 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_reference and its config schema action.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.

Command icon 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

orkutmuratyilmaz created an issue. See original summary.

  • orkutmuratyilmaz committed 5f154dad on 1.0.x
    feature: #3638392 Add value types and the allow-list wording to satisfy...

  • orkutmuratyilmaz committed fe250c31 on 1.0.x
    feature: #3638392 Add an action that assigns the entity whose geometry...
orkutmuratyilmaz’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.