Geolocation module - how to call abstract class method properly

Viewed 84

I'm using the custom module in my project, and it was working fine with Geolocation 1.11 module.

After I've updated Geolocation to 3.7 my custom module stopped working.

What I've found - my custom module was using trait from 'GoogleMapsDisplayTrait.php' and now it is missing in Geolocation. It was like that

class LocationsMapBlock extends BlockBase {  

  use GoogleMapsDisplayTrait;

  public function build() {
    return [
      '#theme' => 'locations_map',
      '#attached' => [
        'library' => ['location/map'],
        'drupalSettings' => [
          'geolocation' => [
            'google_map_url' => $this->getGoogleMapsApiUrl(),
          ],
          'locations' => $this->getLocationMapData(),
        ],
      ],
    ];
  }
}

I've found that now most similar class to trait GoogleMapsDisplayTrait) is inside geolocation_google_maps submodule of geolocation module.

But now it's not a trait, but abstract class with the same methods as previous trait was.

abstract class GoogleMapsProviderBase extends MapProviderBase {
...
}

I've tried to add this class:

use Drupal\geolocation_google_maps\GoogleMapsProviderBase;

But now I'm receiving the error

'Error: Call to undefined method Drupal\location\Plugin\Block\LocationsMapBlock::getGoogleMapsApiUrl() in Drupal\location\Plugin\Block\LocationsMapBlock->build() (line 32 of modules/custom/location/src/Plugin/Block/LocationsMapBlock.php).'

My PHP knowledges is to weak to find the solution...

1 Answers

The undefined function getGoogleMapsApiUrl() is, maybe the file is missing?

Check if the file exists:

/modules/geolocation/modules/geolocation_google_maps/src/GoogleMapsProviderBase.php

Try to install and enable the following modules for a cleaner working with geolocation - specially for the geolocation_google_maps:

geofield
geolocation_geofield
geolocation_google_maps

Try using this call instead of $this->getGoogleMapsApiUrl():

Drupal::service('plugin.manager.geolocation.mapprovider')->getMapProvider('google_maps')->getGoogleMapsApiUrl()
Related