When using the gmap3_tools api function gmap3_tools_add_map to add a map into a form, the map won't be displayed if there are validation errors. That's because the JS is added using drupal_add_js only.

In the salsa_entity (salsa_event) module, we are loading a map within a form but as soon as validation fails, the map's gone. Please add support to add the JS using '#attached' property on the form.

Comments

s_leu’s picture

Status: Active » Needs review
StatusFileSize
new1.95 KB

Here's a patch that makes enables calling code to pass a form and attaches the js to it, if a form is passed.

s_leu’s picture

berdir’s picture

That's a somewhat "creative" solution. Wondering if it would make more sense to add a new function that returns a render array and @deprecate this one. Easier to port to D8, where drupal_add_js() will go away.

s_leu’s picture

StatusFileSize
new3.41 KB

Adding a patch as which adds a separate function for attaching the js to forms, also did some refactoring to prevent redundant code.

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/gmap3_tools.inc
    @@ -54,7 +54,71 @@ define('GMAP3_TOOLS_DEFAULT_MARKERS_POSITION_CENTER_ZOOM', 'center zoom');
    +function gmap3_tools_attach_js($options, &$form) {
    +  if (!is_array($form)) {
    +    return;
    +  }
    

    type hint the function argument with array &$form, then the check here isn't necessary.

  2. +++ b/gmap3_tools.inc
    @@ -54,7 +54,71 @@ define('GMAP3_TOOLS_DEFAULT_MARKERS_POSITION_CENTER_ZOOM', 'center zoom');
    +  static $init = FALSE;
    +  if (!$init) {
    +    $init = TRUE;
    

    Drupal is intelligent enough to avoid duplicates. Even easier if it would be a library, but that's another topic.

    In fact, when you think about caching, you want to ensure that each partial render array has all the information it needs to be displayed.

    A cached form won't run this function again, if there would be two calls/forms, the second one would get an incomplete render array and might not render correctly.

s_leu’s picture

Status: Needs work » Needs review
StatusFileSize
new3.28 KB

Adding the suggested changes

pivica’s picture

Status: Needs review » Fixed

Checked patch from #6, looking good, committed to 7.x-1.x branch.

@s_leu, @Berdir thx for your work.

Status: Fixed » Closed (fixed)

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