How to use dependency injection for Plugins/Blocks [\Drupal::formbuilder() and \Drupal::config()]? I cannot get my head around this dependency injection theory. How are we supposed to get the below methods aligned with dependency injection? 

\Drupal::routeMatch()-&gt;getParameter('node');
\Drupal::request();
\Drupal::formBuilder()-&gt;getForm('Drupal\enquirycart\Form\EnquiryForm');
\Drupal::config()


Here is a class that I need to fix this warning message when viewed in 
https://pareview.sh/


  "\Drupal calls should be avoided in classes, use | | dependency
  injection instead"


    class EnquirycartBlock extends BlockBase implements BlockPluginInterface {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    $default_config = \Drupal::config('enquirycart.settings');

    return [
      'label_display' =&gt; FALSE,
      'enquiry_email' =&gt; $default_config-&gt;get('enquirycart.email'),
    ];

  }

  /**
   * {@inheritdoc}
   */
  public function build() {

    $builtForm = \Drupal::formBuilder()-&gt;getForm('Drupal\enquirycart\Form\EnquirycartButtonForm');
    $renderArray['form'] = $builtForm;

    return $renderArray;

  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form_new = parent::blockForm($form, $form_state);

    return $form_new;

  }
For future reference, from the link by @Kevin:


  The key to making plugins use dependency injection is to implement the
  ContainerFactoryPluginInterface. When plugins are created, the code
  first checks if the plugin implements this interface. If it does, it
  uses the create() and __construct() pattern, if not, it uses just the
  __construct() pattern. - From Acquia's Lesson 11.4 - Dependency injection and plugins


The example code below is written by Mart Matsoo from Chromatic. Please check the full article here: https://chromatichq.com/blog/dependency-injection-drupal-8-plugins. As @Kevin already mentioned in the comments it is a great article about Depencency Injection in Plugins.

Dependency Injection example in a block

Note the injection of the 'current_user' service via $container-&gt;get('current_user') in the create() method. You can pass extra services there.

&lt;?php

namespace Drupal\heytaco\Plugin\Block;

use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a Hey Taco Results Block
 *
 * @Block(
 *   id = "heytaco_block",
 *   admin_label = @Translation("HeyTaco! Leaderboard"),
 * )
 */
class HeyTacoBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * @var $account \Drupal\Core\Session\AccountProxyInterface
   */
  protected $account;

  /**
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   * @param array $configuration
   * @param string $plugin_id
   * @param mixed $plugin_definition
   *
   * @return static
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container-&gt;get('current_user')
    );
  }

  /**
   * @param array $configuration
   * @param string $plugin_id
   * @param mixed $plugin_definition
   * @param \Drupal\Core\Session\AccountProxyInterface $account
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $account) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this-&gt;account = $account;
  }


Dependency Injection example in a controller

The same concept applies here, pass extra services via $container-&gt;get() in the create() method.

&lt;?php

namespace Drupal\heytaco\Controller;

use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Controller\ControllerBase;

/**
 * Controller routines for HeyTaco! block.
 */
class HeyTacoController extends ControllerBase {

  protected $account;

  public function __construct(AccountInterface $account) {
    $this-&gt;account = $account;
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container-&gt;get('current_user')
    );
  }