Created
March 11, 2017 18:18
-
-
Save ggagosh/eb0c8d885135472efdbc0aa64b344d5d to your computer and use it in GitHub Desktop.
SiteSettingsForm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# use unique name for route | |
site_global_config.settings: | |
path: '/admin/config/system/site-configs' | |
defaults: | |
_form: '\Drupal\module_name\Form\SiteSettingsForm' | |
_title: 'Site Configs' | |
requirements: | |
_permission: 'administer site configuration' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Contains \Drupal\module_name\SiteSettingsForm. | |
*/ | |
namespace Drupal\module_name\Form; | |
use Drupal\Core\Form\ConfigFormBase; | |
use Drupal\Core\Form\FormStateInterface; | |
/** | |
* Configure site settings for this site. | |
*/ | |
class SiteSettingsForm extends ConfigFormBase { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getFormId() { | |
return 'site_settings'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function getEditableConfigNames() { | |
return [ | |
'site.settings', | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(array $form, FormStateInterface $form_state) { | |
$config = $this->config('site.settings'); | |
// Build form object example: | |
$form['test_1'] = array( | |
'#type' => 'textfield', | |
'#title' => $this->t('Title'), | |
'#default_value' => $config->get('test_1') | |
); | |
return parent::buildForm($form, $form_state); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function submitForm(array &$form, FormStateInterface $form_state) { | |
$this->config('site.settings') | |
->set('test_1', $form_state->getValue('test_1')) | |
->save(); | |
parent::submitForm($form, $form_state); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment