- Issue created by @matt b
I'd like to create a link on an entity that will then download the PDF
For an article node with a view mode of PDF, I use my theme's preprocess_node hook to add a variable 'pdf_link' that can then be displayed in the node.html.twig
mytheme.theme:
/**
* Implements hook_preprocess_HOOK() for node.html.twig.
* Add PDF link to articles
*/
function mytheme_preprocess_node(&$variables) {
if ($variables['node']->getType()=='article' && \Drupal::currentUser()->hasPermission('view node.article.pdf pdf')) {
$url_options = [
'language' => \Drupal::languageManager()->getCurrentLanguage(),
];
$variables['pdf_link'] = \Drupal\Core\Url::fromRoute('entity_pdf.view', [
'entity_type' => 'node',
'entity' => $variables['node']->id(),
'view_mode' => 'pdf'
], $url_options)->toString();
}
}
Thought the above might help and I'm interested if there is a better way to do this?
Active
2.0
Documentation