Created
November 19, 2021 13:45
-
-
Save levmyshkin/47a39465edd33edb56ce5d628f82f86e to your computer and use it in GitHub Desktop.
Add Drupal configuration form programmatically
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
name: DrupalBook | |
description: Custom module for learning Drupal | |
type: module | |
core: 8.x | |
core_version_requirement: ^8 || ^9 | |
package: DrupalBook |
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
drupalbook.settings: | |
path: '/admin/structure/drupalbook/settings' | |
defaults: | |
_form: '\Drupal\drupalbook\Form\DrupalbookSettingsForm' | |
_title: 'DrupalBook Settings form' | |
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 | |
// /modules/custom/drupalbook/src/Form/DrupalbookSettingsForm.php | |
namespace Drupal\drupalbook\Form; | |
use Drupal\Core\Form\ConfigFormBase; | |
use Drupal\Core\Form\FormStateInterface; | |
/** | |
* Configure example settings for this site. | |
*/ | |
class DrupalbookSettingsForm extends ConfigFormBase { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getFormId() { | |
return 'drupalbook_admin_settings'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function getEditableConfigNames() { | |
return [ | |
'drupalbook.settings', | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(array $form, FormStateInterface $form_state) { | |
$config = $this->config('drupalbook.settings'); | |
$form['drupalbook_api_key'] = array( | |
'#type' => 'textfield', | |
'#title' => $this->t('API Key'), | |
'#default_value' => $config->get('drupalbook_api_key'), | |
); | |
$form['drupalbook_api_client_id'] = array( | |
'#type' => 'textfield', | |
'#title' => $this->t('API Client ID'), | |
'#default_value' => $config->get('drupalbook_api_client_id'), | |
); | |
return parent::buildForm($form, $form_state); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function submitForm(array &$form, FormStateInterface $form_state) { | |
// Retrieve the configuration | |
$this->configFactory->getEditable('drupalbook.settings') | |
// Set the submitted configuration setting | |
->set('drupalbook_api_key', $form_state->getValue('drupalbook_api_key')) | |
// You can set multiple configurations at once by making | |
// multiple calls to set() | |
->set('drupalbook_api_client_id', $form_state->getValue('drupalbook_api_client_id')) | |
->save(); | |
parent::submitForm($form, $form_state); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment