You are here

public function EntityPrintController::viewPrint in Opigno certificate 3.x

Same name and namespace in other branches
  1. 8 src/Controller/EntityPrintController.php \Drupal\opigno_certificate\Controller\EntityPrintController::viewPrint()

Print an entity to the selected format.

Parameters

string $entity_type: The entity type.

string $entity_id: The entity ID.

Return value

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

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 string reference to 'EntityPrintController::viewPrint'
opigno_certificate.routing.yml in ./opigno_certificate.routing.yml
opigno_certificate.routing.yml

File

src/Controller/EntityPrintController.php, line 83

Class

EntityPrintController
Print controller.

Namespace

Drupal\opigno_certificate\Controller

Code

public function viewPrint($entity_type, $entity_id) {
  $entity = $this->entityTypeManager
    ->getStorage($entity_type)
    ->load($entity_id);
  $opigno_certificate = $entity->field_certificate->entity;

  // We're going to render the opigno_certificate,
  // but the opigno_certificate will need pull
  // information from the entity that references it. So set the
  // 'referencing_entity' computed field to the entity being displayed.
  $opigno_certificate
    ->set('referencing_entity', $entity);

  // @todo: check opigno_certificate access before rendering the opigno_certificate.
  // @todo: implement entity access to check that user has completed learning
  // path. This will need to be a custom access operation other than 'view'.
  // Create the Print engine plugin.
  $config = $this
    ->config('entity_print.settings');
  $print_engine = $this->pluginManager
    ->createSelectedInstance('pdf');
  return (new StreamedResponse(function () use ($opigno_certificate, $print_engine, $config) {

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