Unverified Commit ad01b131 authored by jryanconklin's avatar jryanconklin Committed by RobLoach

Issue #3027672 by jryanconklin: Non-default enabled theme's...

Issue #3027672 by jryanconklin: Non-default enabled theme's component-libraries can override those of the default theme
parent 57259caa
components.config:
path: '/admin/config/components/settings'
defaults:
_form: '\Drupal\components\Form\ComponentsSettingsForm'
_title: 'Component Library Configuration'
requirements:
_permission: 'administer site configuration'
components.settings:
type: config_object
label: 'Component Library Settings'
mapping:
namespace_prefix:
type: boolean
label: 'Prefix Namespaces'
<?php
/**
* @file
* Contains \Drupal\components\Form\ComponentSettingsForm.
*/
namespace Drupal\components\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure components settings for this site.
*/
class ComponentsSettingsForm extends ConfigFormBase {
/** @var string Config settings */
const SETTINGS = 'components.settings';
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'components_settings_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
static::SETTINGS,
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config(static::SETTINGS);
$messenger = \Drupal::messenger();
$messenger->addMessage(
t('Be sure to update any theme or module namespaces before enabling.'),
$messenger::TYPE_WARNING);
$form['namespace_prefix'] = [
'#type' => 'checkbox',
'#title' => $this->t('Prefix Module or Theme Name in Namespace'),
'#default_value' => $config->get('namespace_prefix'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Retrieve the configuration
$this->configFactory->getEditable(static::SETTINGS)
->set('namespace_prefix', $form_state->getValue('namespace_prefix'))
->save();
parent::submitForm($form, $form_state);
}
}
......@@ -37,6 +37,10 @@ class ComponentLibraryLoader extends \Twig_Loader_Filesystem {
// namespace in this Twig loader.
parent::__construct();
// Retrieve config settings for module to determine prefixed namespaces.
// See Drupal\components\Form\ComponentsSettingsForm.php
$config = \Drupal::config('components.settings');
// The Drupal\Core\Template\Loader\FilesystemLoader makes a Twig namespace
// for each module and theme, so we re-create that list here.
$existing_namespaces = array();
......@@ -80,6 +84,12 @@ class ComponentLibraryLoader extends \Twig_Loader_Filesystem {
$paths[$key] = $extension->getPath() . '/' . $path;
}
// Prefix the namespace with original name.
// Prevents overriding components with duplicate valid namespaces.
if (!empty($config->get('namespace_prefix'))) {
$namespace = "{$name}_{$namespace}";
}
$this->libraries[] = array(
'type' => $type,
'name' => $name,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment