Scroll
12.15.3. Dependencies Injection в BaseForm
В прошлых статьях мы разобрали что такое Services, Dependency Injection (DI) и как их использовать в своем Controller и блоке:
12.15. Services и Dependency Injection.
12.15.1. Dependency Injection в контроллере
12.15.2. Dependency Injection в блоке
В этой статье пример как добавлять сервисы через DI в класс формы наследующий FormBase :
<?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'];
}
}
Для вызова сервиса через DI в форме мы используем методы __construct и create(), также как мы делали это для блока.