You are here

public function ViewPrintController::viewPrint in Entity Print 8.2

Print an entity to the selected format.

Parameters

string $export_type: The export type.

string $view_name: The view name.

string $display_id: The view display to render.

Return value

\Symfony\Component\HttpFoundation\Response The response object on error otherwise the Print is sent.

1 string reference to 'ViewPrintController::viewPrint'
entity_print_views.routing.yml in modules/entity_print_views/entity_print_views.routing.yml
modules/entity_print_views/entity_print_views.routing.yml

File

modules/entity_print_views/src/Controller/ViewPrintController.php, line 91

Class

ViewPrintController
Controller class for printing Views.

Namespace

Drupal\entity_print_views\Controller

Code

public function viewPrint($export_type, $view_name, $display_id) {

  // Create the Print engine plugin.
  $config = $this
    ->config('entity_print.settings');

  /** @var \Drupal\views\Entity\View $view */
  $view = $this->entityTypeManager
    ->getStorage('view')
    ->load($view_name);
  $executable = $view
    ->getExecutable();
  $executable
    ->setDisplay($display_id);
  if ($args = $this->currentRequest->query
    ->get('view_args')) {
    $executable
      ->setArguments($args);
  }
  try {
    $print_engine = $this->pluginManager
      ->createSelectedInstance($export_type);
  } catch (PrintEngineException $e) {

    // Build a safe markup string using Xss::filter() so that the instructions
    // for installing dependencies can contain quotes.
    $this
      ->messenger()
      ->addError(new FormattableMarkup('Error generating Print: ' . Xss::filter($e
      ->getMessage()), []));
    $url = $executable
      ->hasUrl(NULL, $display_id) ? $executable
      ->getUrl(NULL, $display_id)
      ->toString() : Url::fromRoute('<front>');
    return new RedirectResponse($url);
  }
  return (new StreamedResponse(function () use ($view, $print_engine, $config) {

    // The printed document is sent straight to the browser.
    $this->printBuilder
      ->deliverPrintable([
      $view,
    ], $print_engine, $config
      ->get('force_download'), $config
      ->get('default_css'));
  }))
    ->send();
}