www.tvdrupal.com | Drupal中文视频教程 文档源码
Lesson20 drupal7模块开发1---入门篇,制作一个节点列表区 current_posts.info name = Current Posts description = A block module that lists links to recent posts. core = 7.x -------------- current_posts.module ' . t("Displays links to nodes created on this date") . ''; break; } } /** * Implements hook_block_info(). */ function current_posts_block_info() { $blocks['current_posts'] = array( // The name that will appear in the block list. 'info' => t('Current posts'), // Default setting. 'cache' => DRUPAL_CACHE_PER_ROLE, ); return $blocks; } /** * Custom content function. * * Set beginning and end dates, retrieve posts from database * saved in that time period. * * @return * A result set of the targeted posts. */ function current_posts_contents(){ //Get today's date. $today = getdate(); //Calculate the date a week ago. $start_time = mktime(0, 0, 0,$today['mon'],($today['mday'] - 7), $today['year']); //Get all posts from one week ago to the present. $end_time = time(); //Use Database API to retrieve current posts. $query = db_select('node', 'n') ->fields('n', array('nid', 'title', 'created')) ->condition('status', 1) //Published. ->condition('created', array($start_time, $end_time), 'BETWEEN') ->condition('type', 'article') //type. ->orderBy('created', 'DESC') //Most recent first. ->execute(); return $query; } /** * Implements hook_block_view(). * * Prepares the contents of the block. */ function current_posts_block_view($delta = '') { switch ($delta) { case 'current_posts': $block['subject'] = t('Current posts'); if (user_access('access content')) { // Use our custom function to retrieve data. $result = current_posts_contents(); // Array to contain items for the block to render. $items = array(); // Iterate over the resultset and format as links. foreach ($result as $node) { $items[] = array( 'data' => l($node->title, 'node/' . $node->nid), ); } // No content in the last week. if (empty($items)) { $block['content'] = t('No posts available.'); } else { // Pass data through theme function. $block['content'] = theme('item_list', array( 'items' => $items)); } } return $block; } } ?>
power by
tvdrupal.com
欢迎加入drupal主题会员