0

I'd like to add in my website some links which open the content of the page in a new tab in plain html without any theme etc. At first I added just a link in my node.twig and try with Javascript to do the thing but I didn't successfully achieve it.

When we use a link by default the theme is applied and so the page is just the same in the new tab. I searched a way to do that and I found this post :

displaying a Drupal view without a page template around it

Based on the answer of Ufonion Labs I was able to completely remove all the HTML output around the page content in Drupal 7 by implementing both hook_preprocess_page and hook_preprocess_html in my themes template.php, like this:

function MY_THEME_preprocess_page(&$variables) {
  if (isset($_GET['response_type']) && $_GET['response_type'] == 'embed') {
    $variables['theme_hook_suggestions'][] = 'page__embed';
  }
}

function MY_THEME_preprocess_html(&$variables) {
  if (isset($_GET['response_type']) && $_GET['response_type'] == 'embed') {
    $variables['theme_hook_suggestions'][] = 'html__embed';
  }
}

Then I added two templates to my theme: html--embed.tpl.php:

<?php print $page; ?>

and page--embed.tpl.php:

<?php print render($page['content']); ?>

Now when I open a node page, such as http://example.com/node/3, I see the complete page as usual, but when I add the response_type parameter, such as http://example.com/node/3?response_type=embed, I only get the <div> with the page contents so it can be embedded in another page.

I really like than the url path determine the output if the theme is loaded or not but in my case it's not working: I'm on Drupal 9 with Twig for rendering view so I added the code of template.php in the .theme file but how do you do the part for Twig ? I wonder if you think I can achieve the same thing or is it not possible to do it that way.

My second solution would be to do a custom module but I'm not familiar with that so that's why I estimate all the possible solution.

Best regards and have a good day !

Tenebros
  • 23
  • 5

1 Answers1

0

I m not sure this is what you want to do but you can override the page template with a custom template after adding a theme suggestion


/**
 * Implements HOOK_theme_suggestions_page_alter().
 */
function themename_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  if ($template = \Drupal::request()->get('template')){
      $suggestions[] = 'page__'.$template;
  }
}

Then Create a template in your base theme folder theme/templates/layout/ e.g

theme/templates/layout/page--embed.html.twig

If you want the URL to determine the template or theme then you would need to write a custom controller where you can render entity with a custom template but you can also do it in the theme which is not recommended

Easy way to solve your problem:

/**
 * Implements HOOK_theme_suggestions_page_alter().
 */
function themename_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  if ($template = \Drupal::request()->get('template')){
      $suggestions[] = 'page__'.$template;
  }
}

Template = theme/templates/layout/page--embed.html.twig

/**
 * Implements HOOK_theme_suggestions_html_alter().
 */
function themename_theme_suggestions_html_alter(array &$suggestions, array $variables) {
  if ($template = \Drupal::request()->get('template')){
      $suggestions[] = 'html__'.$template;
  }
}

Template = theme/templates/layout/page--embed.html.twig

/**
 * Implements HOOK_page_attachments_alter().
 */
function themename_page_attachments_alter(array &$attachments){
  if (\Drupal::request()->get('template') == 'embed') {
    //Remove all libraries
    unset($attachments['#attached']['library']);
  }
}


Make sure all your links have this query parameter ?template=embed at the end e.g example.com/node/3?template=embed

as long the URL has ?template=embed at the end, the code should work.

replace themename => with your theme name