diff --git a/entity_print.module b/entity_print.module index 88e7373..b0a2b9a 100644 --- a/entity_print.module +++ b/entity_print.module @@ -5,6 +5,7 @@ * Print any entity. */ +use Drupal\Core\Config\Config; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Form\FormStateInterface; @@ -36,7 +37,7 @@ function entity_print_entity_extra_field_info_alter(&$info) { foreach ($entity_info as &$bundle_info) { $bundle_info['display']['entity_print_view'] = [ 'label' => t('Entity Print'), - 'description' => t('Provides a link to view the PDF version of the entity'), + 'description' => t('Provides a link to view the Print version of the entity'), 'weight' => 0, 'visible' => FALSE, ]; @@ -48,17 +49,22 @@ function entity_print_entity_extra_field_info_alter(&$info) { * Implements hook_entity_view_alter(). */ function entity_print_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) { - if ($display->getComponent('entity_print_view')) { - $route_params = [ - 'entity_type' => $entity->getEntityTypeId(), - 'entity_id' => $entity->id(), - ]; - $build['entity_print_view'] = Link::createFromRoute($display->getThirdPartySetting('entity_print', 'label', t('View PDF')), 'entity_print.view', $route_params) - ->toRenderable(); + $config = \Drupal::config('entity_print.settings'); + + foreach ($config->get('print_engines') as $export_type => $print_engine) { + if ($display->getComponent('entity_print_view')) { + $route_params = [ + 'entity_type' => $entity->getEntityTypeId(), + 'entity_id' => $entity->id(), + 'export_type' => trim($export_type, '_engine'), + ]; + $build['entity_print_view'] = Link::createFromRoute($display->getThirdPartySetting('entity_print', 'label', t('View :export_type', [':export_type' => $export_type])), 'entity_print.view', $route_params) + ->toRenderable(); - // Add the access control. - $build['entity_print_view']['#access'] = \Drupal::accessManager() - ->checkNamedRoute('entity_print.view', $route_params); + // Add the access control. + $build['entity_print_view']['#access'] = \Drupal::accessManager() + ->checkNamedRoute('entity_print.view', $route_params); + } } } diff --git a/entity_print.post_update.php b/entity_print.post_update.php index d23c06d..662128c 100644 --- a/entity_print.post_update.php +++ b/entity_print.post_update.php @@ -1,13 +1,13 @@ getSettings(); $settings['default_paper_size'] = 'letter'; $engine_config->setSettings($settings); @@ -20,11 +20,11 @@ function entity_print_post_update_new_dompdf_configuration() { */ function entity_print_post_update_migrate_config() { $config = \Drupal::configFactory()->getEditable('entity_print.settings'); - if ($plugin_id = $config->get('pdf_engine')) { + if ($plugin_id = $config->get('print_engine')) { /** @var \Drupal\entity_print\Plugin\EntityPrintPluginManager $plugin_manager */ - $plugin_manager = \Drupal::service('plugin.manager.entity_print.pdf_engine'); + $plugin_manager = \Drupal::service('plugin.manager.entity_print.print_engine'); $definition = $plugin_manager->getDefinition($plugin_id); - /** @var \Drupal\entity_print\Plugin\PdfEngineInterface $class */ + /** @var \Drupal\entity_print\Plugin\PrintEngineInterface $class */ $class = $definition['class']; if ($class::dependenciesAvailable()) { @@ -39,7 +39,7 @@ function entity_print_post_update_migrate_config() { } // Create the new config entity. \Drupal::entityTypeManager() - ->getStorage('pdf_engine') + ->getStorage('print_engine') ->create($values) ->save(); diff --git a/entity_print.routing.yml b/entity_print.routing.yml index 1d98bc7..fabb701 100644 --- a/entity_print.routing.yml +++ b/entity_print.routing.yml @@ -1,5 +1,5 @@ entity_print.view: - path: 'entityprint/{entity_type}/{entity_id}' + path: 'entityprint/{export_type}/{entity_type}/{entity_id}' defaults: _controller: '\Drupal\entity_print\Controller\EntityPrintController::viewPrint' _title: 'Entity Print' diff --git a/src/Controller/EntityPrintController.php b/src/Controller/EntityPrintController.php index 28d7eaa..76f1e16 100644 --- a/src/Controller/EntityPrintController.php +++ b/src/Controller/EntityPrintController.php @@ -66,6 +66,8 @@ class EntityPrintController extends ControllerBase { /** * Output an entity as a Print. * + * @param string $export_type + * The export type. * @param string $entity_type * The entity type. * @param int $entity_id @@ -74,13 +76,14 @@ class EntityPrintController extends ControllerBase { * @return \Symfony\Component\HttpFoundation\Response * The response object on error otherwise the Print is sent. */ - public function viewPrint($entity_type, $entity_id) { + public function viewPrint($export_type, $entity_type, $entity_id) { // Create the Print engine plugin. $config = $this->config('entity_print.settings'); $entity = $this->entityTypeManager->getStorage($entity_type)->load($entity_id); + $config_engine = 'print_engines.' . $export_type . '_engine'; try { - $print_engine = $this->pluginManager->createInstance($config->get('print_engines.pdf_engine')); + $print_engine = $this->pluginManager->createInstance($config->get($config_engine)); } catch (PrintEngineException $e) { // Build a safe markup string using Xss::filter() so that the instructions @@ -125,6 +128,8 @@ class EntityPrintController extends ControllerBase { * We need to validate that the user is allowed to access this entity also the * print version. * + * @param string $export_type + * The export type. * @param string $entity_type * The entity type. * @param int $entity_id @@ -133,7 +138,7 @@ class EntityPrintController extends ControllerBase { * @return bool * TRUE if they have access otherwise FALSE. */ - public function checkAccess($entity_type, $entity_id) { + public function checkAccess($export_type, $entity_type, $entity_id) { $account = $this->currentUser(); // Invalid storage type. diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php index 47930ff..dd5f445 100644 --- a/src/Form/SettingsForm.php +++ b/src/Form/SettingsForm.php @@ -86,14 +86,26 @@ class SettingsForm extends ConfigFormBase { foreach ($this->pluginManager->getDefinitions() as $plugin_id => $definition) { /** @var \Drupal\entity_print\Plugin\PrintEngineInterface $class */ $class = $definition['class']; + $export_type = $definition['export_type']; + if ($class::dependenciesAvailable()) { - $print_engines[$plugin_id] = $definition['label']; + if (array_key_exists($export_type, $print_engines)) { + $print_engines[$export_type] = array($plugin_id => $definition['label']); + } + else { + $print_engines[$export_type][$plugin_id] = $definition['label']; + } } else { - $disabled_engines[$plugin_id] = $definition['label']; + if (array_key_exists($export_type, $disabled_engines)) { + $disabled_engines[$export_type] = array($plugin_id => $definition['label']); + } + else { + $disabled_engines[$export_type][$plugin_id] = $definition['label']; + } - // Show the user which Print engines are disabled, but only for the page load - // not on AJAX requests. + // Show the user which Print engines are disabled, but only for + // the page load not on AJAX requests. if (!$request->isXmlHttpRequest()) { drupal_set_message($this->t('@name is not available because it is not configured. @installation.', [ '@name' => $definition['label'], @@ -121,33 +133,33 @@ class SettingsForm extends ConfigFormBase { '#default_value' => $config->get('force_download'), ]; - $form['entity_print']['print_engine'] = [ + $form['entity_print']['pdf_engine'] = [ '#type' => 'select', '#title' => $this->t('PDF Engine'), '#description' => $this->t('Select the PDF engine to render the Print'), - '#options' => $print_engines, + '#options' => $print_engines['pdf'], '#default_value' => $config->get('print_engines.pdf_engine'), '#empty_option' => $this->t('- None -'), '#ajax' => [ - 'callback' => '::ajaxPluginFormCallback', - 'wrapper' => 'print-engine-config', + 'callback' => '::ajaxPdfPluginFormCallback', + 'wrapper' => 'pdf-engine-config', 'effect' => 'fade', ], ]; - $form['entity_print']['print_engine_config'] = [ + $form['entity_print']['pdf_engine_config'] = [ '#type' => 'container', - '#id' => 'print-engine-config', + '#id' => 'pdf-engine-config', ]; // If we have a print_engine in the form_state then use that otherwise, fall // back to what was saved as this is a fresh form. Check explicitly for NULL // in case they selected the None option which is false'y. - $plugin_id = !is_null($form_state->getValue('print_engine')) ? $form_state->getValue('print_engine') : $config->get('print_engines.pdf_engine'); + $pdf_plugin_id = !is_null($form_state->getValue('pdf_engine')) ? $form_state->getValue('pdf_engine') : $config->get('print_engines.pdf_engine'); // If we have a plugin id and the plugin hasn't since been disabled then we // load the config for the plugin. - if ($plugin_id && !in_array($plugin_id, array_keys($disabled_engines), TRUE)) { - $form['entity_print']['print_engine_config'][$plugin_id] = $this->getPluginForm($plugin_id, $form_state); + if ($pdf_plugin_id && !in_array($pdf_plugin_id, array_keys($disabled_engines), TRUE)) { + $form['entity_print']['pdf_engine_config'][$pdf_plugin_id] = $this->getPluginForm($pdf_plugin_id, $form_state); } return parent::buildForm($form, $form_state); @@ -156,8 +168,8 @@ class SettingsForm extends ConfigFormBase { /** * Ajax form callback. */ - public function ajaxPluginFormCallback(&$form, FormStateInterface $form_state) { - return $form['entity_print']['print_engine_config']; + public function ajaxPdfPluginFormCallback(&$form, FormStateInterface $form_state) { + return $form['entity_print']['pdf_engine_config']; } /** @@ -215,7 +227,7 @@ class SettingsForm extends ConfigFormBase { $this->config('entity_print.settings') ->set('default_css', $values['default_css']) ->set('force_download', $values['force_download']) - ->set('print_engines.pdf_engine', $values['print_engine']) + ->set('print_engines.pdf_engine', $values['pdf_engine']) ->save(); } diff --git a/src/Tests/EntityPrintTest.php b/src/Tests/EntityPrintTest.php index 0c97ea1..8a36172 100644 --- a/src/Tests/EntityPrintTest.php +++ b/src/Tests/EntityPrintTest.php @@ -120,6 +120,11 @@ class EntityPrintTest extends WebTestBase { // Invalid entity id also triggers access denied. $this->drupalGet('entityprint/node/invalid-entity-id/debug'); $this->assertResponse(403, 'Invalid entity id triggers access denied.'); + + // Invalid export type also triggers access denied. + $this->drupalGet('entityprint/invalid/node/' . $this->node->id() . '/debug'); + $this->assertResponse(403, 'Invalid entity id triggers access denied.'); + } }