Skip to content

Instantly share code, notes, and snippets.

@mcortes19
Forked from ozin7/Controller.php
Created August 6, 2020 04:02
Show Gist options
  • Save mcortes19/93c1570290c4c818f056492a9ba823ec to your computer and use it in GitHub Desktop.
Save mcortes19/93c1570290c4c818f056492a9ba823ec to your computer and use it in GitHub Desktop.
Drupal 8: Dependency injection in Service, Controller, Plugin.

Service

gurei.services.yml

services:
  gurei.company_manager:
    class: Drupal\gurei\Service\CompanyService
    arguments:
      - '@entity_type.manager'

CompanyService.php

<?php

namespace Drupal\gurei\Service;

use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
 * Class CompanyService.
 */
class CompanyService {

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * CompanyService constructor.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

}

Controller. Implements ContainerInjectionInterface

CompanyController.php

<?php

namespace Drupal\gurei\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\gurei\Service\CompanyService;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class CompanyController.
 */
class CompanyController extends ControllerBase implements ContainerInjectionInterface {

  /**
   * Company manager.
   *
   * @var \Drupal\gurei\Service\CompanyService
   */
  protected $companyManager;

  /**
   * CompanyController constructor.
   */
  public function __construct(CompanyService $company_manager) {
    $this->companyManager = $company_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('gurei.company_manager')
    );
  }
}

Plugin. Implements ContainerFactoryPluginInterface

<?php

namespace Drupal\drupal\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\gurei\Service\CompanyService;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines a fallback plugin for missing block plugins.
 *
 * @Block(
 *   id = "company_block",
 *   admin_label = @Translation("Company block"),
 *   category = @Translation("Block"),
 * )
 */
class CompanyBlock extends BlockBase implements ContainerFactoryPluginInterface {
  /**
   * Company manager.
   *
   * @var \Drupal\gurei\Service\CompanyService
   */
  protected $companyManager;

  /**
   * CompanyBlock constructor.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, CompanyService $company_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->companyManager = $company_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('gurei.company_manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    return [
      '#markup' => $this->t('Company block'),
    ];
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment