Score:1

Hide Admin Toolbar in iFrame

cn flag

Is it somehow possible within Drupal 8 to hide the Admin Toolbar when the site is loaded within an iFrame? Like e.g. passing an argument? :-)

I thought about using some sort of preprocess on the page that is getting rendered within the iFrame, but this will not work since the html is cached by Drupal. Also I thought about doing it within Javascript, but I do not want to include a javascript file on every page but default.

Maybe someone knows a hook_() which will allow me to manipulate the css but is not getting cached?

za flag
can you use a different theme for admin/user? https://www.drupal.org/project/toolbar_visibility
cn flag
No, since this is an implementation for a contrib module, I do not want to force the users to install another theme.
Score:3
cg flag

I would use a similar approach as @Hudri and access the site as https://www.mysite.com?is_iframe=true.

Then, add a class to the body tag if the request option is there:

/**
 * Implements hook_preprocess_HOOK().
 */
function MYMODULE_preprocess_html(&$variables) {
  $is_iframe = \Drupal::request()->query->get('is_iframe');
  if ($is_iframe === 'true') {
    $variables['attributes']['class'][] = 'hide-admin-toolbar';
  }
}

If I remember correctly, Drupal will regenerate the HTML when you access https://www.mysite.com?is_iframe=true vs https://www.mysite.com.


References:

bw flag
Great solution! I assume you're using CSS to hide the toolbar. That can get a bit gnarly if you're also using admin_toolbar. Another option would be to prevent it from rendering at all, e.g.: `if ($is_iframe === 'true') { if (isset($variables['page_top']['toolbar'])) { unset($variables['page_top']['toolbar']); }}`
Score:2
ru flag

This should be doable with a cache context:

Drupal core already provides a cache context
url.query_args
which can be used for your query parameter, e.g. an URL
https://www.mysite.com?is_iframe=true
would be a cache context
\Drupal::service('cache_context.url.query_args')->getContext('is_iframe');

AFAIK the cache_context url.query_args is already enabled by default in Drupal core, so you only need to add this cache context to the render array in an appropriate preprocess function.

Note that cache contexts generally do not work with Internal Page Cache module!

For production you should finetune this with a more specific, custom cache-context url.query_args:my_query_param to limit unnecessary cache contexts (dumb bots could trigger a gazillion cache context calculations otherwise)

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.