Drupal 8 创建一个自定义动作/Action

在 mymodule/src/Plugin/Action 定义一个OfflineAction.php的文件

namespace Drupal\sysu_content_server\Plugin\Action;

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

/**
 * Distribution revierer action
 *
 * @Action(
 *   id = "offline_action",
 *   label = @Translation("Offline content"),
 *   type = "node"
 * )
 */
class OfflineAction extends ActionBase {

  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition
  ) {
    $this->configuration = $configuration;
    $this->pluginId = $plugin_id;
    $this->pluginDefinition = $plugin_definition;
  }

  /**
   * Checks object access.
   *
   * @param mixed                                 $object
   *   The object to execute the action on.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   (optional) The user for which to check access, or NULL to check access
   *   for the current user. Defaults to NULL.
   * @param bool                                  $return_as_object
   *   (optional) Defaults to FALSE.
   *
   * @return bool|\Drupal\Core\Access\AccessResultInterface
   *   The access result. Returns a boolean if $return_as_object is FALSE (this
   *   is the default) and otherwise an AccessResultInterface object.
   *   When a boolean is returned, the result of AccessInterface::isAllowed() is
   *   returned, i.e. TRUE means access is explicitly allowed, FALSE means
   *   access is either explicitly forbidden or "no opinion".
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    // TODO: Implement access() method.
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function executeMultiple(array $entities) {
    dpm('hello');
  }

  /**
   * Executes the plugin.
   */
  public function execute(ContentEntityInterface $entity = NULL) {
    // TODO: Implement execute() method.
  }
}

此Action定义好以后并不会刷新到,如果你的模块还没有安装。你可以定义安装文件.
mymodule/config/install/system.action.offline_action.yml

status: true
dependencies:
  module:
    - node
id: distribution_action
label: 'Office line content'
type: node
plugin: distribution_action

如果你的模块已经启用。你可以使用PHP执行

$entityManage = $action = \Drupal::entityTypeManager()->getStorage('action');
$entityManage->create([
  'label' => t('Offline content'),
  'id' => 'offline_action',
  'type' => 'node',
  'plugin' => 'offline_action',
])->save();