How to use modules in Drupal

来源:互联网 发布:为知笔记 密码 编辑:程序博客网 时间:2024/05/20 06:54

When you finish a series of module actions: download, install and enable; you would wonder how front-end UI accesses or uses your installed modules. From Drupal's codes, i know that there are two ways: one is through menu, the other is by block.

1. Menu Way

function blog_menu() {  $items['blog'] = array(    'title' => 'Blogs',    'page callback' => 'blog_page_last',    'access arguments' => array('access content'),    'type' => MENU_SUGGESTED_ITEM,    'file' => 'blog.pages.inc',  );}
You can make a menu in the front page and call blog_page_last when you access it. Referring to following code, blog_page_last returns array-type data.

function blog_page_last() {  global $user;  $build = array();  $query = db_select('node', 'n')->extend('PagerDefault');  $nids = $query    ->fields('n', array('nid', 'sticky', 'created'))    ->condition('type', 'blog')    ->condition('status', 1)    ->orderBy('sticky', 'DESC')    ->orderBy('created', 'DESC')    ->limit(variable_get('default_nodes_main', 10))    ->addTag('node_access')    ->execute()    ->fetchCol();  if (!empty($nids)) {    $nodes = node_load_multiple($nids);    $build += node_view_multiple($nodes);    $build['pager'] = array(      '#theme' => 'pager',      '#weight' => 5,    );  }  else {    drupal_set_message(t('No blog entries have been created.'));  }  drupal_add_feed('blog/feed', t('RSS - blogs'));  return $build;}
If callback function returns html markup data, it will show directly. If the page callback result is array-type data, it will use drupal_delivery_page method to render the content.

 $page_callback_result = call_user_func_array($router_item['page_callback'], $router_item['page_arguments']);
drupal_deliver_page($page_callback_result, $default_delivery_callback);
Let us examine drupal_deliver_page method's codes

function drupal_deliver_page($page_callback_result, $default_delivery_callback = NULL) {  if (!isset($default_delivery_callback) && ($router_item = menu_get_item())) {    $default_delivery_callback = $router_item['delivery_callback'];  }  $delivery_callback = !empty($default_delivery_callback) ? $default_delivery_callback : 'drupal_deliver_html_page';  // Give modules a chance to alter the delivery callback used, based on  // request-time context (e.g., HTTP request headers).  drupal_alter('page_delivery_callback', $delivery_callback);  if (function_exists($delivery_callback)) {    $delivery_callback($page_callback_result);  }  else {    // If a delivery callback is specified, but doesn't exist as a function,    // something is wrong, but don't print anything, since it's not known    // what format the response needs to be in.    watchdog('delivery callback not found', 'callback %callback not found: %q.', array('%callback' => $delivery_callback, '%q' => $_GET['q']), WATCHDOG_ERROR);  }}
The default callback is drupal_deliver_html_page, but there provides two options: delivery_callback and page_delivery_callback. delivery_callback is used in the menu definition and page_delivery_callback is for alter hook.

2 Block way

You can assign the block to one region and the block content will show in the region correspondingly.

function blog_block_view($delta = '') {  global $user;  if (user_access('access content')) {    $result = db_select('node', 'n')      ->fields('n', array('nid', 'title', 'created'))      ->condition('type', 'blog')      ->condition('status', 1)      ->orderBy('created', 'DESC')      ->range(0, variable_get('blog_block_count', 10))      ->addTag('node_access')      ->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' => 'more_link',        '#url' => 'blog',        '#title' => t('Read the latest blog entries.'),      );      return $block;    }  }}

The regions are defined in the theme info files and can be managed by admin in the structure block area. the content is rendered by theme function hook_more_link.

if (isset($elements['#theme'])) {    $elements['#children'] = theme($elements['#theme'], $elements);  }
  $output = $prefix . $elements['#children'] . $suffix;



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 调制解调器的灯一直闪怎么办 691宽带用户名和密码无效怎么办 房间里有狐臭味怎么办 吃冰冻水果胃特别痛怎么办 小米5s屏幕闪烁怎么办 系统重装重启了怎么办 华为p10开不了机怎么办 眼镜腿夹的头疼怎么办 摩托车车头锁坏了怎么办 夏天骑摩托车戴头盔热怎么办 头盔玻璃罩边固定老是掉怎么办 电动车不戴头盔被扣车怎么办 郴州骑电动车没戴头盔怎么办 配置数据源时发现两个版本怎么办 微信占用内存3g怎么办 打印机显示内存已满怎么办 网页显示代理服务器连接失败怎么办 墙内线路断了怎么办 墙里的电线坏了怎么办 鱼竿最前端断了怎么办 下雨天墙壁与管道间漏水怎么办 电饭锅的电线被雨淋了怎么办 钢琴跨八度手短怎么办 弹钢琴手指不灵活怎么办呢? 理发剪不锋利了怎么办 室外宽带线断了怎么办 接宽带光纤太短怎么办 装修光纤网线太短怎么办 宽带入户线断了怎么办 电信有无线没网怎么办 墙里的网线断了怎么办 墙里网线断了怎么办 3根网线断了怎么办 剪了层次的头发怎么办 小米6充电线坏了怎么办 小米6导航信号弱怎么办 麦多多充不了电怎么办 一加数据线坏了怎么办 小米耳机泡水了怎么办 公司拖欠工资公司破产了怎么办 苹果x外壳掉漆怎么办