You are here

class ExampleWithDependencyInjection in Extra Field 8.2

Same name and namespace in other branches
  1. 8 modules/extra_field_example/src/Plugin/ExtraField/Display/ExampleWithDependencyInjection.php \Drupal\extra_field_example\Plugin\ExtraField\Display\ExampleWithDependencyInjection

Example Extra field Display.

Plugin annotation


@ExtraFieldDisplay(
  id = "article_only",
  label = @Translation("Dependency Injection example"),
  description = @Translation("An extra field that uses dependency injection."),
  bundles = {
    "node.article",
  }
)

Hierarchy

Expanded class hierarchy of ExampleWithDependencyInjection

File

modules/extra_field_example/src/Plugin/ExtraField/Display/ExampleWithDependencyInjection.php, line 24

Namespace

Drupal\extra_field_example\Plugin\ExtraField\Display
View source
class ExampleWithDependencyInjection extends ExtraFieldDisplayBase implements ContainerFactoryPluginInterface {
  use StringTranslationTrait;

  /**
   * The current request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $currentRequest;

  /**
   * Constructs a ExtraFieldDisplayFormattedBase object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, RequestStack $request_stack) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->currentRequest = $request_stack
      ->getCurrentRequest();
  }

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

  /**
   * {@inheritdoc}
   */
  public function view(ContentEntityInterface $entity) {

    // Some output to demonstrate the injected service.
    $scheme = $this->currentRequest
      ->getScheme();
    return [
      '#markup' => $this
        ->t('Request scheme: @scheme', [
        '@scheme' => $scheme,
      ]),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExampleWithDependencyInjection::$currentRequest protected property The current request object.
ExampleWithDependencyInjection::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
ExampleWithDependencyInjection::view public function Builds a renderable array for the field. Overrides ExtraFieldDisplayInterface::view
ExampleWithDependencyInjection::__construct public function Constructs a ExtraFieldDisplayFormattedBase object. Overrides PluginBase::__construct
ExtraFieldDisplayBase::$entity protected property The field's parent entity.
ExtraFieldDisplayBase::$entityViewDisplay protected property The entity view display.
ExtraFieldDisplayBase::$viewMode protected property The view mode the entity is rendered in.
ExtraFieldDisplayBase::getEntity public function Returns the field's parent entity. Overrides ExtraFieldDisplayInterface::getEntity
ExtraFieldDisplayBase::getEntityViewDisplay public function Returns the entity view display object of the field's host entity. Overrides ExtraFieldDisplayInterface::getEntityViewDisplay
ExtraFieldDisplayBase::getViewMode public function Returns the entity view mode object of the field's host entity. Overrides ExtraFieldDisplayInterface::getViewMode
ExtraFieldDisplayBase::setEntity public function Stores the field's parent entity. Overrides ExtraFieldDisplayInterface::setEntity
ExtraFieldDisplayBase::setEntityViewDisplay public function Stores the entity view display. Overrides ExtraFieldDisplayInterface::setEntityViewDisplay
ExtraFieldDisplayBase::setViewMode public function Stores the entity view mode. Overrides ExtraFieldDisplayInterface::setViewMode
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.