Geolocation module - how to call abstract class method properly - 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...

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

Class 'CWebModule' not found when adding Yii2 Quiz

I have added yii2 quiz module in my project
I have added the module inside the frontend/modules/quiz
Then in the app's main.php i have added something like
'modules' => [
'quiz' => [
'class' => 'app\modules\quiz\Module',
]
],
Then after running it, I'm getting the "CWebModule not found".
class Module extends CWebModule{
......
}
My module class extending CWebModule.
How to fix this error?

Yii2 Component class not loading config when instantiated

I have created a simple custom Component that extends from yii\base\Component.
namespace app\components\managers;
use yii\base\Component;
use yii\base\InvalidConfigException;
class HubspotDataManager extends Component
{
public $hubspotApiKey;
private $apiFactory;
public function init()
{
if (empty($this->hubspotApiKey)) {
throw new InvalidConfigException('Hubspot API Key cannot be empty.');
}
parent::init();
// initialise Hubspot factory instance after configuration is applied
$this->apiFactory = $this->getHubspotApiFactoryInstance();
}
public function getHubspotApiFactoryInstance()
{
return new \SevenShores\Hubspot\Factory([
'key' => $this->hubspotApiKey,
'oauth' => false, // default
'base_url' => 'https://api.hubapi.com' // default
]);
}
}
I have registered the component in my config/web.php application config, where I have also added a custom parameter.
'components' => [
...
'hubspotDataManager' => [
'class' => app\components\managers\HubspotDataManager::class,
'hubspotApiKey' => 'mycustomkeystringhere',
],
...
],
However, I find that when I instantiate my component like so:
$hubspot = new HubspotDataManager();
this hubspotApiKey config parameter is not passed into the __construct($config = []) - $config is just an empty array, so in init() the config does not set the component hubspotApiKey property the the value of hubspotApiKey in the config, so accordingly I see this from my thrown exception:
Invalid Configuration – yii\base\InvalidConfigException
Hubspot API Key cannot be empty.
However, if I call up the component like so:
Yii::$app->hubspotDataManager
it does pass this config variable in! Why is this? What extra legwork must I do to get the component to load it's application config data for standard class instantiation? I cannot find anything about this particular scenario in the docs.
NOTE: Using latest Yii2 version 2.0.15.1 using basic application template.
When you create an instance without using the Service Locator, the configuration is of course not known.
The flow is like this, Yii::$app is a Service Locator. It will pass the configuration to the Dependency Injector containter Yii::$container.
If you want to pass the config without using the Service Locator Yii::$app, you could set the container:
Yii::$container->set(app\components\managers\HubspotDataManager::class, [
'hubspotApiKey' => 'mycustomkeystringhere',
]);
and
$hubspot = Yii::$container->get(app\components\managers\HubspotDataManager::class);
the result would be the same as using the service locator Yii::$app.
You could also instantiate the a new instance of the class like this and pass the configuration to it.
$hubspot = new HubspotDataManager([
'hubspotApiKey' => 'mycustomkeystringhere',
]);

CakePhp 3 Plugin Authentication adapter was not found

I'm kinda a newbie in Cakephp (3.5) and I'm currently trying to make my first plugin (called Example) which contains several sub-directories. One of them is the UserManager directory which contains a Users MVC standard suit with authentication.
Since I want to add social logins and other stuffs, I created my own auth component as explained in the docs :
plugins/Example/UserManager/src/Controller/AppController.php
<?php
namespace Example\UserManager\Controller;
use App\Controller\AppController as BaseController;
class AppController extends BaseController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('Auth', [
'authenticate' => [
'Example/UserManager.Example' => [
'fields' => ['username' => 'email', 'password' => 'pass'],
'userModel' => 'Users',
],
],
]);
}
}
plugins/Example/UserManager/src/Auth/ExampleAuthenticate.php
<?php
namespace App\Auth;
use Cake\Auth\BaseAuthenticate;
use Cake\Http\ServerRequest;
use Cake\Http\Response;
class ExampleAuthenticate extends BaseAuthenticate
{
// The same as Form authentication, since I'm testing
}
The problem is that I can't make the authentication component find the ExampleAuthenticate class. I already tried by setting the authenticate config param like
Example
ExampleAuthenticate
UserManager.Example
Example/UserManager.Example
Example\UserManager.Example
Example/UserManager.ExampleAuthenticate
Example\UserManager.ExampleAuthenticate
but I always get the error Authentication adapter "..." was not found. when visiting http://localhost/Project/example/user-manager/users :(
Does anyone have any clue of what I might be missing?
The problem was that the php function class_exists(...) didn't recognise the custom authentication class, so after digging a bit more I realised that the namespace shown in the docs only works for a custom authentication file defined in the App environment, but not in the Plugin one (silly me).
So I changed namespace App\Auth; to namespace Example\UserManager\Auth; inside ExampleAuthenticate.php and it worked like a charm! now the function class_exists('Example\\UserManager\\Auth\\ExampleAuthenticate') returns true and everything works perfect by defining Example/UserManager.Example in the authenticate config params.

