Created
January 4, 2015 21:02
-
-
Save danielnv18/b9bdb5c761266336e5a2 to your computer and use it in GitHub Desktop.
Create a form in drupal 8
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\demo\Form\AdminForm. | |
*/ | |
namespace Drupal\demo\Form; | |
use Drupal\Core\Form\ConfigFormBase; | |
use Drupal\Core\Form\FormStateInterface; | |
class AdminForm extends ConfigFormBase | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getFormID() { | |
return 'admin_form'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(array $form, FormStateInterface $form_state) { | |
$config = $this->config('demo.admin_form_config'); | |
$form['name'] = [ | |
'#type' => 'textfield', | |
'#title' => $this->t('Name'), | |
'#description' => $this->t(''), | |
'#default_value' => $config->get('name'), | |
]; | |
return parent::buildForm($form, $form_state); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function validateForm(array &$form, FormStateInterface $form_state) { | |
return parent::validateForm($form, $form_state); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function submitForm(array &$form, FormStateInterface $form_state) { | |
parent::submitForm($form, $form_state); | |
$this->config('demo.admin_form_config') | |
->set('name', $form_state->getValue('name')) | |
->save(); | |
} | |
} |
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
demo.admin_form: | |
path: 'admin/config/demo/settings' | |
defaults: | |
_form: '\Drupal\demo\Form\AdminForm' | |
_title: 'demo AdminForm Form' | |
requirements: | |
_permission: 'access administration pages' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment