I need to show a block if a field has a given state, or print nothing if it has a different state. I can get it to work using a field template with the code below, but printing useless html just to stop it from rendering it's default output seems more than a little hacky.

I've tried setting $element = null; - that didn't work. I tried changing #markup to be empty, that didn't work. Even using print ''; means Drupal kicks it's default rendering back in.

Is there a recommended method for telling a field to do nothing?

<?php if($element['#items'][0]['value'] === 1){
    //This is an archive page - show the block.
    $block = module_invoke('customblocks', 'block_view','archive');
    print render($block['content']);
    } else {
        //nothing to do here - not an archive page
        //The only way to stop Drupal rendering something appears to be to get it to print SOMETHING.
        //So we're going to print an invisible hr.
        print '<hr style="display:none;" />';

This tutorial might be helpful http://akyl.net/how-extract-and-theme-drupal-7-fields-out-renderpagecontent

Your Answer

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

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