Problem/Motivation

Lat/Lng Widget fails to save when no float given

Steps to reproduce

* Create a field within an entity type
* Use "Geolocation Lat/Lng" widget in form display
* Create a content with that field
* Enter something else than a valid float in lat or lng
* Save
* The resulting error:

The website encountered an unexpected error. Please try again later.
TypeError: deg2rad(): Argument #1 ($num) must be of type float, string given in deg2rad() (line 165 of modules/contrib/geolocation/src/Plugin/Field/FieldType/GeolocationItem.php).
deg2rad('48.225770009296a') (Line: 165)
Drupal\geolocation\Plugin\Field\FieldType\GeolocationItem->setValue(Array, ) (Line: 72)
Drupal\Core\TypedData\Plugin\DataType\ItemList->setValue(Array, 1) (Line: 107)
Drupal\Core\Field\FieldItemList->setValue(Array) (Line: 403)
Drupal\Core\Field\WidgetBase->extractFormValues(Object, Array, Object) (Line: 235)
Drupal\Core\Entity\Entity\EntityFormDisplay->extractFormValues(Object, Array, Object) (Line: 334)
Drupal\Core\Entity\ContentEntityForm->copyFormValuesToEntity(Object, Array, Object) (Line: 293)
Drupal\Core\Entity\EntityForm->buildEntity(Array, Object) (Line: 155)
Drupal\Core\Entity\ContentEntityForm->buildEntity(Array, Object) (Line: 186)
Drupal\Core\Entity\ContentEntityForm->validateForm(Array, Object)
call_user_func_array(Array, Array) (Line: 82)
Drupal\Core\Form\FormValidator->executeValidateHandlers(Array, Object) (Line: 275)
Drupal\Core\Form\FormValidator->doValidateForm(Array, Object, 'node_point_of_interest_edit_form') (Line: 118)
Drupal\Core\Form\FormValidator->validateForm('node_point_of_interest_edit_form', Array, Object) (Line: 588)
Drupal\Core\Form\FormBuilder->processForm('node_point_of_interest_edit_form', Array, Object) (Line: 320)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object) (Line: 39)
Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 158)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 49)
Asm89\Stack\Cors->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Proposed resolution

Implement a check before calculating "lat_sin" etc. in "GeolocationItem"

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

daniel.pernold created an issue. See original summary.

daniel.pernold’s picture

StatusFileSize
new1.33 KB

I added a check to do not calculate cos/sin if lat/lng is not valid.

3257751-2.patch

andregp’s picture

Version: 8.x-3.7 » 8.x-3.x-dev
Status: Active » Needs review
StatusFileSize
new347.03 KB
new150.15 KB
new1.44 KB
new840 bytes

Changing version as bug issues should be directed to the dev version.

The issue is also found on the dev version. The patch #2 applies correctly and solves the issue.

The only fix is that tabs were used on the indentation which are against Drupal Coding standards so I'm sending the same patch with spaces instead (and some other small CS fixes).

daniel.pernold’s picture

StatusFileSize
new1.77 KB

Unfortunately, my patch breaks entering of sexagesimal format. This patch fixes this issue.

daniel.pernold’s picture

StatusFileSize
new3.07 KB

Patch #4 breaks the feature. Had to dive deeper. Patch #5 treats lat and lng as separate values and fixes a bug not setting anything if sexagesimal and decimal is mixed.

christianadamski’s picture

Status: Needs review » Reviewed & tested by the community

Cannot confirm. Simply installing a new website and the demo module, entering "hello" results ins a regular error

"This value should be of the correct primitive type."

thrown by core Drupal "PrimitiveTypeConstraint".

What is differing in your install?

christianadamski’s picture

Status: Reviewed & tested by the community » Postponed (maintainer needs more info)
christianadamski’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)
svicer’s picture

Reproduced on clear drupal 10 standard instalation.
Enviroment: php8.1 and php8.2
Drupal 9.5.9 and 10.0.9
Drush 11.5.1
drupal/geolocation 3.12.0

