0

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!

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.