9.15.4. Dependencies Injection in ConfigFormBase settings form.
In previous articles, we discussed what Services, Dependency Injection (DI) are and how to use them in your controller, block and form:
9.15. Services and Dependency Injection.
9.15.1. Dependency Injection in Controller
9.15.2. Dependency Injection in BlockBase
9.15.3. Dependencies Injection in BaseForm
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 configuration form class inheriting ConfigFormBase. You can download Commerce Order Number module from drupal.org:
https://www.drupal.org/project/commerce_order_number
/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);
...
}
}
Services for a configuration form are connected in the same way as for a regular form, using the __construct() and create() methods.