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;
}
}
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')
);
}
}
<?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'),
];
}
}