Skip to content

Instantly share code, notes, and snippets.

@levmyshkin
Created November 19, 2021 14:24
Show Gist options
  • Save levmyshkin/fb5794412c62ff31a69a08b1492621d0 to your computer and use it in GitHub Desktop.
Save levmyshkin/fb5794412c62ff31a69a08b1492621d0 to your computer and use it in GitHub Desktop.
Drupal Dependencies Injection in BaseForm
<?php
namespace Drupal\drupalbook\Form;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Egulias\EmailValidator\EmailValidator;
/**
* Class ModalForm.
*/
class ModalForm extends FormBase {
/**
* Email validator.
*
* @var \Egulias\EmailValidator\EmailValidator
*/
protected $emailValidator;
/**
* Constructs a new ModalForm.
*
* @param \Egulias\EmailValidator\EmailValidator $email_validator
* The email validator.
*/
public function __construct(EmailValidator $email_validator) {
$this->emailValidator = $email_validator;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('email.validator')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'drupalbook_contact_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $options = NULL) {
$form['email'] = [
'#type' => 'email',
'#title' => $this->t('Email'),
'#description' => $this->t('Your email address'),
'#required' => TRUE,
];
$form['actions']['send'] = [
'#type' => 'submit',
'#value' => $this->t('Send'),
];
return $form;
}
/**
* Email validation.
*/
protected function validateEmail(array &$form, FormStateInterface $form_state) {
if (!$this->emailValidator->isValid($form_state->getValue('email'))) {
return FALSE;
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$email = $form_state->getValue('email');
if (!empty($email)) {
if (!$this->validateEmail($form, $form_state)) {
$form_state->setErrorByName('email', $this->t('%email is an invalid email address.', array('%email' => $email)));
}
}
else {
$form_state->setErrorByName('email', $this->t("Please enter an email address."));
}
$form_state->setValue('email', $email);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
/**
* Gets the configuration names that will be editable.
*
* @return array
* An array of configuration object names that are editable if called in
* conjunction with the trait's config() method.
*/
protected function getEditableConfigNames() {
return ['config.drupalbook_contact_form'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment