Thursday 14 March 2019

Dependency Injection In Created Custom Block Programmatically Drupal 8

Dependency Injection in Custom Block

As in previous post, DI in Custom Form, this can also be used in creating custom block
Replace \Drupal::formBuilder() Global Service container with dependency injection from previously created block.


<?php

/**
 * @file
 * This file contains \Drupal\example\Plugin\Block\HelloBlock
 */

namespace Drupal\firstcustommodule\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Form\FormBuilderInterface;

/**
 * Provides a 'Hello' block.
 *
 * @Block(
 *   id = "hello_block",
 *   admin_label = @Translation("Hello Block")
 * )
 */
class HelloBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
 * The form builder
*
* @var \Drupal\Core\Form\FormBuilderInterface;
*/
protected $formBuilder;

/**
 *{@inheritdoc}
*/
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    // Instantiate this block class.
return new static(
  $configuration,
  $plugin_id,
  $plugin_definition,
  // Load the service required to construct this class.
  $container->get('form_builder')
);
  }
/**
* Class constructor
 */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $form_builder) {
$this->formBuilder = $form_builder;
  }
  /**
   * {@inheritdoc}
   */
  public function build() {
    return $this->formBuilder->getForm('Drupal\firstcustommodule\Form\CompanyAddForm');
  }
}

Explanation:
Block is plugin in Drupal 8. To implement dependency injection while creating blocks plugin, ContainerFactoryPluginInterface need to implement. Plugin manager checks if this block plugin is implementing ContainerFactoryPluginInterface then it provides create() and __construct()(provided by BlockBase) method which are necessary to dependency injection.

To use the create pattern by new plugin manager, your plugin need to implement the ContainerFactoryPluginInterface Interface.

In above code, ContainerFactoryPluginInterface is impelemted by class HelloBlock extending BlockBase

create() method Creates an instance of the plugin and having arguments are ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition. These arguments are available in method declaration in ContainerFactoryPluginInterface.
$container: The container to pull out services used in the plugin.
$configurationA configuration array containing information about the plugin instance.
$plugin_id: The plugin ID for the plugin instance.
$plugin_definition: The plugin implementation definition.

__construct(): must have arguments in same order as in create() method as shown below.

public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $form_builder)

Now in build method, "formBuilder" service is available and can be accessed with
$this->formBuilder.




















No comments:

Post a Comment