Plugin 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 How to add local tasks dynamically in Drupal 8? http://lobsterr.me/post/how-add-local-tasks-dynamically-drupal-8 <span class="field field--name-title field--type-string field--label-hidden">How to add local tasks dynamically in Drupal 8?</span> <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">09/16/2019</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>In some cases we want to generate local task based on some data. In our example we want to provide custom settings per node type for it we will provide local tasks based on node types. </p> <p>As usual full example you can find here: <a href="https://github.com/LOBsTerr/drupal-modules-examples">https://github.com/LOBsTerr/drupal-modules-examples</a></p> <p>First we need to define deriver plugin, which will expose the local tasks dynamically for us.</p> <p>We will setup a route, which will accept a parameter "type" and we will just print it in order to see that it is a different page.</p> <pre> <code class="language-php">local_tasks.dynamic_tasks: path: '/dynamic-tasks/{type}' defaults: _controller: '\Drupal\local_tasks\Controller\LocalTasksController::dynamicTasks' _title: 'Dynamic tasks' type: '' requirements: _permission: 'access content'</code></pre> <p>and controller method</p> <pre> <code class="language-php">public function dynamicTasks($type = NULL) { return [ '#markup' =&gt; $this-&gt;t('This is an example : @type', [ '@type' =&gt; $type ]), ]; }</code></pre> <p>Then we add in your file <strong>[your_module_name].links.task.yml </strong>(in our case local_tasks.links.task.yml), we set a deriver class</p> <pre> <code class="language-yaml"># Dynamically add local tasks. local_tasks.dynamic_tasks: route_name: 'local_tasks.dynamic_tasks' title: 'Dynamic tasks' base_route: 'local_tasks.dynamic_tasks' deriver: Drupal\local_tasks\Plugin\Derivative\NodeTypeLocalTask # We set derive class, which will provide local tasks dynamically.</code></pre> <p> </p> <p>Now we can add a class Drupal\local_tasks\Plugin\Derivative\NodeTypeLocalTask.php</p> <pre> <code class="language-php">&lt;?php namespace Drupal\local_tasks\Plugin\Derivative; use Drupal\Component\Plugin\Derivative\DeriverBase; /** * Provides dynamic tabs based on node types. */ class NodeTypeLocalTask extends DeriverBase { /** * {@inheritdoc} */ public function getDerivativeDefinitions($base_plugin_definition) { // Get node types. $node_types = \Drupal::entityTypeManager() -&gt;getStorage('node_type') -&gt;loadMultiple(); foreach ($node_types as $node_type_name =&gt; $node_type) { $this-&gt;derivatives[$node_type_name] = $base_plugin_definition; $this-&gt;derivatives[$node_type_name]['title'] = $node_type-&gt;label(); $this-&gt;derivatives[$node_type_name]['route_parameters'] = ['type' =&gt; $node_type_name]; } return $this-&gt;derivatives; } } </code></pre> <p> </p> <p>As you can see above, we get list of node types and pass title and route parameters. The route name comes from <strong>local_tasks.links.task.yml</strong></p> <p>The only thin left is to clean the cache and open <strong>/dynamic-tasks/</strong>, you should see the list of node types as tabs.</p> <p>Like this you can easily add local tasks dynamically on your custom page applying more complicated logic in Drupal.</p> <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/menu" hreflang="en">Menu</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> <div class="field field--name-f-related-items field--type-entity-reference field--label-above list-group list-group-flush"> <div class="field__label">Related items</div> <ul class="field field--name-f-related-items field--type-entity-reference field--label-above list-group list-group-flush field__items"> <li class="field__item list-group-item"><a href="/post/how-add-local-task-tab-drupal" hreflang="en">How to add a local task (tab) in Drupal?</a></li> <li class="field__item list-group-item"><a href="/post/how-add-local-task-custom-page-drupal" hreflang="en">How to add a local task to custom page in Drupal?</a></li> <li class="field__item list-group-item"><a href="/post/how-hide-local-task-tab-drupal" hreflang="en">How to hide a local task (tab) in Drupal?</a></li> </ul> </div> <section class="field field--name-f-comments field--type-comment field--label-above comment-wrapper"> </section> Mon, 16 Sep 2019 12:04:07 +0000 LOBsTerr 16 at http://lobsterr.me