Skip to content
Snippets Groups Projects
Commit 17738397 authored by Vojislav Jovanovic's avatar Vojislav Jovanovic
Browse files

Issue #3311019 by vaish, chrisolof: Module configuration issues

parent 5832315a
No related branches found
No related tags found
1 merge request!12Improve module's configuration
<?php
/**
* @file
* Install, update, and uninstall functions for the Address Autocomplete module.
*/
/**
* Issue #3311019: Un-serialize AddressProvider plugin config.
*/
function address_autocomplete_update_8800() {
$address_provider_plugins = \Drupal::service('plugin.manager.address_provider')
->getDefinitions();
$settings = \Drupal::configFactory()
->getEditable('address_autocomplete.settings');
foreach (array_keys($address_provider_plugins) as $plugin_id) {
$plugin_config = $settings->get($plugin_id);
if (is_string($plugin_config)) {
$settings->set($plugin_id, unserialize($plugin_config));
}
}
$settings->save();
}
address_autocomplete.settings:
type: config_object
label: Address autocomplete settings
mapping:
active_plugin:
type: string
label: Active address provider plugin
france_address:
type: mapping
label: Settings for the France Address address provider plugin
mapping:
plugin_id:
type: string
label: Provider plugin ID
endpoint:
type: uri
label: Data API Address endpoint
google_maps:
type: mapping
label: Settings for the Google Maps address provider plugin
mapping:
plugin_id:
type: string
label: Provider plugin ID
api_key:
type: string
label: API Key
mapbox_geocoding:
type: mapping
label: Settings for the Mapbox Geocoding address provider plugin
mapping:
plugin_id:
type: string
label: Provider plugin ID
token:
type: string
label: Token
post_ch:
type: mapping
label: Settings for the Post CH address provider plugin
mapping:
plugin_id:
type: string
label: Provider plugin ID
endpoint:
type: uri
label: post.ch API endpoint
username:
type: string
label: post.ch API username
password:
type: string
label: post.ch API password
......@@ -11,10 +11,8 @@ class ProviderSettingsForm extends ConfigFormBase {
/**
* Name of the config.
*
* @var string
*/
public static $configName = 'address_autocomplete.settings';
protected const CONFIG_NAME = 'address_autocomplete.settings';
/**
* @inheritDoc
......@@ -41,7 +39,7 @@ class ProviderSettingsForm extends ConfigFormBase {
* @inheritDoc
*/
protected function getEditableConfigNames() {
return [ProviderSettingsForm::$configName];
return [self::CONFIG_NAME];
}
/**
......@@ -63,7 +61,9 @@ class ProviderSettingsForm extends ConfigFormBase {
* @inheritDoc
*/
public function getPluginInstance($plugin_id) {
return $this->providerManager->createInstance($plugin_id);
$editable_config = $this->config(self::CONFIG_NAME)->get($plugin_id) ?? [];
return $this->providerManager->createInstance($plugin_id, $editable_config);
}
/**
......@@ -92,9 +92,9 @@ class ProviderSettingsForm extends ConfigFormBase {
$plugin_id = $this->getPluginIdFromRequest();
$instance = $this->getPluginInstance($plugin_id);
$instance->submitConfigurationForm($form, $form_state);
$config = $this->config(ProviderSettingsForm::$configName);
$config = $this->config(self::CONFIG_NAME);
$instanceConfiguration = $instance->getConfiguration();
$config->set($plugin_id, serialize($instanceConfiguration));
$config->set($plugin_id, $instanceConfiguration);
$config->save();
parent::submitForm($form, $form_state);
......
......@@ -2,7 +2,6 @@
namespace Drupal\address_autocomplete\Plugin;
use Drupal\address_autocomplete\Form\ProviderSettingsForm;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Form\FormStateInterface;
use GuzzleHttp\Client;
......@@ -14,35 +13,40 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class AddressProviderBase extends PluginBase implements AddressProviderInterface {
/**
* @inheritDoc
* The Guzzle HTTP Client.
*
* @var \GuzzleHttp\Client
*/
protected $client;
/**
* @inheritDoc
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, Client $client) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->setConfiguration($configuration);
$this->client = new Client();
$this->client = $client;
}
/**
* @inheritDoc
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
// Swap config with a values from config object.
$configObject = $container->get('config.factory')
->get(ProviderSettingsForm::$configName);
if ($configurationSerialized = $configObject->get($plugin_id)) {
$configuration = unserialize($configurationSerialized);
if (empty($configuration)) {
// Load runtime config for this address provider.
$configuration = $container
->get('config.factory')
->get('address_autocomplete.settings')
->get($plugin_id);
$configuration ??= [];
}
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('http_client')
);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment