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
}
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.
hide()
This function hides elements from being rendered.hide()
. Thanks for your help! Greatly appreciatedhook_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 athook_block_list_alter
andunset()
your block there.