PrintInvoiceAction.php in Ubercart 8.4
File
uc_order/src/Plugin/Action/PrintInvoiceAction.php
View source
<?php
namespace Drupal\uc_order\Plugin\Action;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelEvents;
class PrintInvoiceAction extends ActionBase implements ContainerFactoryPluginInterface {
protected $dispatcher;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventDispatcherInterface $dispatcher) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->dispatcher = $dispatcher;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('event_dispatcher'));
}
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
return $object
->access('view', $account, $return_as_object);
}
public function execute($order = NULL) {
$build = [
'#theme' => 'uc_order_invoice',
'#order' => $order,
'#op' => 'print',
'#prefix' => '<div style="page-break-after: always;">',
'#suffix' => '</div>',
];
$output = '<html><head><title>Invoice</title></head>';
$output .= '<body onload="print();">';
$output .= drupal_render($build);
$output .= '</body></html>';
$response = new Response($output);
$listener = function ($event) use ($response) {
$event
->setResponse($response);
};
$this->dispatcher
->addListener(KernelEvents::RESPONSE, $listener);
}
}