Skip to content
Snippets Groups Projects
Commit 36c18624 authored by Dezső Biczó's avatar Dezső Biczó Committed by Björn Brala
Browse files

Issue #3351101 by mxr576, bbrala, Agnes_Varro: Should respect...

Issue #3351101 by mxr576, bbrala, Agnes_Varro: Should respect jsonapi.base_path container parameter override
parent f2babd5d
No related branches found
No related tags found
1 merge request!29Issue #3351101: Should respect jsonapi.base_path container parameter override
Pipeline #17526 failed
......@@ -60,6 +60,7 @@ class ConfigSubscriber implements EventSubscriberInterface {
$container = \Drupal::getContainer();
// It is problematic to rebuild the container during the installation.
$should_process = $container->getParameter('kernel.environment') !== 'install'
&& (!$container->hasParameter('jsonapi_extras.base_path_override_disabled') || !$container->getParameter('jsonapi_extras.base_path_override_disabled'))
&& $event->getConfig()->getName() === 'jsonapi_extras.settings';
if ($should_process) {
// @see \Drupal\jsonapi_extras\JsonapiExtrasServiceProvider::alter()
......
......@@ -27,6 +27,13 @@ class JsonapiExtrasSettingsForm extends ConfigFormBase {
*/
protected $jsonApiResourceRepository;
/**
* The dependency injection container.
*
* @var \Drupal\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
* Constructs a \Drupal\system\ConfigFormBase object.
*
......@@ -36,11 +43,18 @@ class JsonapiExtrasSettingsForm extends ConfigFormBase {
* The router builder to rebuild menus after saving config entity.
* @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $jsonApiResourceRepository
* Resource type repository.
* @param \Drupal\Component\DependencyInjection\ContainerInterface|null $container
* The dependency injection container.
*/
public function __construct(ConfigFactoryInterface $config_factory, RouteBuilder $router_builder, ResourceTypeRepositoryInterface $jsonApiResourceRepository) {
public function __construct(ConfigFactoryInterface $config_factory, RouteBuilder $router_builder, ResourceTypeRepositoryInterface $jsonApiResourceRepository, ContainerInterface $container = NULL) {
parent::__construct($config_factory);
$this->routerBuilder = $router_builder;
$this->jsonApiResourceRepository = $jsonApiResourceRepository;
if ($container === NULL) {
$container = \Drupal::getContainer();
@trigger_error('Calling ' . __METHOD__ . ' without the $container argument is deprecated in jsonapi_extras:8.x-3.24 and will be required in jsonapi_extras:8.x-4.0. See https://www.drupal.org/node/3384627', E_USER_DEPRECATED);
}
$this->container = $container;
}
/**
......@@ -50,7 +64,8 @@ class JsonapiExtrasSettingsForm extends ConfigFormBase {
return new static(
$container->get('config.factory'),
$container->get('router.builder'),
$container->get('jsonapi.resource_type.repository')
$container->get('jsonapi.resource_type.repository'),
$container
);
}
......@@ -73,6 +88,10 @@ class JsonapiExtrasSettingsForm extends ConfigFormBase {
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('jsonapi_extras.settings');
$path_prefix_default_value = $config->get('path_prefix');
if ($this->isJsonApiBasePathParameterOverrideActive()) {
$path_prefix_default_value = ltrim($this->container->getParameter('jsonapi.base_path'), '/');
}
$form['path_prefix'] = [
'#title' => $this->t('Path prefix'),
......@@ -80,9 +99,14 @@ class JsonapiExtrasSettingsForm extends ConfigFormBase {
'#required' => TRUE,
'#field_prefix' => '/',
'#description' => $this->t('The path prefix for JSON:API.'),
'#default_value' => $config->get('path_prefix'),
'#disabled' => $this->isJsonApiBasePathParameterOverrideActive(),
'#default_value' => $path_prefix_default_value,
];
if ($this->isJsonApiBasePathParameterOverrideActive()) {
$form['path_prefix']['#description'] = $this->t('@original <strong>This configuration option is disabled because the JSON:API base path is overridden via the <em>jsonapi.base_path</em> container parameter.</strong>', ['@original' => $form['path_prefix']['#description']]);
}
$form['include_count'] = [
'#title' => $this->t('Include count in collection queries'),
'#type' => 'checkbox',
......@@ -104,7 +128,7 @@ class JsonapiExtrasSettingsForm extends ConfigFormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($path_prefix = $form_state->getValue('path_prefix')) {
if (!$this->isJsonApiBasePathParameterOverrideActive() && ($path_prefix = $form_state->getValue('path_prefix'))) {
$this->config('jsonapi_extras.settings')
->set('path_prefix', trim($path_prefix, '/'))
->save();
......@@ -124,4 +148,14 @@ class JsonapiExtrasSettingsForm extends ConfigFormBase {
parent::submitForm($form, $form_state);
}
/**
* Checks if jsonapi.base_path container parameter override is active.
*
* @return bool
* TRUE if it is active, FALSE otherwise.
*/
private function isJsonApiBasePathParameterOverrideActive(): bool {
return $this->container->hasParameter('jsonapi_extras.base_path_override_disabled') && $this->container->getParameter('jsonapi_extras.base_path_override_disabled');
}
}
......@@ -20,7 +20,13 @@ class JsonapiExtrasServiceProvider extends ServiceProviderBase {
->read('jsonapi_extras.settings');
if ($settings !== FALSE) {
$container->setParameter('jsonapi.base_path', '/' . $settings['path_prefix']);
if ($container->getParameter('jsonapi.base_path') !== '/jsonapi') {
$container->setParameter('jsonapi_extras.base_path_override_disabled', TRUE);
}
else {
$container->setParameter('jsonapi.base_path', '/' . $settings['path_prefix']);
$container->setParameter('jsonapi_extras.base_path_override_disabled', FALSE);
}
}
// Enable normalizers in the "src-impostor-normalizers" directory to be
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment