Skip to content

Instantly share code, notes, and snippets.

@fredysan
Forked from ozin7/Controller.php
Created February 1, 2024 21:59
Show Gist options
  • Save fredysan/0b56f4ace00c66eccd75debc518142c5 to your computer and use it in GitHub Desktop.
Save fredysan/0b56f4ace00c66eccd75debc518142c5 to your computer and use it in GitHub Desktop.
Drupal 8: Dependency injection in Service, Controller, Plugin, Deriver.
<?php
/**
* Controller. Implements ContainerInjectionInterface
*/
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 {
/**
* CompanyController constructor.
*/
public function __construct(private CompanyService $companyManager) {}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('gurei.company_manager')
);
}
}

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;
  }

}
<?php
namespace Drupal\gurei\Plugin\Deriver;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SearchIndexStatusPageDeriver extends DeriverBase implements ContainerDeriverInterface {
/**
* @inheritDoc
*/
public static function create(
ContainerInterface $container,
$base_plugin_id
) {
// TODO: Implement create() method.
}
}
<?php
class SponsorChildAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
/**
* SponsorChildAccessControlHandler constructor.
*/
public function __construct(
EntityTypeInterface $entity_type,
private EntityTypeManagerInterface $entityTypeManager
) {
parent::__construct($entity_type);
}
/**
* {@inheritDoc}
*/
public static function createInstance(
ContainerInterface $container,
EntityTypeInterface $entity_type
) {
return new static(
$entity_type,
$container->get('entity_type.manager')
);
}
}
<?php
class PriceListItemImportForm extends FormBase {
public function __construct(private EntityFieldManagerInterface $entityFieldManager, private EntityTypeManagerInterface $entityTypeManager) {
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_field.manager'),
$container->get('entity_type.manager')
);
}
}
<?php
/**
* Plugin. Implements ContainerFactoryPluginInterface
*/
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 {
/**
* CompanyBlock constructor.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, private CompanyService $companyManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@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