0

I have built a module that needs to provide a small piece of dynamic content on every page load of a certain content type. It works great, however the section of code I provided is always cached in Drupal theme registry (it seems). The Variables I need generated on each pageload are "$userpicturepath" and "$variables['userfullname']" How do I prevent this from being cached? For completeness, here is my module's code:

subscribe_blocks.module

//implementation of hook_block_info
function subscribe_blocks_block_info() {    
    $blocks['author_subscribe_block'] = array(
        'info' => t('Author Subscribe Block'),
        'cache' => DRUPAL_NO_CACHE,
    );
    return $blocks;
}

//implementation of hook_block_view
function subscribe_blocks_block_view($delta = '') {
    switch ($delta) {
        case 'author_subscribe_block':
            $blocks['content'] = theme('subscribe_blocks_block');
            break;
    }
    return $blocks;
}


// Implementation of hook_theme
function subscribe_blocks_theme(){
    if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
        $node = node_load(arg(1));
    }
    $user_fields = user_load($node->uid);

    return array(
        'subscribe_blocks_block' => array(
            'template' => 'subscribeblocks',
            'variables' => array( 
                'userfullname' => $user_fields->field_name['und'][0]['safe_value'],
                'userpicture' => $user_fields->picture->filename,
                'userfields' => $user_fields,
            ),
        ),
    );
}

subscribeblocks.tpl.php

<?php
drupal_page_is_cacheable(FALSE);

if ($variables['userpicture'] != NULL){
    $userpicturepath = 'sites/example.com/files/styles/user_profile/public/pictures/' . $variables['userpicture']; 
} else{
    $userpicturepath = 'https://someimgurl';
}
?>

<div class='subscribe-bar'>
    <div class='container'>
        <div class='col-sm-9 col-sm-offset-2'>
            <div class='subscribe-left'>
                <img src=<?php echo $userpicturepath; ?> class='subscribeuserpic' >
            </div>
            <div class='subscribe-right'>
                <p>Never miss another story by <strong><?php echo($variables['userfullname']); ?></strong>. Subscribe today.</p>
                <div class='slideInFormContainer'>
                    <?php
                    $block = module_invoke('webform', 'block_view', 'client-block-5401');
                    print render($block['content']);
                    ?>
                </div>
            </div>
        </div>
    </div>
</div>

I'm using Drupal 7, have all other caching turned off (for now), and have tried using several modules including CacheExclude (https://www.drupal.org/project/cacheexclude) and Ajax Blocks (https://www.drupal.org/project/ajaxblocks).

  • however the section of code I provided is always cached which section? based on the code's comments you provided above, I cannot tell. – No Sssweat Dec 19 '17 at 7:24
  • Good point, the cached items are $userpicturepath and $variables['userfullname'] – dougblitz210 Dec 20 '17 at 6:52
0

Probably not the best way to do this, but I was able to overcome this by rebuilding the theme cache when this block was run using:

<?php
drupal_theme_rebuild();

I'd love if someone could help with a better answer though!

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.