TokenVarConfig.php in Token Variable 8
File
src/Form/TokenVarConfig.php
View source
<?php
namespace Drupal\token_var\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Diff\DiffFormatter;
use Drupal\system\FileDownloadController;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TokenVarConfig extends ConfigFormBase implements ContainerInjectionInterface {
public function getFormId() {
return 'token_var_config_form';
}
protected function getEditableConfigNames() {
return [
'token_var.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = \Drupal::config('token_var.settings')
->get('token_var_replacements');
$form = parent::buildForm($form, $form_state);
$form['#tree'] = TRUE;
foreach ($this->configFactory
->listAll() as $name) {
$form['token_var'][$name] = array(
'#type' => 'details',
'#title' => $name,
);
foreach ($this->configFactory
->get($name)
->getRawData() as $key => $value) {
if (!is_array($value) && !is_object($value)) {
$default_value = isset($config[str_replace('.', '|', $name)][$key]) ? 1 : 0;
$form['token_var'][$name][$key] = array(
'#type' => 'checkbox',
'#title' => $key,
'#default_value' => $default_value,
);
}
}
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues()['token_var'];
$config = $this
->config('token_var.settings');
$enabled_vars = array();
foreach ($values as $name => $data) {
$name = str_replace('.', '|', $name);
foreach ($data as $key => $value) {
if ($value) {
$enabled_vars[$name][$key] = $value;
}
}
}
$config
->set('token_var_replacements', $enabled_vars)
->save();
token_clear_cache();
parent::submitForm($form, $form_state);
}
}