Score:0

VBO custom actions - reset after action

gg flag

I have created two VBO custom action buttons. One prints orders, the other updates them. Clicking one disables it and the other until the page reloads (losing all checked orders).

How can I have the print button rebuild/refresh/reload the page or otherwise make the buttons available again and maintain the checked items, if possible.

Here is the unique code for the two buttons:

class PrintOrderAction extends ViewsBulkOperationsActionBase implements ContainerFactoryPluginInterface {

  ...

  public function execute(Order $order = NULL) {
    $this->executeMultiple([$order]);
    return $this->t('Order Invoice(s) Printed');
  }

  public function executeMultiple(array $orders) {
    try {
      (new StreamedResponse(function() use ($orders){
        $this->printBuilder->deliverPrintable($orders, $this->entityPrintPluginManager->createSelectedInstance('pdf'), TRUE);
      }))->send();
    }
    catch (PrintEngineException $e){
      $this->messenger()->addError(new FormattableMarkup(Xss::filter($e->getMessage()), []));
    }
  }

}

...

class FulfillOrderAction extends ViewsBulkOperationsActionBase {

...

public function execute(Order $order = NULL) {
    if(!$state = $order->getState()){
      return $this->t('Order #:number can\'t change state',[
        ':number' => $order->getOrderNumber(),
      ]);
    }

    $order_state_transitions = $state->getTransitions();
    if(!is_null($order_state_transitions['validate'])) {
      $state->applyTransition($order_state_transitions['validate']);
      $order->save();
    }

    return $this->t('Order #:number marked fulfill', [
      ':number' => $order->getOrderNumber(),
    ]);
  }

}

EDIT: I believe this has to do with the StreamedResponse and how it doesn't reload the page, but leaves the form essentially unusable. For example, if I select two items to print, print them and then try to print those two again, nothing happens. If I select other items to print, it tries to do something, but eventually times out.

Is Drupal freezing the form in some way that prohibits further submissions, perhaps in some effort to eliminate double submissions? If so, is there some way to disable that for this particular form?

Does that even make sense. Really struggling with this one.

Jaypan avatar
de flag
I don't have the answer to your question, but as a re-think, I would probably just create a third action that calls the first two.
Delford Chaffin avatar
gg flag
@Jaypan - That's an interesting possibility. I didn't envision them necessarily always applying to the same selected options all the time and tried to keep it flexible. But if that seems to be the way real-world use continues, maybe that's a good idea. Thanks!
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.