Lat/Lng Widget fails to save when no float given

Created on 10 January 2022, over 2 years ago
Updated 24 January 2024, 5 months ago

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"

🐛 Bug report
Status

Needs work

Version

3.12

Component

Geolocation Field / Backend

Created by

🇦🇹Austria daniel.pernold

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇦Ukraine SVICER Mirgorod

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

    Steps to reproduce:
    - Install standart 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() 
  • Status changed to Needs work about 1 year ago
  • 🇺🇦Ukraine SVICER Mirgorod
  • Status changed to Closed: cannot reproduce about 1 year ago
  • 🇩🇪Germany ChristianAdamski Berlin, Germany

    - 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.

  • 🇺🇦Ukraine SVICER Mirgorod

    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

  • Status changed to Needs work 8 months ago
  • 🇧🇪Belgium dtfabio Ninove

    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):

    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

  • 🇮🇳India jagraj_singh_gill

    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.
    In code, parent set value was being called before the custom code execution. After changing the order of execution it started working for me. I have attached my patch below.

  • Status changed to Needs review 6 months ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7 updated deps
    last update 6 months ago
    32 pass, 1 fail
  • 🇮🇳India jagraj_singh_gill

    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.

  • 🇪🇸Spain Eduardo Morales Alberti Spain, 🇪🇺

    Created constraint validation on issue https://www.drupal.org/project/geolocation/issues/3236557 📌 Add long / lat range & type validation helper Needs review

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7 updated deps
    last update 5 months ago
    32 pass, 1 fail
Production build 0.69.0 2024