Created
January 13, 2016 15:32
-
-
Save sillygwailo/6604bf20ba1424b524c1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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\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