Skip to content

Instantly share code, notes, and snippets.

@sillygwailo
Created January 13, 2016 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sillygwailo/6604bf20ba1424b524c1 to your computer and use it in GitHub Desktop.
Save sillygwailo/6604bf20ba1424b524c1 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Contains \Drupal\configform_example\Form\ConfigFormExampleConfigForm.
*/
namespace Drupal\configform_example\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class ConfigFormExampleConfigForm extends ConfigFormBase {
/**
* {@inheritdoc}.
*/
public function getFormId() {
return 'configform_example_form';
}
/**
* {@inheritdoc}.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$config = $this->config('configform_example.settings');
$form['email'] = [
'#type' => 'email',
'#title' => $this->t('Your .com email address.'),
'#default_value' => $config->get('email_address'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if (strpos($form_state->getValue('email'), '.com') === FALSE ) {
$form_state->setErrorByName('email', $this->t('This is not a .com email address.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('configform_example.settings');
$config->set('email_address', $form_state->getValue('email'));
$config->save();
return parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['configform_example.settings'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment