Skip to content

Instantly share code, notes, and snippets.

@danielnv18
Created January 4, 2015 21:02
Show Gist options
  • Save danielnv18/b9bdb5c761266336e5a2 to your computer and use it in GitHub Desktop.
Save danielnv18/b9bdb5c761266336e5a2 to your computer and use it in GitHub Desktop.
Create a form in drupal 8
<?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();
}
}
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