SiteSettingsConfigForm.php in Site Settings and Labels 8
File
src/Form/SiteSettingsConfigForm.php
View source
<?php
namespace Drupal\site_settings\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\KeyValueStore\KeyValueDatabaseFactory;
class SiteSettingsConfigForm extends ConfigFormBase {
protected $keyvalueDatabase;
public function __construct(ConfigFactoryInterface $config_factory, KeyValueDatabaseFactory $keyvalue_database) {
parent::__construct($config_factory);
$this->keyvalueDatabase = $keyvalue_database;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('keyvalue.database'));
}
protected function getEditableConfigNames() {
return [
'site_settings.config',
];
}
public function getFormId() {
return 'site_settings_config_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('site_settings.config');
$form['template_key'] = [
'#type' => 'machine_name',
'#title' => $this
->t('Template key'),
'#description' => $this
->t('The key at which site settings should be made available in templates such as {{ site_settings.your_settings_group.your_setting_name }} with a template key of "site_settings".'),
'#default_value' => $config
->get('template_key'),
'#required' => TRUE,
'#machine_name' => [
'exists' => [
$this,
'machineNameExists',
],
],
];
return parent::buildForm($form, $form_state);
}
public function machineNameExists($value) {
return FALSE;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this
->config('site_settings.config')
->set('template_key', $form_state
->getValue('template_key'))
->save();
}
}