-2

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?

5
  • You are assigning (single =) inside both conditions; you should be comparing (double ==) Commented Dec 10, 2021 at 17:11
  • I tried that. still no luck
    – chaydru
    Commented Dec 10, 2021 at 19:00
  • 1
    Please update your code sample above to reflect the corrections I suggested. Commented Dec 10, 2021 at 19:50
  • updated my code
    – chaydru
    Commented Dec 10, 2021 at 20:55
  • You are reading the array incorrectly on top of using = to check equality, as I was saying in the question you deleted.
    – Kevin
    Commented Dec 13, 2021 at 3:25

1 Answer 1

0

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;
  }

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