There seems to be a problem when I check if the node is served by ajax or not, I use this function :

function mytheme_preprocess_node(&$variables) {
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        $variables['is_ajax'] = true;
        $variables['is_ajax'] = false;

The same code works great with preprocess_page or preprocess_html

Is there a way to make it work? or eventually get the page/html variable in the node preprocess ?

Thank you !

  • maybe just check if current path is system/ajax
    – user21641
    Commented Oct 31, 2016 at 11:18

1 Answer 1


The node is cached and this function is only called for the first time the node is displayed for the given view mode.

If you really need to vary your output between ajax and non-ajax context, you need to add a cache context for that. See https://www.drupal.org/docs/8/api/cache-api/cache-contexts and the other related documentation pages. You could use the header:NAME-OF-THE-header cache context I guess.

As an alternative, do whatever needs to be done differently with ajax vs not with Javascript/CSS

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.