Drupal 8 makes it possible to carry out certain mass actions on the site's contents, such as publishing or unpublishing massively contents, etc. It may be useful to provide to certain user profiles some customized actions related to the specificities of their site, in this tuto, I'll show you how to create our custom bulk export action to export nodes to excel, csv or pdf file for example.
Create a module
In Drupal 8, it is necessary to create an info.yml
file that contains the metadata for every custom module. you will need to create the codimth_custom_action.info.yml
file under the modules/custom/codimth_custom_action
folder. Inside this file enter following:
name: Codimth Custom Action
description: Create Custom Action
package: Codimth
type: module
core_version_requirement: ^8.8.0 || ^9.0
Once the folder and file has been created, you can go to your Drupal dashboard and enable the custom module we have just created.
In the module's config folder, we provide the schema (config/schema/codimth_custom_action.schema.yml) of the configuration implemented, namely the configurations of our custom action: system.action.node_export_action.yml.
create config/schema/codimth_custom_action.schema.yml file like this :
action.configuration.codimth_custom_action:
type: node_export_action
label: 'Export Content'
Let's look at the contents of the file config/install/system.action.node_export_action.yml .
langcode: en
status: true
dependencies:
module:
- node
id: node_export_action
label: 'Export Content'
type: node
plugin: node_export_action
configuration: { }
The key elements of the plugin configuration are:
- Its dependencies: we declare the node module in order to be able to use the nodes.
- The identifier of the configuration (id) that must correspond to the identifier included in the file name (system.action.ID.yml ).
- The entity type on which the plugin will act (here nodes entities)
- And finally the identifier of the Plugin Class (key plugin). It is this identifier that we declare in our class.
Let's browse the file of this Plugin, the file codimth_custom_action/src/Plugin/Action/ExportAction.php.
<?php
namespace Drupal\codimth_custom_action\Plugin\Action;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Session\AccountInterface;
/**
* create custom action
*
* @Action(
* id = "node_export_action",
* label = @Translation("Export Content"),
* type = "node"
* )
*/
class ExportAction extends ActionBase {
/**
* {@inheritdoc}
*/
public function execute($node = NULL) {
if ($node) {
// TODO: export your node here
\Drupal::messenger()->addStatus('node is exported ');
}
}
/**
* {@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('create', $account, TRUE);
return $return_as_object ? $result : $result->isAllowed();
}
}
This Action plugin simply extends the ActionBase class and overrides its two main methods.
- The execute() method, which will execute the desired operation, here you can export your nodes to excel, csv or pdf file for example.
- The access() method, which will verify that the user has access to do this operation.
You will notice in the Annotations of the Plugin, its identifier, the same one declared in the plugin configuration file, as well as the entity type on which our Action Plugin applies.
After enabling this module, we can see now this custom action in our administration view.