Unable to add map link in popup

Created on 13 January 2025, 3 months ago

I am a bit lost at what happened here. We were using a bit of code in a preprocess paragraph to add a link with the following code

    /** @var \Drupal\address_map_link\MapLinkManager $mapLinkManager */
    $mapLinkManager = Drupal::service('plugin.manager.map_link');
    // Specify the map link plugin type to use.
    /** @var \Drupal\address_map_link\Plugin\MapLink\GoogleMaps $mapLinkType */
    $mapLinkType = $mapLinkManager->createInstance('google_maps');
    // Pass Address to getAddressUrl and create a URL based on the address.
    // directions_url is now available for use in the node template.
    $url = $mapLinkType->getAddressUrl($address)->setOption('attributes', ['target' => '_blank']);
    $map_link = Link::fromTextAndUrl(t('Show on Google Maps'), $url)->toString();
    if (isset($variables["content"]["field_geofield"][0]["#attached"]["drupalSettings"]["leaflet"])) {
      $attached_key = key($variables["content"]["field_aos_pc10_geofield"][0]["#attached"]["drupalSettings"]["leaflet"]);
      $variables["content"]["field_geofield"][0]["#attached"]["drupalSettings"]["leaflet"][$attached_key]["features"][0]["popup"] .= '</br>' . $map_link;
    }

But now we get the error undefined array key 0 and popup. How can I check where to add this part on the new code?

💬 Support request
Status

Active

Version

10.2

Component

Code

Created by

🇪🇸Spain uridrupal

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

Comments & Activities

  • Issue created by @uridrupal
  • 🇮🇹Italy itamair

    Thanks @uridrupal for reporting this.
    BUT ... could you be more detailed in reporting this?
    When the regressions exactly happened to you? In which circumstances?

    Did you upgrade from which Leaflet version into the 10.2.34 mentioned in this issue of you.
    Because important API changes happened from previous Leaflet versions into 10.x ones.

    Or did it happened in a more recent 10.x.x upgrade?
    You should inspect your upgrades and specify all that ... and may be you (yourselves) should be able to understand what impacted in your custom code alters ...

  • 🇪🇸Spain uridrupal

    I will be adding more context now.

        if (isset($variables["content"]["field_geofield"][0]["#attached"]["drupalSettings"]["leaflet"])) {
          $attached_key = key($variables["content"]["field_geofield"][0]["#attached"]["drupalSettings"]["leaflet"]);
          $view = $address->view();
          $address_view = Drupal::service('renderer')->renderPlain($view);
          $variables["content"]["field_geofield"][0]["#attached"]["drupalSettings"]["leaflet"][$attached_key]["features"][0]["popup"]["value"] = $address_view;
          $variables["content"]["field_geofield"][0]["#attached"]["drupalSettings"]["leaflet"][$attached_key]["features"][0]["popup"]["value"] .= '</br>'.$map_link;
        }

    When using this code I get the map with a pin, which on clicking shows the Address and a link to "Show on google maps"

    When trying to use this code with the latest version of leaftlet I get the error message in console

    Uncaught TypeError: lFeature.bindPopup is not a function

      Drupal.Leaflet.prototype.feature_bind_popup = function(lFeature, feature) {
        if (feature.popup && feature.popup.value) {
          const popup_options = feature.popup.options ? JSON.parse(feature.popup.options) : {};
          lFeature.bindPopup(feature.popup.value, popup_options);
        }
      };

    This code was working for version 10.2.12

  • 🇪🇸Spain uridrupal

    The issue wasn't in the module itself. Somehow the configuration was lost and the field weren't using the geocoder, so when doing the preprocess there was no coordinates, therefore no map location and no popup.
    Took a while to figure out because the configuration wasn't changed, but the changes were there.

  • 🇮🇹Italy itamair

    May you try the attached patch, asap?
    Latest release 10.2.34 did change part of that code (that triggers your error) but I cannot figure out why you now trigger/intercept an lFeature object for which the bindPopup method is not defined ...

    Let me know ASAP if it solves this issue of you. Might be a good defensive code addition to commit into a new (last minute) Leaflet release,
    just one day before the official launch of the Drupal CMS (Leaflet module is part of ...)

    • itamair committed 1160aa42 on 10.2.x
      More defensive code in adding Leaflet Popup to the Leaflet Feature, only...
  • 🇮🇹Italy itamair

    Ok. Let's add that ... as I QAed and is not harmful and rather should guarantee better check that .bindPopup method is supported on each lFeature.
    I committed #7 into 10.2.x branch and just deployed a new leaflet 10.2.35 release with all this ...

    Please @uridrupal still upgrade to 10.2.35 and let us know if this new release fixes also this use case issue of you, eventually.

  • 🇪🇸Spain uridrupal

    @itamair The issue as I understand it was that the field was empty because we were not using the Geocoder on the geofield.
    Basically we had an address field with geocoder and the geofield was using that address field to fill the coordinates.
    When assigning values to geofield, it was empty because it was not using any coordinates form the address field.
    Basically that's what I have found out. Not sure if it's an error that should happen, but the root cause for me it was the geofield being empty, so the map was empty, and when adding values to the popup, it was also empty I would assume.

  • 🇮🇹Italy itamair

    Thanks @uridrupal ... all clear.
    Nevertheless the new 10.2.35 release if worth with more defensive check.

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

Production build 0.71.5 2024