How to use hook_theme and hook_block_view correctly The following code works - the block_view calls the theme function and the correct variables get passed.

Is this the right way to use these functions when you have a block and you want it themed with variables and using a specific template?

Thanks,

function youtube_pull_block_view($delta = '') {

    switch($delta){

      case 'youtube_pull':
      $block['content'] = theme('popular_analytics', array('videos' =&gt; get_most_popular()));    
      break;
    }

return $block;
}

function youtube_pull_theme() {

return array('youtube_pull_listing' =&gt; array(
             'variables' =&gt; array('videos' =&gt; NULL),
             'template' =&gt; 'youtube-pull-listing',));
}
That code would be perfectly fine, if you define a theme function and use that theme function. In your case, the code is defining the youtube_pull_listing theme function, but using the popular_analytics theme function.

As for an example of hook_block_view() implementation that uses a theme function, see user_block_view(), which contains the following code.

  if (user_access('access content')) {
    // Retrieve a list of new users who have subsequently accessed the site successfully.
    $items = db_query_range('SELECT uid, name FROM {users} WHERE status &lt;&gt; 0 AND access &lt;&gt; 0 ORDER BY created DESC', 0, variable_get('user_block_whois_new_count', 5))-&gt;fetchAll();
    $output = theme('user_list', array('users' =&gt; $items));

    $block['subject'] = t('Who\'s new');
    $block['content'] = $output;
  }
  return $block;


The fact the theme function is using a template file doesn't make any difference for how the function is called. The only difference is that the variables declared in hook_theme() are the variables the template file will be able to access.

Keep in mind that, in Drupal 7, it is now preferable to return an array in $block['content'], in the same way blog_block_view() does.

  if (user_access('access content')) {
    $result = db_select('node', 'n')
      -&gt;fields('n', array('nid', 'title', 'created'))
      -&gt;condition('type', 'blog')
      -&gt;condition('status', 1)
      -&gt;orderBy('created', 'DESC')
      -&gt;range(0, variable_get('blog_block_count', 10))
      -&gt;addTag('node_access')
      -&gt;execute();

    if ($node_title_list = node_title_list($result)) {
      $block['subject'] = t('Recent blog posts');
      $block['content']['blog_list'] = $node_title_list;
      $block['content']['blog_more'] = array(
        '#theme' =&gt; 'more_link',
        '#url' =&gt; 'blog',
        '#title' =&gt; t('Read the latest blog entries.'),
      );

      return $block;
    }
  }


The reasons why using theme() is bad are given in theme() renamed to _theme() and should not be called directly, even if that change record refers to Drupal 8.


It circumvents caching
It circumvents defaults of types defined in hook_element_info(), including attached assets
It circumvents the pre_render and post_render stages
It circumvents JavaScript states information