Create an action for custom mass updates with Drupal 8

sun above town

Drupal 8 makes it possible to carry out certain mass actions on the site's contents, such as publishing or unpublishing massively contents, positioning them at the top of lists, etc. It may be useful to provide to certain user profiles some customized actions related to the specificities of their site, such as highlighting certain taxonomy terms, changing the value of a specific field, and thus avoiding heavy and tedious update operations to users on each of the content to be modified.

To simplify the management of these contents, we can create customized actions, which will rely on the plugin Action provided by Drupal core, actions that can be launched massively from a view, like what proposed the Views bulk operation module for Drupal 7, whose the migration to Drupal 8 of some of its features is under discussion.

How to highlight certain taxonomy terms

We will go on an use case that is not uncommon to meet. Imagine a site that has a Keywords vocabulary, and we want to be able to easily highlight certain keywords that would be placed in a block of a dedicated view.

We can simply create a boolean field (we will call it field_push) on the Keywords vocabulary and we will then be able to distinguish which keywords we want to put forward on this block, simply by checking this checkbox on each taxonomy term, and of course to filter the keywords on that field into a specific view to retrieve them.

Un champ booléen attaché aux termes de taxonomy Keywords

We can then edit each keyword to check or not this option. But for a publisher, or a webmaster, this task can quickly prove tedious. It is necessary to identify the keywords already put forward or not, go on each of them, then modify them according to the moment. On a site with many keywords, this can become very time-consuming or even source of error.

Modifier les termes de taxonomy un par un

 

Creating an administrative View

We can create a view that will allow publishers to manage these keywords and their highlights. This will allow them to quickly identify which keywords are put forward, and to be able to modify them in mass in a few clicks.

Création d'une vue basée sur les termes de taxonomy

We therefore create a view based on taxonomy terms, and we restrict this view to the Keywords vocabulary.

We can quickly obtain a view listing the keywords, their status with the boolean field highlighting them, and an action list on each taxonomy term to be able to modify them one by one.

Une vue d'administration basique des termes

But we do not have on our view this magic field called Bulk update which allows us to launch mass updates on the selected items of our view, like what we can have on a view listing contents or users.

If we do not have this Bulk update field in views for taxonomy terms, it is simply because by default no action is yet defined on this entity type. But creating a customized action, thanks to the Drupal 8 Plugin system, can be done very simply.

Create a custom bulk update action

We will create a module that we will call My BO (machine name: my_bo) that will provide us these custom actions. The structure of this module is illustrated below. You can find the entire code of this sample module on this GitHub repository.

Structure du module My BO

In the module's config folder, we provide the schema (my_bo.schema.yml) of the configuration implemented, namely the configurations of our two custom actions: system.action.term_push_front.yml and system.action.term_unpush_front.yml.

The src/ Plugin/Action folder will contain the classes of the two Action Plugins created.

Let's look at the contents of the file system.action.term_push_front.yml

# File system.action.term_push_front.yml
langcode: en
status: true
dependencies:
  module:
    - taxonomy
id: term_push_front
label: 'Push term in front'
type: taxonomy_term
plugin: term_push_front
configuration: {  }

The key elements of the plugin configuration are:

  • Its dependencies: we declare the taxonomy module in order to be able to use the taxonomy terms
  • The identifier of the configuration (id) that must correspond to the identifier included in the file name
  • The entity type on which the plugin will act (here taxonomy_term 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 src/Plugin/Action/TermPushFront.php

<?php

namespace Drupal\my_bo\Plugin\Action;

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

/**
 * Push term in front.
 *
 * @Action(
 *   id = "term_push_front",
 *   label = @Translation("Push term in front"),
 *   type = "taxonomy_term"
 * )
 */
class TermPushFront extends ActionBase {

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

  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    /** @var \Drupal\taxonomy\TermInterface $object */
    $result = $object->access('update', $account, TRUE)
      ->andIf($object->field_push->access('edit', $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 change the value of our boolean field field_push
  • The access() method, which will verify that the user initiating the update operation has the right to modify the entity in question and this particular field

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 bulk update field in our administration view.

Champ views de mise à jour massive sur les termes de taxonomy

And we can select the actions that will be available via this bulk update field.

Paramètres du champ de mise à jour massive

An administration view for custom bulk upgrades

We then have a view allowing some users to mass update the site's keywords, on a specific field. We can also note that creating a specific action can allow us to allow some users to update a property of an entity, such as taxonomy terms, without having rights to that entity itself. All you have to do is to customize the access() method of the plugin to modulate these rights.

Une vue d'administration de termes de taxonomy avec des mises à jour en masse possible

 

Going further with configurable actions

In this post, we created simple actions to modify the value of a field in a predetermined way. Boolean fields lend themselves quite well. But we can also easily create configurable actions that allow you to modify the value of a text field in a massive way, for example by entering or selecting a new value when you start the update. To do this, it is enough that the Plugin extends not the ActionBase class, but the ConfigurableActionBase class that will allow us to implement a form allowing this interaction. But this may be the subject of another post.

 

Commentaires

Soumis par Renrhaf (non vérifié) le 07/06/2017 à 01:25 - Permalien

Hello and thanks for this useful article.
Any idea how to create an action as a batch action ?

Soumis par Daniele (non vérifié) le 04/07/2017 à 01:17 - Permalien

Thanks for your article. But I need to create an action with a form, just like i did in D7 with hook_action_form. How can I do it? I created a Plugin that extends ConfigurableActionBase class. But when I try to execute the actions, i cannot see the form I declared in buildConfigurationForm method.
Suggestions?

Soumis par MPP (non vérifié) le 02/10/2017 à 12:40 - Permalien

Any idea how you can pass an argument from the view to the action (E.g. a view with the current ID argument)?

Ajouter un commentaire