Steps to reproduce:
- Install standard Drupal profile
- Enable geolocation module
- Add geolocation field to Article
- Try to create new Article and fill Latitude and Longitude with random string
- Click to Save button
- You will see 500 error on page and in logs

The website encountered an unexpected error. Please try again later.
TypeError: deg2rad(): Argument #1 ($num) must be of type float, string given in deg2rad() 
svicer’s picture

Status: Closed (works as designed) » Needs work
christianadamski’s picture

Status: Needs work » Closed (cannot reproduce)
StatusFileSize
new109.64 KB

- went to simplytest.me
- chose Geolocation 8.x-3.x-dev
- waited for ready
- added geolocation field to article
- went to create new article
- added random letters to lat / lng
- got a correct warning (see screenshot)

Unless you can provide a way to reproduce your issue, I really can't do anything.

svicer’s picture

StatusFileSize
new262.2 KB

I think this problem cannot be reproduced on simplytest.me because this test tool uses PHP 7.4. But this problem is reproduced on PHP 8.1 and PHP 8.2

dtfabio’s picture

Hi ChristianAdamski,

I also had the same problem on one of our sites using this module.

Drupal version: 10.1.5
PHP version: 8.1.11
Module version: 3.12.0

I got the error after entering a float with a comma as a separator instead of a full stop.

These values (Latitude : 29,9773, Longitude : 31,1325)

Gave the following error (see image):

Error on the production site after adding the field.

Only there was a peculiarity, while creating my content the error did happen, but while editing I got the message (NaN) on the field after entering the values. (So after entering it correctly once, it doesn't seem to occur again.)

So validation did happen, but just not during initeal adding of the content?

The patch fixed the problem for me and the field still works. The only thing I am still somewhat unsure about is the message displayed.

In Belgium, this is a valid number (as we work with commas). So perhaps a conversion to full stop in the code or a mention under the field that a full stop is required would be useful, I think?

After using the patch message is shown that longitude is not nummeric.

Warning on the fields that the value was not a number after adding patch.

Field showing the map after adding the patch to show that it still works.

Just to be sure, I also tried your simplytest.me way, only with the difference that I used the latest release instead of the dev branch.

This is the test site status page.

Simply test instance status page with geolocation version 3.12 installed.

Perhaps using the release version and not the dev branch caused the PHP version to be chosen correctly. Because in my instance, the site is PHP 8.1.

I fill in the fields with the test values listed above.

Filled in vield with test values that return the error.

After which I am shown the following error on the page and the logs show the same error as on our production site.

Error after saving shown on the test instance screen.

Error after saving in the logs of the test instance.

I hope this is enough information to take a closer look at this ticket, so that this solution (possibly with a few more tweaks) can be included in to the module.

Greetings,

Fabio

jagraj_singh_gill’s picture

Version: 8.x-3.x-dev » 8.x-3.12
StatusFileSize
new867 bytes

I was facing similar issue with version 8.x-3.12 when I was adding any string in the Lat/Long fields as soon as focus was going out of field input, it was changing to NaN. And on submission it was giving same issue.
In code, parent set value was being called before the custom code execution. I have attached my patch below. For me after changing the order of execution it started #Working.

jagraj_singh_gill’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 14: geolocation-3257751-14.patch, failed testing. View results

jagraj_singh_gill’s picture

Just to provide more context on above issue, I am only able to reproduce this issue when I use Geolocation Google Maps API - Geocoding and Map in Form display of this Geolocation field.

christianadamski’s picture

This is not reproducible on simplytest.me in v3, not v4.

hikkypo’s picture

I have to disagree with #19, when adding the module geolocation to a simplytest.me on version 3.14. Then adding a new geolocation field to the basic page content type. When i then go and make a new node of this type and fill test in both fields and save the page i get the exact mentioned error.

I also tested patch https://www.drupal.org/files/issues/2024-01-02/geolocation-3257751-14.patch and it produced an error on save thereby fixing the issue.

christianadamski’s picture

Status: Needs work » Fixed

OK, AI convinced me, that this is a real issue and I'm mistaken. I merged #14. Thanks everyone!

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.

Status: Fixed » Closed (fixed)

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