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