We are using a customized version of TacJS for a multi-domain → Drupal 9 instance.
In current state of the module, we have to duplicate a lot of existing code.
A few small updates to the code of the module who allows us to drastically reduce our amount of duplicated code.
We mostly have to duplicate the form code (buildForm
and submitForm
).
First of all, access to the config
object should be done in a custom method to we can override it easily to access to our custom domain specific configuration.
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('tacjs.settings');
(...)
Should be converted to something like:
/**
* Get configuration.
*/
protected function getConfig() {
return $this->config('tacjs.settings');
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->getConfig();
(...)
Our domain getConfig
method would look something like:
/**
* Get configuration.
*/
protected function getConfig() {
$domain_id = $this->getRequest()->get('domain_id');
$config = $this->config('tacjs_domain.' . $domain_id . '.settings');
if ($config->isNew()) {
$config->merge($this->config('tacjs.settings')->getRawData());
}
return $config;
}
Ideally the form construction should also be done in its own distinct method so we could easily override it to add our domain related elements.
For example through a new addFormElements
method:
/**
* Add elements to form.
*/
protected function addFormElements(array &$form, Config $config) {
foreach ($this->languageManager->getLanguages() as $key => $value) {
$langcodes[$key] = $value->getName();
}
$form['services'] = [
'#type' => 'vertical_tabs',
'#title' => $this->t('Add Services'),
'#title_display' => 'invisible',
];
(...)
Our custom domain version would then look like:
/**
* Add elements to form.
*/
protected function addFormElements(array &$form, Config $config) {
$domain_id = $this->getRequest()->get('domain_id');
$form['domain_id'] = [
'#type' => 'hidden',
'#title' => $this->t('Domain ID'),
'#default_value' => $domain_id,
];
$form['domain'] = [
'#prefix' => '<p>',
'#suffix' => '</p>',
'#markup' => t('Domain: @domain_id', ['@domain_id' => $domain_id]),
'#weight' => -100,
];
parent::addFormElements($form, $config);
}
The final buildForm
method that wouldn't need to be overridden would look like:
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->getConfig();
$this->addFormElements($form, $config);
return parent::buildForm($form, $form_state);
}
The same type of small refactoring should be done on the submitForm
method.
This update would drastically facilitate the maintenance of your domain → compatible version of TacJS and eventually allow us to publish it on Drupal.org website.
If interested, I'm willing to prepare a MR request implementing the required changes.
Thanks for all.
Fixed
6.2
Code
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.