Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP TypeError trim in Geofield proximity view #51

Open
olafgrabienski opened this issue Apr 8, 2024 · 5 comments
Open

PHP TypeError trim in Geofield proximity view #51

olafgrabienski opened this issue Apr 8, 2024 · 5 comments

Comments

@olafgrabienski
Copy link
Member

olafgrabienski commented Apr 8, 2024

When I set up a content view with Geofield data, I get the following error:

TypeError: trim(): Argument #1 ($string) must be of type string, array given in trim() (line 324 of .../modules/geofield/geofield.elements.inc).

Steps to reproduce

  • Install and enable the modules Address Field, Geofield, and Geocoder.
  • In the Geocoder settings, choose "Google API Key", and provide a Google Maps API Key.
    ( Note: It's not that easy to obtain a Google Maps API Key, so I hope someone running a site with similar setup can try to reproduce the issue. )
  • Add a "Location" content type with an Address Field and a Geofield (Geocode from Address field).
  • Build a view of Locations, add the fields Title, Address, and Geofield - proximity.
  • Add an exposed filter for the Geofield - proximity field.

Actual behavior: The view doesn't show anything. In DBlog, you see the error message from above.

Expected behavior: The view works, and you can filter the locations by proximity.

Additional information

  • Backdrop 1.27.1
  • PHP 8.2.7
@olafgrabienski
Copy link
Member Author

olafgrabienski commented Apr 8, 2024

Some more observations:

As far as I know, the error happens with PHP 8.2 but not with PHP 8.1.

Also, there is a workaround: Add a 'Geofield:lat (not empty)' filter to the view.

Finally, the issue seems to be fixed when I change line 234 in geofield.elements.inc from:

if (!empty($element['origin']['#value']) && trim($element['origin']['#value']) != '' && empty($element['distance']['#value']))

to:

if (!empty($element['origin']['#value']) && is_string($element['origin']['#value']) && trim($element['origin']['#value']) != '' && empty($element['distance']['#value']))

(I found a file with this patch in an older test site. Unfortunately, I don't recall from where I got it.)

@indigoxela
Copy link
Member

Some more digging: I never saw that error, but my setup is different: no Address field, not using Google API, no automatic geocoding from field. (Still PHP 8.2)

Hope this helps to narrow things down a bit.

@indigoxela
Copy link
Member

indigoxela commented Apr 9, 2024

@olafgrabienski you could do a little more debugging, I guess.

When the field value's empty, $element['origin']['#value'] doesn't seem to be empty (first condition), and it's an array... what is it then?

Can you put in a:

debug($element['origin']['#value']);

right before that breaking line and check what the value is? Either directly on page (when error display's on), or in dblog if not.

Not on a production site, of course.

@olafgrabienski
Copy link
Member Author

Thanks for your feedback, @indigoxela! Interestingly, I'm not able to reproduce the error at the moment, even though I have removed both the patch and the 'Geofield:lat (not empty)' views filter (and cleared caches several times). Hm ..., when I tested this some months ago, I also started with this very issue and ended up with a working site, and I wasn't sure what exactly fixed it. And now again, as the issue seems to be fixed somehow, I don't know how to reproduce it.

That said, I put however the debug code before the line in question, getting the following message in DBlog.

User notice: false in geofield_proximity_search_validate() (line 324 of .../modules/geofield/geofield.elements.inc).

Not sure if this is still relevant, though.

@indigoxela
Copy link
Member

indigoxela commented Apr 9, 2024

User notice: false...

In this case the first condition works, so you don't even get to the "trim()".

And now again, as the issue seems to be fixed somehow...

A Heisenbug - they're the worst. 🙊

Your initial report indicates, that this value can be a non-empty array under circumstances... But without steps to reproduce, I guess we're stuck ATM. 🤷 Anyway, the issue exists now and maybe someone comes up with a clue. Maybe some "hickup" when geocoding the address? Not sure. I usually avoid automatic geocoding of address fields - had all sorts of trouble with that in D7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants