ExampleWithDependencyInjection.php in Extra Field 8.2
File
modules/extra_field_example/src/Plugin/ExtraField/Display/ExampleWithDependencyInjection.php
View source
<?php
namespace Drupal\extra_field_example\Plugin\ExtraField\Display;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\extra_field\Plugin\ExtraFieldDisplayBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class ExampleWithDependencyInjection extends ExtraFieldDisplayBase implements ContainerFactoryPluginInterface {
use StringTranslationTrait;
protected $currentRequest;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RequestStack $request_stack) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentRequest = $request_stack
->getCurrentRequest();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('request_stack'));
}
public function view(ContentEntityInterface $entity) {
$scheme = $this->currentRequest
->getScheme();
return [
'#markup' => $this
->t('Request scheme: @scheme', [
'@scheme' => $scheme,
]),
];
}
}