I became impatient waiting for a comprehensive example of creating a Content Entity with bundles, so I decided to refactor the Drupal 8 Node module as a custom Content Entity. However, I'll use the Node module as reference for my questions pertaining to Actions and Action plugins.

My main question is, how and when are Action plugins called?

I'll use StickyNode as an example.

The StickyNode plugin is defined as such:

<?php
/**
 * @file
 * Contains \Drupal\node\Plugin\Action\StickyNode.
 */

namespace Drupal\node\Plugin\Action;

use Drupal\Core\Action\ActionBase;

/**
 * Makes a node sticky.
 *
 * @Action(
 *   id = "node_make_sticky_action",
 *   label = @Translation("Make selected content sticky"),
 *   type = "node"
 * )
 */
class StickyNode extends ActionBase {

  /**
   * {@inheritdoc}
   */
  public function execute($entity = NULL) {
    $entity->status = NODE_PUBLISHED;
    $entity->sticky = NODE_STICKY;
    $entity->save();
  }

}
?>

And in node/config/install/system.action.node_make_sticky_action.yml, node_make_sticky_action is defined:

id: node_make_sticky_action
label: 'Make content sticky'
status: true
langcode: en
type: node
plugin: node_make_sticky_action
dependencies:
  module:
    - node

Some clarification would be greatly appreciated.

Comments

sajalsoni’s picture

Hello,

I'm not sure, if you've already got your answer. But, I'll comment this one if it helps others. Core "Actions" module provides different actions which you can perform on certain entities. For ex, "Make a node Sticky" action is used to make node as "Sticky".

Go to "Content" on admin side, and there is a dropdown "with selection". It lists all the actions available for "node" entity. Similarly, if you go to "People", you will see the similar dropdown listing all the actions related to "user" entity.

You can also define your custom action by implementing the custom module.

Hope that helps.

Sajal

PieterWintmolders’s picture

So as an example, this should work? Because it doesn't.. and I can't figure out why not
file modules/example/src/Plugin/Action/ExampleNode.php

<?php
/**
 * @file
 * Contains \Drupal\example\Plugin\Action\ExampleNode.
 */

namespace Drupal\node\Plugin\Action;

use Drupal\Core\Action\ActionBase;

/**
 * Makes a node example.
 *
 * @Action(
 *   id = "node_example_action",
 *   label = @Translation("Make selected content example"),
 *   type = "node"
 * )
 */
class ExampleNode extends ActionBase {

  /**
   * {@inheritdoc}
   */
  public function execute($entity = NULL) {
   /* example stuff */
    $entity->save();
  }

}
?>

and in modules/example/config/example.action.node_example_action.yml

id: node_example_action
label: 'Make content example'
status: true
langcode: en
type: node
plugin: node_example_action
dependencies:
  module:
    - node
jordanpagewhite’s picture

Change 'modules/example/config/example.action.node_example_action.yml' to 'modules/examples/config/install/system.action.node_example_action.yml'

cambraca’s picture

This just happened to me, I was trying to add a new Action plugin to an already installed module on the site, what you need to make sure is that the configuration in the .yml file gets into the DB. I solved this by adding the following update function (and running update.php):

function example_update_8001() {
  $config_installer = \Drupal::service('config.installer');
  $config_installer->installDefaultConfig('module', 'example');
}

Now, since my module didn't have any extra config, this was fine, but I'm not sure what will happen otherwise. You can always add the configuration key by key in the update function, I guess.

nikathone’s picture

With devel module enabled you can navigate to /devel/php then run paste the code and run execute

$config_installer = \Drupal::service('config.installer');
$config_installer->installDefaultConfig('module', 'mymodule_name');