Skip to content

Instantly share code, notes, and snippets.

@larowlan
Created January 7, 2014 22:02
Show Gist options
  • Save larowlan/8307742 to your computer and use it in GitHub Desktop.
Save larowlan/8307742 to your computer and use it in GitHub Desktop.
Blocks before plugins
<?php
/**
* Implements hook_block_info().
*/
function forum_block_info() {
$blocks['active'] = array(
'info' => t('Active forum topics'),
'cache' => DRUPAL_CACHE_CUSTOM,
'properties' => array('administrative' => TRUE),
);
$blocks['new'] = array(
'info' => t('New forum topics'),
'cache' => DRUPAL_CACHE_CUSTOM,
'properties' => array('administrative' => TRUE),
);
return $blocks;
}
/**
* Implements hook_block_configure().
*/
function forum_block_configure($delta = '') {
$form['forum_block_num_' . $delta] = array(
'#type' => 'select',
'#title' => t('Number of topics'),
'#default_value' => variable_get('forum_block_num_' . $delta, '5'),
'#options' => drupal_map_assoc(range(2, 20))
);
return $form;
}
/**
* Implements hook_block_save().
*/
function forum_block_save($delta = '', $edit = array()) {
variable_set('forum_block_num_' . $delta, $edit['forum_block_num_' . $delta]);
}
/**
* Implements hook_block_view().
*
* Generates a block containing the currently active forum topics and the most
* recently added forum topics.
*/
function forum_block_view($delta = '') {
$query = db_select('forum_index', 'f')
->fields('f')
->addTag('node_access');
switch ($delta) {
case 'active':
$title = t('Active forum topics');
$query
->orderBy('f.last_comment_timestamp', 'DESC')
->range(0, variable_get('forum_block_num_active', '5'));
break;
case 'new':
$title = t('New forum topics');
$query
->orderBy('f.created', 'DESC')
->range(0, variable_get('forum_block_num_new', '5'));
break;
}
$block['subject'] = $title;
// Cache based on the altered query. Enables us to cache with node access enabled.
$block['content'] = drupal_render_cache_by_query($query, 'forum_block_view');
$block['content']['#access'] = user_access('access content');
return $block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment