aggregator_block($op = 'list', $delta = '', $edit = array())
drupal/modules/aggregator/aggregator.module, line 300
Implementation of hook_block().
Generates blocks for the latest news items in each category and feed.
function aggregator_block($op = 'list', $delta = '', $edit = array()) {
if (user_access('access news feeds')) {
if ($op == 'list') {
$result = db_query('SELECT cid, title FROM {aggregator_category} ORDER BY title');
foreach ($result as $category) {
$block['category-' . $category->cid]['info'] = t('!title category latest items', array('!title' => $category->title));
}
$result = db_query('SELECT fid, title FROM {aggregator_feed} WHERE block <> 0 ORDER BY fid');
foreach ($result as $feed) {
$block['feed-' . $feed->fid]['info'] = t('!title feed latest items', array('!title' => $feed->title));
}
}
elseif ($op == 'configure') {
list($type, $id) = explode('-', $delta);
if ($type == 'category') {
$value = db_query('SELECT block FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $id))->fetchField();
$form['block'] = array(
'#type' => 'select',
'#title' => t('Number of news items in block'),
'#default_value' => $value,
'#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20))
);
return $form;
}
}
elseif ($op == 'save') {
list($type, $id) = explode('-', $delta);
if ($type == 'category') {
db_merge('aggregator_category')
->key(array('cid' => $id))
->fields(array('block' => $edit['block']))
->execute();
}
}
elseif ($op == 'view') {
list($type, $id) = explode('-', $delta);
switch ($type) {
case 'feed':
if ($feed = db_query('SELECT fid, title, block FROM {aggregator_feed} WHERE block <> 0 AND fid = :fid', array(':fid' => $id))->fetchObject()) {
$block['subject'] = check_plain($feed->title);
$result = db_query_range("SELECT * FROM {aggregator_item} WHERE fid = :fid ORDER BY timestamp DESC, iid DESC", array(':fid' => $id), 0, $feed->block);
$read_more = theme('more_link', url('aggregator/sources/' . $feed->fid), t("View this feed's recent news."));
}
break;
case 'category':
if ($category = db_query('SELECT cid, title, block FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $id))->fetchObject()) {
$block['subject'] = check_plain($category->title);
$result = db_query_range('SELECT i.* FROM {aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON ci.iid = i.iid WHERE ci.cid = :cid ORDER BY i.timestamp DESC, i.iid DESC', array(':cid' => $category->cid), 0, $category->block);
$read_more = theme('more_link', url('aggregator/categories/' . $category->cid), t("View this category's recent news."));
}
break;
}
$items = array();
foreach ($result as $item) {
$items[] = theme('aggregator_block_item', $item);
}
// Only display the block if there are items to show.
if (count($items) > 0) {
$block['content'] = theme('item_list', $items) . $read_more;
}
}
if (isset($block)) {
return $block;
}
}
}