What Are Action Plugins In Drupal? Know Everthing Here 2024

What Are Action Plugins In Drupal? Know Everthing Here 2024

Explore the versatile landscape of Drupal’s Action Plugins in 2024, uncovering their role in extending functionality and promoting code reuse. From seamless integration to scalability, our guide delves into the intricacies of Action Plugins, providing a comprehensive understanding of how they enhance the development process within the dynamic Drupal framework. Let’s delve deeper into the world of Action Plugins and harness their power for an optimized web development experience.

Action Plugin Overview

Drupal’s Actions module is a core module that lets developers and site builders define actions and triggers to build automated workflows. A set of operations or tasks that can be carried out on a site are referred to as actions in Drupal. Sending an email, posting content, or updating a database record are a few examples of actions.

Advantages Of Action Plugin In Drupal

Action plugins in Drupal offer several advantages, enhancing the functionality and flexibility of the platform:

  • Extensibility: Action plugins provide a modular and extensible way to add custom actions to Drupal. Developers can create their own plugins to extend the functionality of the system without altering core code.
  • Reusability: Action plugins follow a reusable design pattern, allowing developers to use the same action in multiple contexts. This promotes code reuse and simplifies the development process.
  • Configurability: Action plugins are highly configurable, enabling site administrators to customize and configure actions through the Drupal administration interface. This flexibility simplifies the management of various actions without requiring extensive coding.
  • Consistency: Action plugins contribute to maintaining consistency in code and user interfaces. By adhering to Drupal’s plugin system, developers can ensure that their custom actions align with Drupal’s coding standards and best practices.
  • Integration with Rules: Action plugins seamlessly integrate with the Rules module in Drupal. This integration allows developers and site builders to create complex workflows and trigger actions based on specific conditions, enhancing the overall functionality of the website.
  • Scalability: As part of Drupal’s plugin system, action plugins contribute to the scalability of the platform. Developers can efficiently scale their applications by creating and utilizing plugins tailored to their specific needs.
  • Community Support: Action plugins benefit from the Drupal community’s collective expertise. The community actively contributes to the development and improvement of plugins, ensuring that they stay current, secure, and aligned with the latest Drupal updates.

Custom Actions Plugin

Activities If the action is configurable, extend Drupal\Core\Action\ActionBase or Drupal\Core\Action\ConfigurableActionBase and use the annotation class Drupal\Core\Annotation\Action.

Plugin Annotation contains the definition of the Action Plugin. There are three necessary keys for it:

/**

* Provides an Archive Node Action.

*

* @Action(

*   id = “ttn_archive_node”,

*   label = @Translation(“Archive Node”),

*   type = “node”,

*   category = @Translation(“Custom”)

* )

*/

id: The Action Plugin’s ID; label: The Action Plugin’s name; type: The Entity type that the Action Plugin falls under; – (Optional) Action Plugin Category

Archive Node Action

This is an easy action that doesn’t need to be configured. When it runs, the node’s alias is changed to /archive//. Additionally, it puts the word “Archive” in front of the title. Lastly, it turns off the promoted and sticky flags.

 

<?php

 

namespace Drupal\ttn\Plugin\Action;

 

use Drupal\Core\Action\ActionBase;

use Drupal\Core\Session\AccountInterface;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;

use Symfony\Component\DependencyInjection\ContainerInterface;

use Drupal\pathauto\PathautoState;

 

/**

 * Provides an Archive Node Action.

 *

 * @Action(

 *   id = “ttn_archive_node”,

 *   label = @Translation(“Archive Node”),

 *   type = “node”,

 *   category = @Translation(“Custom”)

 * )

 */

class ArchiveNode extends ActionBase implements ContainerFactoryPluginInterface {

 

  /**

   * The Messenger service.

   *

   * @var \Drupal\Core\Messenger\MessengerInterface

   */

  protected $messenger;

 

  /**

   * Logger service.

   *

   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface

   */

  protected $logger;

 

  /**

   * The path alias manager.

   *

   * @var \Drupal\path_alias\AliasManagerInterface

   */

  protected $aliasManager;

 

  /**

   * Language manager for retrieving the default Langcode.

   *

   * @var \Drupal\Core\Language\LanguageManagerInterface

   */

  protected $languageManager;

 

  /**

   * {@inheritdoc}

   */

  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {

    $instance = new static($configuration, $plugin_id, $plugin_definition);

    $instance->logger = $container->get(‘logger.factory’)->get(‘ttn’);

    $instance->messenger = $container->get(‘messenger’);

    $instance->aliasManager = $container->get(‘path_alias.manager’);

    $instance->languageManager = $container->get(‘language_manager’);

    return $instance;

  }

 

  /**

   * {@inheritdoc}

   */

  public function access($node, AccountInterface $account = NULL, $return_as_object = FALSE) {

    /** @var \Drupal\node\NodeInterface $node */

    $access = $node->access(‘update’, $account, TRUE)

      ->andIf($node->title->access(‘edit’, $account, TRUE));

    return $return_as_object ? $access : $access->isAllowed();

  }

 

   /**

   * {@inheritdoc}

   */

  public function execute($node = NULL) {

 

    /** @var \Drupal\node\NodeInterface $node */

 

    $language = $this->languageManager->getCurrentLanguage()->getId();

 

    $old_alias = $this->aliasManager->getAliasByPath(‘/node/’ . $node->id(), $language);

 

    $title = $node->getTitle();

    $date = $node->created->value;

    $year = date(‘Y’, $date);

    $new_title = $this->t(‘[Archive] | @title’, [‘@title’ => $title]);

    $node->setTitle($new_title);

    $node->setSticky(FALSE);

    $node->setPromoted(FALSE);

 

    $new_alias = ‘/archive/’ . $year . $old_alias;

    $node->set(“path”, [

      ‘alias’ => $new_alias,

      ‘langcode’ => $language,

      ‘pathauto’ => PathautoState::SKIP,

    ]);

    $node->save();

    $message = $this->t(‘Node with NID : @id Archived.’, [‘@id’ => $node->id()]);

    $this->logger->notice($message);

    $this->messenger->addMessage($message);

  }

 

}

 

You must add system.action.<plugin_id>.yml, which is located in config/install, in order to obtain Action Plugin Discoverable.

The following illustrates the.yml file’s structure:

 

langcode: en

status: true

id: ttn_archive_node

label: ‘Archive Node’

type: node

plugin: ttn_archive_node

 

The /admin/content page displays the created Action Plugin.

Custom Actions Existing Enable Module

that is kept in the install/config folder. Create a hook_update_N() file and use it.

<?php 

// Install config. 

function ttn_update_9501(&$sandbox){ 

  $config_installer = \Drupal::service(‘config.installer’); 

  $config_installer->installDefaultConfig(‘module’, ‘ttn’);

}

 

Run update.php

Conclusion

Drupal Action Plugins are an effective tool that give developers an extensible and adaptable way to improve functionality and optimize workflows. Their adaptability and smooth integration add to the Drupal ecosystem’s dynamic and effective web development experience.

We hope that this tutorial has helped you with Drupal Core with Composer. Additionally, Appic Softwares is a company you should check out if you’re wanting to hire Drupal developers on a devoted basis.

You can employ our team of knowledgeable Drupal developers to handle your software.

So, what are you waiting for?

Hire them now!

Get Free Consultation Now!


    Contact Us

    Consult us today to develop your application.

      Get in touch with us


      Skype Whatsapp Gmail Phone