{"id":59518,"date":"2023-12-22T10:15:00","date_gmt":"2023-12-22T04:45:00","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=59518"},"modified":"2024-01-02T17:33:07","modified_gmt":"2024-01-02T12:03:07","slug":"actions-plugin-in-drupal-10","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/actions-plugin-in-drupal-10\/","title":{"rendered":"Actions Plugin in Drupal 10"},"content":{"rendered":"<h2>Action Plugin Overview<\/h2>\n<p>The Actions module is a core module in Drupal that allows site builders and developers to create automated workflows by defining actions and triggers. In Drupal, actions refer to a set of tasks or operations that can be performed on a site. For example, an action can be sending an email, publishing content, or updating a database record.<\/p>\n<h2>Custom Actions Plugin<\/h2>\n<p>Here, A Drupal action is a functionality that performs a specific action when executed. For example, Archive Node or Make Content Sticky.<\/p>\n<p>Actions use the annotation class <strong>Drupal\\Core\\Annotation\\Action<\/strong>, and extend <strong>Drupal\\Core\\Action\\ActionBase<\/strong> or <strong>Drupal\\Core\\Action\\ConfigurableActionBase<\/strong> (if the action is configurable.)<br \/>\nAction Plugin definition is defined in Plugin Annotation. It has 3 required keys-<\/p>\n<pre>\/**\r\n* Provides an Archive Node Action.\r\n*\r\n* @Action(\r\n*\u00a0 \u00a0id = \"ttn_archive_node\",\r\n*\u00a0 \u00a0label = @Translation(\"Archive Node\"),\r\n*\u00a0 \u00a0type = \"node\",\r\n*\u00a0 \u00a0category = @Translation(\"Custom\")\r\n* )\r\n*\/\r\n<\/pre>\n<p>id \u2013 ID of the Action Plugin<br \/>\nlabel \u2013 Name of the Action Plugin<br \/>\ntype \u2013 Entity type to which the Action Plugin belongs to<br \/>\ncategory \u2013 (optional) Category of the Action Plugin<\/p>\n<h2>Archive Node Action<\/h2>\n<p>This is a simple action which requires no configuration. When it is run, it changes the alias of the node to \/archive\/\/. It also sets the title to have the word `Archive` in the front of it. Finally, it disables the sticky and promoted flags.<\/p>\n<pre>&lt;?php\r\n\r\nnamespace Drupal\\ttn\\Plugin\\Action;\r\n\r\nuse Drupal\\Core\\Action\\ActionBase;\r\nuse Drupal\\Core\\Session\\AccountInterface;\r\nuse Drupal\\Core\\Plugin\\ContainerFactoryPluginInterface;\r\nuse Symfony\\Component\\DependencyInjection\\ContainerInterface;\r\nuse Drupal\\pathauto\\PathautoState;\r\n\r\n\/**\r\n * Provides an Archive Node Action.\r\n *\r\n * @Action(\r\n *   id = \"ttn_archive_node\",\r\n *   label = @Translation(\"Archive Node\"),\r\n *   type = \"node\",\r\n *   category = @Translation(\"Custom\")\r\n * )\r\n *\/\r\nclass ArchiveNode extends ActionBase implements ContainerFactoryPluginInterface {\r\n\r\n  \/**\r\n   * The Messenger service.\r\n   *\r\n   * @var \\Drupal\\Core\\Messenger\\MessengerInterface\r\n   *\/\r\n  protected $messenger;\r\n\r\n  \/**\r\n   * Logger service.\r\n   *\r\n   * @var \\Drupal\\Core\\Logger\\LoggerChannelFactoryInterface\r\n   *\/\r\n  protected $logger;\r\n\r\n  \/**\r\n   * The path alias manager.\r\n   *\r\n   * @var \\Drupal\\path_alias\\AliasManagerInterface\r\n   *\/\r\n  protected $aliasManager;\r\n\r\n  \/**\r\n   * Language manager for retrieving the default Langcode.\r\n   *\r\n   * @var \\Drupal\\Core\\Language\\LanguageManagerInterface\r\n   *\/\r\n  protected $languageManager;\r\n\r\n  \/**\r\n   * {@inheritdoc}\r\n   *\/\r\n  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {\r\n    $instance = new static($configuration, $plugin_id, $plugin_definition);\r\n    $instance-&gt;logger = $container-&gt;get('logger.factory')-&gt;get('ttn');\r\n    $instance-&gt;messenger = $container-&gt;get('messenger');\r\n    $instance-&gt;aliasManager = $container-&gt;get('path_alias.manager');\r\n    $instance-&gt;languageManager = $container-&gt;get('language_manager');\r\n    return $instance;\r\n  }\r\n\r\n  \/**\r\n   * {@inheritdoc}\r\n   *\/\r\n  public function access($node, AccountInterface $account = NULL, $return_as_object = FALSE) {\r\n    \/** @var \\Drupal\\node\\NodeInterface $node *\/\r\n    $access = $node-&gt;access('update', $account, TRUE)\r\n      -&gt;andIf($node-&gt;title-&gt;access('edit', $account, TRUE));\r\n    return $return_as_object ? $access : $access-&gt;isAllowed();\r\n  }\r\n\r\n   \/**\r\n   * {@inheritdoc}\r\n   *\/\r\n  public function execute($node = NULL) {\r\n\r\n    \/** @var \\Drupal\\node\\NodeInterface $node *\/\r\n\r\n    $language = $this-&gt;languageManager-&gt;getCurrentLanguage()-&gt;getId();\r\n\r\n    $old_alias = $this-&gt;aliasManager-&gt;getAliasByPath('\/node\/' . $node-&gt;id(), $language);\r\n\r\n    $title = $node-&gt;getTitle();\r\n    $date = $node-&gt;created-&gt;value;\r\n    $year = date('Y', $date);\r\n    $new_title = $this-&gt;t('[Archive] | @title', ['@title' =&gt; $title]);\r\n    $node-&gt;setTitle($new_title);\r\n    $node-&gt;setSticky(FALSE);\r\n    $node-&gt;setPromoted(FALSE);\r\n\r\n    $new_alias = '\/archive\/' . $year . $old_alias;\r\n    $node-&gt;set(\"path\", [\r\n      'alias' =&gt; $new_alias,\r\n      'langcode' =&gt; $language,\r\n      'pathauto' =&gt; PathautoState::SKIP,\r\n    ]);\r\n    $node-&gt;save();\r\n    $message = $this-&gt;t('Node with NID : @id Archived.', ['@id' =&gt; $node-&gt;id()]);\r\n    $this-&gt;logger-&gt;notice($message);\r\n    $this-&gt;messenger-&gt;addMessage($message);\r\n  }\r\n\r\n}\r\n<\/pre>\n<p>To get Action Plugin Discoverable, you need to add <strong>system.action.&lt;plugin_id&gt;.yml,<\/strong> which is placed in <strong>config\/install.<br \/>\n<\/strong><br \/>\nThe structure of the .<strong>yml<\/strong> file is shown below:<\/p>\n<pre>langcode: en\r\nstatus: true\r\nid: ttn_archive_node\r\nlabel: 'Archive Node'\r\ntype: node\r\nplugin: ttn_archive_node<\/pre>\n<p>Created Action Plugin can be viewed on the <strong>\/admin\/content page<\/strong>.<\/p>\n<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/238564950-40d4e78a-b0e8-40e1-bd7d-58998fab3168.png\" \/><\/p>\n<h2>Custom Actions Existing Enable Module<\/h2>\n<p>Which is placed in config\/install. Create .install file and use hook_update_N().<\/p>\n<pre>&lt;?php \r\n\/\/ Install config. \r\nfunction ttn_update_9501(&amp;$sandbox){ \r\n  $config_installer = \\Drupal::service('config.installer'); \r\n  $config_installer-&gt;installDefaultConfig('module', 'ttn');\r\n}\r\n\r\nRun update.php\r\n\r\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>In the Actions module, you can create a customized workflow triggered by a specific event. The Actions module provides a user-friendly interface for defining and managing actions. For example, when a user submits a form on your website, you can trigger an action to send an email notification to the site administrator.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Action Plugin Overview The Actions module is a core module in Drupal that allows site builders and developers to create automated workflows by defining actions and triggers. In Drupal, actions refer to a set of tasks or operations that can be performed on a site. For example, an action can be sending an email, publishing [&hellip;]<\/p>\n","protected":false},"author":1700,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":216},"categories":[3602],"tags":[4862],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59518"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1700"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=59518"}],"version-history":[{"count":9,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59518\/revisions"}],"predecessor-version":[{"id":59637,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/59518\/revisions\/59637"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=59518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=59518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=59518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}