1

I want to create simple actions that will change the field_support_status field value to 1 or 0. I enabled the "Actions" module and created a custom module with my actions, but after the module is installed the actions do not appear.

In my module, I created files with the following structure:

/src/Plugins/Action/SupportStatusYes.php

<?php
namespace Drupal\support_action\Plugin\Action;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;
/**
 * Change support status on yes.
 *
 * @Action(
 *   id = "support_status_yes",
 *   label = @Translation("Change support status on yes"),
 *   type = "node"
 * )
 */
class SupportStatusYes extends ActionBase {

  /**
   * {@inheritdoc}
   */
  public function execute($entity = NULL) {
    /** @var \Drupal\node\NodeInterface $node */
    if ($entity->hasField('field_support_status')) {
      $entity->field_support_status->value = 1;
      $entity->save();
    }

  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    /** @var \Drupal\node\NodeInterface $object */
    $result = $object->access('update', $account, TRUE)
      ->andIf($object->field_support_status->access('edit', $account, TRUE));

    return $return_as_object ? $result : $result->isAllowed();
  }

}

/src/Plugins/Action/SupportStatusNo.php

<?php
namespace Drupal\support_action\Plugin\Action;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;
/**
 * Change support status on no.
 *
 * @Action(
 *   id = "support_status_no",
 *   label = @Translation("Change support status on no"),
 *   type = "node"
 * )
 */
class SupportStatusNo extends ActionBase {

  /**
   * {@inheritdoc}
   */
  public function execute($entity = NULL) {
    /** @var \Drupal\node\NodeInterface $node */
    if ($entity->hasField('field_support_status')) {
      $entity->field_support_status->value = 0;
      $entity->save();
    }

  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    /** @var \Drupal\node\NodeInterface $object */
    $result = $object->access('update', $account, TRUE)
      ->andIf($object->field_support_status->access('edit', $account, TRUE));

    return $return_as_object ? $result : $result->isAllowed();
  }

}

/config/install/system.action.support_status_no.yml

langcode: en
status: true
dependencies:
  module:
    - node
id: support_status_no
label: 'Change support status on no'
type: node
plugin: support_status_no
configuration: {  }

/config/install/system.action.support_status_yes.yml

langcode: en
status: true
dependencies:
  module:
    - node
id: support_status_yes
label: 'Change support status on yes'
type: node
plugin: support_status_yes
configuration: {  }

/config/schema/support_action.schema.yml

action.configuration.support_status_yes:
  type: action_configuration_default
  label: 'Change support status on yes'

action.configuration.support_status_no:
  type: action_configuration_default
  label: 'Change support status on no'

Tell me please what am I doing wrong?

0

The correct directory structure is src/Plugin, not src/Plugins.

The files in config/schema/ and config/install/ look correct to me. I struggled with those mightily. And I'll emphasize that actions only seem to be registered when the module is installed.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.