Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll

9.15.3. Dependencies Injection in BaseForm

16/03/2021, by Ivan

In previous articles, we covered such services, dependency injection (DI) and how to use them in your controller and block:

9.15. Services and Dependency Injection.
9.15.1. Dependency Injection in Controller
9.15.2. Dependency Injection in Block

Code examples can be viewed on github:
https://github.com/levmyshkin/drupalbook8

In this article, an example of how to add services via DI to a form class inheriting 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'];
  }
}

To call a service via DI on a form, we use the __construct() and create() methods, just as we did for the block.