drupal区块的创建和调用

来源:互联网 发布:淘宝上的淘抢购在哪里 编辑:程序博客网 时间:2024/05/06 20:29
drupal中页面划分为区块和内容,在page.tpl.php或者page--slh-linghang--slh-linghang-list.tpl.php中的页面调用创建的区块 <div class='test'>
                <?php
               //调用相应的区块
                $block_slh_linghang = module_invoke('slh_linghang', 'block_view', 'slh_linghang_block');
                print render($block_slh_linghang['content']);

                ?>
            </div>
            //page--slh-linghang--slh-linghang-list.tpl.php页面的中间的内容

          <?php print render($page['content']); ?>

区块的定义:/**
 * 定义区块
 * @return multitype:multitype:string boolean
 */
function slh_linghang_block_info() {

  $blocks = array();

  $blocks ['slh_linghang_block'] = array (
      'info' => 'slh_linghang block',
      'status' => FALSE,
      'cache' => DRUPAL_NO_CACHE,
      'region' => 'footer',
  );


  return $blocks;

}

/**
 * Implements hook_block_view().
 * 显示区块
 * This hook generates the contents of the blocks themselves.
 */
function slh_linghang_block_view($delta = '') {

  switch ($delta) {

    case 'slh_linghang_block' :

      $block ['subject'] = 'slh_linghang_block的标题';
      $block ['content'] = slh_linghang_contents ( $delta );
      break;

  }

  return $block;

}

/**
 * A module-defined block content function.
 */
function slh_linghang_contents($which_block) {

  switch ($which_block) {

    case 'slh_linghang_block':
    $sql="select * from node n left join field_data_field_og_catalog f on n.nid=f.entity_id";
    $sql.=" WHERE f.field_og_catalog_tid=14 order by n.created DESC LIMIT 3";

    $result = db_query($sql)->fetchAll();
    $vars=array();
    // dpm($result);
    // var_dump($result);
    foreach($result as $v){
      $node=node_load($v->nid);
       $fid = $node->field_qiniu_fid ['und'] [0] ['value'];
        $file = file_load($fid);
        $f_uri = $file->uri;
      // dpm($node);
      $vars[]=array(
        'nid'=>$node->nid,
        'uid'=>$node->uid,
        'title'=>$node->title,
        'content'=>drupal_substr($node->body['und'][0]['value'],0,100),
        'pic-uri' => $f_uri,
        'user'=>$node->name,
        );
    }
   //将变量在theme模板中显示出来
    return theme('slh_linghang_block',array('vars'=>$vars));

  }


1 0
原创粉丝点击