Plugin API http://lobsterr.me/ en How to use dependency injection for Plugins in Drupal 9? http://lobsterr.me/post/how-use-dependency-injection-plugins-drupal-9 <span class="field field--name-title field--type-string field--label-hidden">How to use dependency injection for Plugins in Drupal 9?</span> <div class="field field--name-f-intro field--type-string-long field--label-hidden field__item">Services are very powerful way to make our code reusable. How can we pass service to our plugin in Drupal 9?</div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/user/1" typeof="schema:Person" property="schema:name" datatype="">LOBsTerr</span></span> <span class="field field--name-created field--type-created field--label-hidden">01/22/2021</span> <div class="field field--name-f-content field--type-entity-reference-revisions field--label-hidden field__items"> <div class="field__item"> <div class="paragraph paragraph--type--l-text paragraph--view-mode--default"> <div class="clearfix text-formatted field field--name-f-text field--type-text-long field--label-hidden field__item"><p>For this example we take code of simple plugin EntityLink. It doesn't actually it could be any plugin</p> <pre> <code class="language-php">use Drupal\views\Plugin\views\field\EntityLink; use Drupal\views\ResultRow; /** * Field handler to present a link to approval. * * @ingroup views_field_handlers * * @ViewsField("approve_link") */ class ApproveLInk extends EntityLink { /** * {@inheritdoc} */ protected function getEntityLinkTemplate() { return 'approve'; } /** * {@inheritdoc} */ protected function renderLink(ResultRow $row) { $this-&gt;options['alter']['query'] = $this-&gt;getDestinationArray(); return parent::renderLink($row); } /** * {@inheritdoc} */ protected function getDefaultLabel() { return $this-&gt;t('Approve'); } }</code></pre> <p>We don't call constructor for our plugin. In order to pass our service. Let's take <strong>current_user </strong>service.</p> <p>First we need implement </p> <pre> <code class="language-php">class ApproveLInk extends EntityLink implements ContainerFactoryPluginInterface {</code></pre> <p>Add create method, where we pass our service</p> <pre> <code class="language-php">/** * @param \Symfony\Component\DependencyInjection\ContainerInterface $container * @param array $configuration * @param string $plugin_id * @param mixed $plugin_definition * * @return static */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container-&gt;get('current_user') ); }</code></pre> <p>After that add parameters to our constructor</p> <pre> <code class="language-php">/** * @var AccountInterface $account */ protected $account; /** * @param array $configuration * @param string $plugin_id * @param mixed $plugin_definition * @param \Drupal\Core\Session\AccountInterface $account */ public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountInterface $account) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this-&gt;account = $account; }</code></pre> <p> </p> </div> </div> </div> </div> <div class="field field--name-f-tags field--type-entity-reference field--label-hidden field__items"> <div class="field__item btn btn-secondary"><a href="/tags/plugin-api" hreflang="en">Plugin API</a></div> <div class="field__item btn btn-secondary"><a href="/tags/plugin" hreflang="en">Plugin</a></div> <div class="field__item btn btn-secondary"><a href="/tags/drupal-9" hreflang="en">Drupal 9</a></div> </div> <section class="field field--name-f-comments field--type-comment field--label-above comment-wrapper"> </section> Thu, 21 Jan 2021 23:23:14 +0000 LOBsTerr 27 at http://lobsterr.me