ZF2 view plugin manager not merging config

On my module.config.php I've got something like:
return [
'view_helpers' => [
'invokables' => [
'mycustomviewhelper' => 'Namespace\View\Helper\MyCustomViewHelper',
],
],
];
I have also got a utility class that will handle the responsibility of rendering a helper. Something like Zend\Paginator.
Zend\Paginator has a __toString() method that proxies to render() call, which instantiates View\Renderer\PhpRenderer() and then calls $view->paginationControl($this).
I am trying to replicate the similar functionality in my utility class, which has similar strategy to what Zend\Paginator already does, the only thing being different is my view helper is a custom one. Hence, my code looks like:
$view->MyCustomViewHelper($this);
This does not work, because the PhpRenderer ignores the config defined manually and does the following in getHelperPluginManager:
$this->setHelperPluginManager(new HelperPluginManager());
I've tried invoking the helpers already defined in ViewHelperManager and this works well.
I did try merging in the config beforehand and then setting the PhpRenderer in the view but then this caused other problems, such as my partials were not found etc.
Now my question is why does ZF not consider any custom registered views when trying to render it in isolation. Is there any other way to do this?
Thank you.
Right, after a bit of a debugging, and playing with the configs, I was able to make this work. Still not sure if this is the best way to do this, but looks like currently there's no other way to make it work.
I created a factory for the utility class, instantiated the PhpRenderer, and then merged in my config with the ViewPluginManager manually. My factory now looks like:
public function createService(ServiceLocatorInterface $serviceLocatorInterface)
{
$dataTable = new DataTable;
$phpRenderer = new PhpRenderer();
$config = new \Zend\ServiceManager\Config([
'invokables' => [
'datatablerenderer' => 'DataTable\View\Helper\DataTableRenderer'
],
]);
$phpRenderer->setHelperPluginManager(new HelperPluginManager($config));
$dataTable->setView($phpRenderer);
return $dataTable;
}
However will have to refactor this so that the config comes from the view_helpers config key and is not hardcoded.

Namespace error when using gridview using custom theme in yii2

I am new to yii2, but I have been using Yii 1.x for quite a long time. I am using advanced template of Yii2 and implementing custom theme for the backend. I am using https://github.com/mithun12000/adminUI theme for the backend. I have set up my theme as follows:
install theme using composer
added theme support in backend/config/main.php as follows:
'view'=>[
'theme'=>[
'pathMap'=>['#app/views'=>'#webroot/themes/admin/views'],
'baseUrl'=>'#web/themes/admin'
]
],
Changed namespace app\assets; into namespace backend\assets; in backend/assets/AppAsset.php
I created my theme in web folder as backend/web/themes/admin and put my views there.
In my controller, to use the theme I just created, I put the following code:
$this->getView()->theme = Yii::createObject([
'class' => '\yii\base\Theme',
'pathMap' => ['#backend/views' => '#webroot/themes/admin/views'],
'baseUrl' => '#web/themes/admin',
]);
The login screen works fine. But if I have any widget, suppose Gridview, then I get namespace error. The error I get is:
Unknown Class – yii\base\UnknownClassException
Unable to find 'app\assets\AppAsset' in file: D:\projects\bmjobs\site\backend/assets/AppAsset.php. Namespace missing?
If I change the namespace in AppAsset.php to app\assets, then I get the following error:
PHP Fatal Error – yii\base\ErrorException
Call to a member function checkAccess() on a non-object
I am not sure where I went wrong. Can anybody please help me out with this?
Thanks in advance.
you may change your pathMap in backend/config/main.php
'pathMap' => ['#app/views' => '#app/themes/admin/views'],

Categories