Score:-2

Alter "Action processing results" message for a specific action

in flag

I have a view based on view bulk operations. this is a comment based view and performs the below actions:

1. Publish comment 2. Unpublish comment

When one of these actions are selected to be executed on a set of records, I get to see a success message like below:

Action processing results: Unpublish Comment (1).

I would like to alter the success message as:

The selected comment(s) have been unpublished successfully

I am using VBO 4.0.0 which has action processing message update commit in it. No clue about how this can be implemented for a specific actions. any help?

UPDATE:

Below is my trial so far:

function mymod_batch_alter(&$batch) {
  foreach ($batch['sets'] as $key => $set) {
      $batch['sets'][$key]['finished'] = 'mymod_views_bulk_operations_execute_finished';
    }
  }

/**
 * Implements hook_views_bulk_operations_execute_finish().
 */
function mymod_views_bulk_operations_execute_finished($success, array $results, array $operations) {
    if ($success) {
      $operations = $results['operations'];
      if($operations == "Approve Comment"){
        $message = \Drupal::messenger()->addMessage('Success! Approved');
      }
      elseif($operations == "Disapprove Comment"){
        $message = \Drupal::messenger()->addMessage('Success! Disapproved');
      }
    }
    else {
      $message = \Drupal::messenger()->addWarning('Failure message...');
    }
    return NULL;
  }

This code is working well, but just one issue. everytime, even for the "disapprove comment" action, I get to see "Success! Approved" message. Control is going into wrong if-condition. Can someone point the mistake out?

Stefanos Petrakis avatar
in flag
You are assigning (single =) inside both conditions; you should be comparing (double ==)
in flag
I tried that. still no luck
Stefanos Petrakis avatar
in flag
Please update your code sample above to reflect the corrections I suggested.
in flag
updated my code
Kevin avatar
in flag
You are reading the array incorrectly on top of using = to check equality, as I was saying in the question you deleted.
Score:0
in flag

Alright, the below code works:

function mymod_batch_alter(&$batch) {
  foreach ($batch['sets'] as $key => $set) {
      $batch['sets'][$key]['finished'] = 'mymod_views_bulk_operations_execute_finished';
    }
  }

/**
 * Implements hook_views_bulk_operations_execute_finish().
 */
function mymod_views_bulk_operations_execute_finished($success, array $results, array $operations) {
    if ($success) {
      $operations = $results['operations'];
      if($operations[0] == "Approve Comment"){
        $message = \Drupal::messenger()->addMessage('Success! Approved');
      }
      elseif($operations[0] == "Disapprove Comment"){
        $message = \Drupal::messenger()->addMessage('Success! Disapproved');
      }
    }
    else {
      $message = \Drupal::messenger()->addWarning('Failure message...');
    }
    return NULL;
  }
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.