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"
Comments
Comment #2
daniel.pernold commentedI added a check to do not calculate cos/sin if lat/lng is not valid.
3257751-2.patch
Comment #3
andregp commentedChanging 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).
Comment #4
daniel.pernold commentedUnfortunately, my patch breaks entering of sexagesimal format. This patch fixes this issue.
Comment #5
daniel.pernold commentedPatch #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.
Comment #6
christianadamski commentedCannot 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?
Comment #7
christianadamski commentedComment #8
christianadamski commentedComment #9
svicer commentedReproduced 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
Comment #10
svicer commentedComment #11
christianadamski commented- 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.
Comment #12
svicer commentedI 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
Comment #13
dtfabio commentedHi 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):
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?
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.
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.
After which I am shown the following error on the page and the logs show the same error as on our production site.
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
Comment #14
jagraj_singh_gill commentedI 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.
Comment #15
jagraj_singh_gill commentedComment #17
jagraj_singh_gill commentedJust 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.
Comment #18
eduardo morales albertiCreated constraint validation on issue https://www.drupal.org/project/geolocation/issues/3236557
MR https://git.drupalcode.org/project/geolocation/-/merge_requests/34/diffs
Comment #19
christianadamski commentedThis is not reproducible on simplytest.me in v3, not v4.
Comment #20
hikkypo commentedI 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.
Comment #22
christianadamski commentedOK, AI convinced me, that this is a real issue and I'm mistaken. I merged #14. Thanks everyone!