diff --git a/entity_print.module b/entity_print.module index 39c2b42..b4f2326 100644 --- a/entity_print.module +++ b/entity_print.module @@ -78,17 +78,21 @@ function entity_print_entity_view_alter(array &$build, EntityInterface $entity, $route_params = [ 'entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), + 'revision_id' => $entity->getRevisionId() ?? '', 'export_type' => trim($export_type, '_engine'), ]; - $build[$key] = [ '#type' => 'print_link', '#title' => $display->getThirdPartySetting('entity_print', $export_type . '_label', t('View @label', ['@label' => $definition['label']])), '#export_type' => $export_type, - '#url' => Url::fromRoute('entity_print.view', $route_params), '#weight' => $component['weight'] ?? 0, - '#access' => $access_manager->checkNamedRoute('entity_print.view', $route_params, NULL, TRUE), + '#access' => $access_manager->checkNamedRoute('entity_print.revision.view', $route_params, NULL, TRUE), ]; + $route_name = 'entity_print.revision.view'; + if (empty($route_params['revision_id'])) { + $route_name = 'entity_print.view'; + } + $build[$key]['#url'] = Url::fromRoute($route_name, $route_params); } } } @@ -146,4 +150,4 @@ function entity_print_form_entity_view_display_edit_form_submit(&$form, FormStat } } $display->save(); -} +} \ No newline at end of file diff --git a/entity_print.routing.yml b/entity_print.routing.yml index a3ec50f..83e5f0d 100644 --- a/entity_print.routing.yml +++ b/entity_print.routing.yml @@ -30,6 +30,22 @@ entity_print.view.debug: requirements: _custom_access: '\Drupal\entity_print\Controller\EntityPrintController::checkAccess' +entity_print.revision.view: + path: 'print/{export_type}/{entity_type}/{entity_id}/{revision_id}' + defaults: + _controller: '\Drupal\entity_print\Controller\EntityPrintController::viewPrint' + _title: 'Entity Print' + requirements: + _custom_access: '\Drupal\entity_print\Controller\EntityPrintController::checkAccess' + +entity_print.revision.view.debug: + path: 'print/{export_type}/{entity_type}/{entity_id}/{revision_id}/debug' + defaults: + _controller: '\Drupal\entity_print\Controller\EntityPrintController::viewPrintDebug' + _title: 'Entity Print Debug' + requirements: + _custom_access: '\Drupal\entity_print\Controller\EntityPrintController::checkAccess' + entity_print.settings: path: 'admin/config/content/entityprint' defaults: diff --git a/src/Controller/EntityPrintController.php b/src/Controller/EntityPrintController.php index 9bfdeb3..c8ee9db 100644 --- a/src/Controller/EntityPrintController.php +++ b/src/Controller/EntityPrintController.php @@ -76,14 +76,17 @@ class EntityPrintController extends ControllerBase { * The entity type. * @param int $entity_id * The entity id. + * @param int $revision_id + * The entity revision id. * * @return \Symfony\Component\HttpFoundation\Response * The response object on error otherwise the Print is sent. */ - public function viewPrint($export_type, $entity_type, $entity_id) { + public function viewPrint($export_type, $entity_type, $entity_id, $revision_id = NULL) { // Create the Print engine plugin. $config = $this->config('entity_print.settings'); - $entity = $this->entityTypeManager->getStorage($entity_type)->load($entity_id); + $entity_storage = $this->entityTypeManager->getStorage($entity_type); + $entity = $revision_id ? $entity_storage->loadRevision($revision_id) : $entity_storage->load($entity_id); $print_engine = $this->pluginManager->createSelectedInstance($export_type); return (new StreamedResponse(function () use ($entity, $print_engine, $config) { @@ -101,14 +104,17 @@ class EntityPrintController extends ControllerBase { * The entity type. * @param int $entity_id * The entity id. + * @param int $revision_id + * The entity revision id. * * @return \Symfony\Component\HttpFoundation\Response * The response object. * * @todo improve permissions in https://www.drupal.org/node/2759553 */ - public function viewPrintDebug($export_type, $entity_type, $entity_id) { - $entity = $this->entityTypeManager->getStorage($entity_type)->load($entity_id); + public function viewPrintDebug($export_type, $entity_type, $entity_id, $revision_id = NULL) { + $entity_storage = $this->entityTypeManager->getStorage($entity_type); + $entity = $revision_id ? $entity_storage->loadRevision($revision_id) : $entity_storage->load($entity_id); $use_default_css = $this->config('entity_print.settings')->get('default_css'); return new Response($this->printBuilder->printHtml($entity, $use_default_css, FALSE)); } @@ -125,11 +131,13 @@ class EntityPrintController extends ControllerBase { * The entity type. * @param int $entity_id * The entity id. + * @param int $revision_id + * The entity revision id. * * @return \Drupal\Core\Access\AccessResult * The access result object. */ - public function checkAccess($export_type, $entity_type, $entity_id) { + public function checkAccess($export_type, $entity_type, $entity_id, $revision_id = NULL) { if (empty($entity_id)) { return AccessResult::forbidden(); } @@ -141,8 +149,15 @@ class EntityPrintController extends ControllerBase { return AccessResult::forbidden(); } + $entity_storage = $this->entityTypeManager->getStorage($entity_type); + // Unable to find the entity requested. - if (!$entity = $this->entityTypeManager->getStorage($entity_type)->load($entity_id)) { + if (!$entity = $entity_storage->load($entity_id)) { + return AccessResult::forbidden(); + } + + // Unable to find the entity revision requested. + if ($revision_id && !$entity = $entity_storage->loadRevision($revision_id)) { return AccessResult::forbidden(); } @@ -180,16 +195,19 @@ class EntityPrintController extends ControllerBase { * @param string $entity_type * The entity type. * @param string|int $entity_id - * The entity type id. + * The entity id. + * @param int $revision_id + * The entity revision id. * * @return \Symfony\Component\HttpFoundation\RedirectResponse * The redirect response. */ - public function viewRedirect($export_type, $entity_type, $entity_id) { + public function viewRedirect($export_type, $entity_type, $entity_id, $revision_id = NULL) { return $this->redirect('entity_print.view', [ 'export_type' => $export_type, 'entity_type' => $entity_type, 'entity_id' => $entity_id, + 'revision_id' => $revision_id, ]); } @@ -202,15 +220,18 @@ class EntityPrintController extends ControllerBase { * The entity type. * @param string|int $entity_id * The entity type id. + * @param int $revision_id + * The entity revision id. * * @return \Symfony\Component\HttpFoundation\RedirectResponse * The redirect response. */ - public function viewRedirectDebug($export_type, $entity_type, $entity_id) { + public function viewRedirectDebug($export_type, $entity_type, $entity_id, $revision_id = NULL) { return $this->redirect('entity_print.view.debug', [ 'export_type' => $export_type, 'entity_type' => $entity_type, 'entity_id' => $entity_id, + 'revision_id' => $revision_id, ]); }