Score:0

Custom Simple Action after updating a node

cn flag

I am trying to create a simple way to execute certain actions on the database every time when I update a node.

I have been looking at Drupal's Action module, however I can't find any good manuals explaining how to use it. The ECA module works as a mechanism to launch my Action but I can't execute my Action and I don't know how to proceed.

I am currently creating my own module that will contain my actions. The following class is the class of my action.

<?php

namespace Drupal\my_custom_actions\Plugin\Action;

use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;

/**
 * create custom action
 *
 * @Action(
 *   id = "node_action",
 *   label = @Translation("DataBase Action"),
 *   type = "node"
 * )
 */
class DBAction extends ActionBase {

    /**
     * {@inheritdoc}
     */
    public function execute($node = NULL) {
        if ($node) {
            // TODO: procedure to execute in database
            \Drupal::messenger()->addStatus('The execution is OK, and we have the node');
        }
    }

    /**
     * {@inheritdoc}
     */
    public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
        /** @var \Drupal\node\NodeInterface $object */
        // TODO: write here your permissions
        $result = $object->access('update', $account, TRUE);
        return $return_as_object ? $result : $result->isAllowed();
    }

}

The config/schema/module_test.schema.yml file like this :

action.configuration.module_test:
  type: node_action
  label: 'DataBase Action'

config/install/system.action.node_action.yml

langcode: en
status: true
dependencies:
  module:
    - node
id: node_action
label: 'Export Content'
type: node
plugin: node_action
configuration: {  }

However, after update one node, two things happen:

  • The variable $node of the execute method always comes in as null. Not entering to the if.
  • The variable $object of the access method always comes as null. Throwing an error in $result = $object->access('update', $account, TRUE);

I don't know if the actions work only for Bulk Operations. But my goal is to only execute an action every time I update a node. Am I on the right track using Actions or not? Drupal documentation doesn't seem very intuitive to me.

Thanks!

Kevin avatar
in flag
Does this answer your question? [How to create my own custom simple action in D8?](https://drupal.stackexchange.com/questions/230742/how-to-create-my-own-custom-simple-action-in-d8)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.