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?


2 Answers 2


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.


  • 1
    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']); }} Apr 7, 2022 at 17:46

This should be doable with a cache context:

Drupal core already provides a cache context
which can be used for your query parameter, e.g. an URL
would be a cache context

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.