Skip to main content
2 of 3
Added some code for better context

Skip / Don't render template file

To get straight to the point, there are times when I may not want to render anything. Some condition failed and I now want to display an error for the admin/developer and skip rendering a template file so the front end isn't affected.

The context would be in a preprocess function. In this case I'm using hook_preprocess_entity. I check for a condition and if it fails, I want to skip the template. I'm thinking this hook may be too late since it's already decided that a template file should be used. But I don't think there's a hook that I can reliably use to intercept this.

I've been digging in core's theme.inc but can't figure it out.

I tried resetting the theme_hook_suggestions and theme_hook_suggestion but it seemed to still default to a template file.

I've tried moving my check into the template file itself (I don't like this one but though, it stinks), but I have to break out of php and return some empty html (blank space/line seems to work); Otherwise Drupal seems to move onto the next template suggestion. Really weird.

Current workaround is registering a new template file with hook_theme and having just a blank line in that template file. If my check fails I set this file as the theme_hook_suggestion.

Hope all that makes sense.

Does anyone know how to do this?

Edit

function HOOK_preprocess_entity (&$variables) {


    if (something_is_not_true()) {
        drupal_set_message('Some error message', 'error');
        
        // somehow don't render the template here

        // current workaround
        $variables['theme_hook_suggestion'] = 'error_helper';

        return;
    }

    // normal preprocess stuff
}