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?


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';


    // normal preprocess stuff

Edit 2

To answer @BeeBee - I'm using the Bean module and have a custom block type with an entity reference field.

When we develop and add a new content type to be selectable in the entity reference field, nodes of that content type have to get their data parsed/formatted a certain way to use a certain template file. There is no fallback and we don't want one as it would make the front end look bad for the end user. It's really an all or nothing type deal.

So if I or Another Developer have not set this data parse/formatter up for the newly supported content type yet then I'd like to not render the block and show a helpful error message to the developer as to what he/she has to do to get this working.

So the condition is if the referenced entity is not one of the explicitly stated supported content types then don't render the block (well I guess bean entity) and set an error message.

  • Can you be a bit more specific of exactly what you're trying to achieve? Are you trying to disable a whole entity from being rendered, or a specific part of it? Do you care about caching? What's the typical context or condition when said thing is disabled/not rendered? Are you manipulating a custom entity or Node? – Beebee Nov 2 '17 at 15:42
  • Without knowing the above things I asked, I could point you here: hide()This function hides elements from being rendered. – Beebee Nov 2 '17 at 15:44
  • @Beebee added more context above under "Edit 2". Am looking into hide(). Thanks for your help! Greatly appreciated – John_911 Nov 2 '17 at 15:56
  • Have a look at hook_block_view_alter (You'll need to determine that you're viewing a Bean block of a certain type). I don't have access to D7 so can't test easily. Try setting $data['content']['#access'] = FALSE; if your condition's fulfilled, otherwise you may just want to look at hook_block_list_alter and unset() your block there. – Beebee Nov 2 '17 at 17:05
  • Also related: How to disable/hide /unset a block programatically? – Beebee Nov 2 '17 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.