地理定位模块-如何正确调用抽象类方法

发布于 2025-01-09 19:17:27 字数 1348 浏览 1 评论 0原文

我在项目中使用自定义模块,它与 Geolocation 1.11 模块配合得很好。

Geolocation 更新到 3.7 后,我的自定义模块停止工作。

我发现 - 我的自定义模块正在使用“GoogleMapsDisplayTrait.php”中的特征,但现在它在Geolocation中丢失了。就像

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(),
        ],
      ],
    ];
  }
}

我发现现在与特征 GoogleMapsDisplayTrait) 最相似的类位于 geolocation 模块的 geolocation_google_maps 子模块内。

但现在它不是一个特征,而是一个抽象类,其方法与以前的特征相同。

abstract class GoogleMapsProviderBase extends MapProviderBase {
...
}

我尝试添加此类:

use Drupal\geolocation_google_maps\GoogleMapsProviderBase;

但现在我收到错误

“错误:调用 Drupal\location\Plugin\Block\LocationsMapBlock::getGoogleMapsApiUrl() in Drupal\location\Plugin\Block\LocationsMapBlock-> 中的未定义方法” ;build()(modules/custom/location/src/Plugin/Block/LocationsMapBlock.php 第 32 行)。

我的 PHP 知识太薄弱,无法找到解决方案......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅一夜美梦 2025-01-16 19:17:27

未定义的函数getGoogleMapsApiUrl()是,也许文件丢失了?

检查文件是否存在:

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

尝试安装并启用以下模块,以便清理器使用地理定位 - 特别是 geolocation_google_maps

geofield
geolocation_geofield
geolocation_google_maps

尝试使用此模块调用而不是 $this->getGoogleMapsApiUrl()

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文