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

12.15.4. Dependencies Injection в ConfigFormBase конфигурационной форме

16/03/2021, by Ivan

В прошлых статьях мы разобрали что такое Services, Dependency Injection (DI) и как их использовать в своем контроллере, блоке и форме:

12.15. Services и Dependency Injection.
12.15.1. Dependency Injection в контроллере
12.15.2. Dependency Injection в блоке
12.15.3. Dependencies Injection в BaseForm

В этой статье пример как добавлять сервисы через DI в класс конфигурационной формы наследующий ConfigFormBase :

/modules/contrib/commerce_order_number/src/Form/SettingsForm.php:

<?php

namespace Drupal\commerce_order_number\Form;

use Drupal\commerce_order_number\OrderNumber;
use Drupal\commerce_order_number\OrderNumberFormatterInterface;
use Drupal\commerce_order_number\OrderNumberGeneratorManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Configure commerce_order_number settings for this site.
 */
class SettingsForm extends ConfigFormBase {

  /**
   * The order number generator manager.
   *
   * @var \Drupal\commerce_order_number\OrderNumberGeneratorManager
   */
  protected $orderNumberGeneratorManager;

  /**
   * The order number formatter.
   *
   * @var \Drupal\commerce_order_number\OrderNumberFormatterInterface
   */
  protected $orderNumberFormatter;

  /**
   * Constructs a new SettingsForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\commerce_order_number\OrderNumberGeneratorManager $order_number_generator_manager
   *   The order number generator manager.
   * @param \Drupal\commerce_order_number\OrderNumberFormatterInterface $order_number_formatter
   *   The order number formatter.
   */
  public function __construct(ConfigFactoryInterface $config_factory, OrderNumberGeneratorManager $order_number_generator_manager, OrderNumberFormatterInterface $order_number_formatter) {
    parent::__construct($config_factory);

    $this->orderNumberGeneratorManager = $order_number_generator_manager;
    $this->orderNumberFormatter = $order_number_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('plugin.manager.commerce_order_number_generator'),
      $container->get('commerce_order_number.order_number_formatter')
    );
  }

  /**
   * @inheritDoc
   */
  protected function getEditableConfigNames() {
    return ['commerce_order_number.settings'];
  }

  /**
   * @inheritDoc
   */
  public function getFormId() {
    return 'commerce_order_number_settings';
  }

  /**
   * @inheritDoc
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('commerce_order_number.settings');
    ...
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('commerce_order_number.settings')
      ->set('generator', $form_state->getValue('generator'))
      ->set('padding', $form_state->getValue('padding'))
      ->set('pattern', $form_state->getValue('pattern'))
      ->set('force', $form_state->getValue('force'))
      ->save();

    parent::submitForm($form, $form_state);
  }

  /**
   * @inheritDoc
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
    ...
    $example_order_number_formatted = $this->orderNumberFormatter->format($example_order_number, $padding, $pattern);
    ...
  }

}

Сервисы для конфигурационной формы подключаются также как и для обычной формы, с использованием методов __construct() и create().