Problem/Motivation

I have a use case where a view is using the GeofieldProximityArgument contextual filter to filter for nodes within a given radius, the view is rendered programmatically and due to other contextual filters, the view is rendered multiple times in a single request with different arguments.

After debugging why the view was sometimes returning incorrect results, it looks like the use of "static" in the filter was causing the distances to not be recalculated when the origin point or radius changed.

Steps to reproduce

1. Create a new content type "Event" with a lat / long geofield for location and create some dummy content.
2. Create a new view "Events" showing event content, with a Lat / Long Proximity contextual filter on the geofield and sort by proximity.
3. Render the view programmatically multiple times in the same request with different values for the contextual filter.

    $view = \Drupal\views\Views::getView('events');
    $view->setDisplay('block_1');
    $view->setArguments(['-29.463268,149.842062<=200km']);
    $view->preExecute();
    $view->execute();

4. Either by stepping through GeofieldProximityArgument::getParsedReferenceLocation() with a debugger or by comparing results, confirm that the reference location (origin) is only ever set/calculated on the first run because of the static $values.

Proposed resolution

Remove the use of static in this case. The calculations are likely quick enough that this is a small enough micro-optimisation to be ignored for most use cases.

Comments

Eric115 created an issue. See original summary.

eric115’s picture

Patch to refactor getParsedReferenceLocation() to not use static value.

  • itamair committed 70938ae on 8.x-1.x authored by Eric115
    Issue #3264184 by Eric115: Static value in GeofieldProximityArgument...
itamair’s picture

Status: Active » Fixed

Thanks a lot @Eric115. Committed into dev, will be part of the next Geofield module release.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.