自己写模块实现分类

步骤1,添加模块,名称叫mytaxonomy,建立5个文件,分别是

 

  • mytaxonomy.module --- 模块hook
  • mytaxonomy.info --- 模块信息
  • mytaxonomy.pages.inc --- 函数
  • product.tpl.php --- 产品模版
  • catetory_block.tpl.php --- 分类block 模版

 

步骤2,mytaxonomy.info 文件:

1
2
3
4
5
name = Drupalla
description = 猪跑啦.
core = 7.x
package = Drupalla
files[] = mytaxonomy.module

 

 

步骤3,mytaxonomy.module 文件,增加5个hook,分别是

 

  • hook_menu:定义一个变量路径叫 category/%
  • hook_theme:指向列表名为mytaxonomy,就能自动用product.tpl.php跟catetory_block.tpl.php模版文件
  • hook_permission:指定这个category/% 路径的访问权限
  • hook_block_info:建立一个左侧的sidebar block 的信息
  • hook_block_view:建立一个左侧的sidebar block 的内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
  * Implements hook_menu().
  */
function  mytaxonomy_menu() {
   // Admin settings for the site.
   $items [ 'category/%' ] = array (
     'title' => '分类' ,
     'description' => '分类' ,
     'page callback' => 'show_product' , //指定函数
     'page arguments' => array (1),
     'file' => 'mytaxonomy.pages.inc' , //指定函数的所在文件
     'access arguments' => array ( 'view product' ),
     'access callback' => TRUE,
     'type' => MENU_NORMAL_ITEM,
   );
   return $items ;
}
 
function mytaxonomy_theme(){
     return array (
     'product' => array (
         'template' => 'product' ,
     'variables' => array ()
     ),
     'catetory_block' => array (
         'template' => 'catetory_block' ,
     'variables' => array ()
     ),
     );
}
function mytaxonomy_permission() {
   return array (
     'view product' =>  array (
       'title' => t( 'View product' ),
     ),
   );
}
 
 
 
/**
  * Implements hook_block_info().
  */
function mytaxonomy_block_info() {
   $blocks [ 'catetory_block' ] = array (
     'info' => t( 'Category' ),
     'cache' => DRUPAL_NO_CACHE,
   ); //如有多个分类,就写多个block,复制多份
   return $blocks ;
}
/**
  * Implements hook_block_view().
  */
function mytaxonomy_block_view( $block_name = '' ) {
   if ( $block_name == 'catetory_block' ) {
     $content = show_catetory(); // 定义block的函数
     $block = array (
       'subject' => t( 'Category' ),
       'content' => $content ,
     );
     } //如有多个分类,就写多个block,复制多份
   return $block ;
}
 
 
 
function show_catetory() //只提供大概db_select 写法。根据自己改写
{  
     $output = '' ;
     $query = db_select( 'taxonomy_term_data' , 'td' );
     $query ->leftJoin( 'taxonomy_term_hierarchy' , 'th' , 'td.tid = th.tid' );
     $query ->fields( 'td' )->fields( 'th' , array ( 'parent' ))
         ->condition( 'td.vid' , '2' )->condition( 'th.parent' , 0)->orderBy( 'weight' , 'ASC' )->orderBy( 'tid' , 'DESC' );
     $query = $query ->execute()->fetchAll();
     $rows = array ();
     foreach ( $query as $ob )
     {
         $row = array ();
         $row [ 'tid' ] = $ob -> tid;
         $row [ 'name' ] = l( $ob -> name, 'category/' . $ob -> tid); //指定路径到 hook_menu 中的$items['category/%']
         $p = db_select( 'taxonomy_term_hierarchy' , 't' )->fields( 't' )->condition( 't.parent' , $ob -> tid)->execute();
         $row [ 'num_of_results' ] = ' (' . $p ->rowCount(). ')' ; //分类下有多少个文章;
         $rows [] = $row ;
     }  
     $output .= theme( 'catetory_block' , array ( 'rows' => $rows )); //用catetory_block.tpl.php 模版
     return $output ;
}

 

步骤4,mytaxonomy.pages.inc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
 
function show_product( $tid ){
     //通过获取过来的tid写sql,写法如步骤三的function show_catetory()
     //注意 $output .= theme('product', array('rows' => $rows));//用product.tpl.php 模版
     $output = '' ;
     $query = db_select( 'field_data_field_manufacturers' , 'f' )->extend( 'PagerDefault' )->limit(50); //注意这行field_data_field_manufacturers是你的表
     $query ->fields( 'f' , array ( 'entity_id' ))
         ->condition( 'f.field_manufacturers_tid' , $tid )->orderBy( 'field_manufacturers_tid' , 'DESC' );
     $query = $query ->execute()->fetchAll();
     $rows = array ();
     foreach ( $query as $ob )
     {
         $row = array ();
         $node = node_load( $ob ->entity_id);
         //print_r($node);
         $row [ 'title' ] = l( $node ->title, 'node/' . $node ->nid);
         $image = "" ;
         if (! empty ( $node ->field_thumb)){ $image = '<img src="' .file_create_url( $node ->field_thumb[ 'und' ][0][ 'uri' ]). '">' ;}
         $row [ 'image' ] = $image ;
         $row [ 'author' ] = $node ->name;
         $row [ 'created' ] = $node ->created;
         $rows [] = $row ;    
     }
     $output .= theme( 'pager' );
     $output .= theme( 'product' , array ( 'rows' => $rows )); //用product.tpl.php 模版
     $output .= theme( 'pager' );
     return $output ;
}

 

步骤5,product.tpl.php跟catetory_block.tpl.php

只给出大概写法,参数得自己根据传过来的data变

product.tpl.php

1
2
3
4
5
<?php foreach ( $rows as $data ){?>
<div class = "productlist" >
   <?php print $data [ 'image' ] ?><div style= "height:50px;width:260px;" ><?php print $data [ 'title' ] ?></div>
</div>
<?php }?>

catetory_block.tpl.php

1
2
3
4
5
<ul>
<?php foreach ( $rows as $data ){?>
<li><?php print $data [ 'name' ] ?></li>
<?php }?>
</ul>

步骤6,安装模块,在block页,把模块所生成的block拉到想显示的区域里面。这里假使拖拉到左侧sidebar。

 

步骤7,清空缓存。打开网站,在sidebar会出现分类,分类模版是catetory_block.tpl.php。点击分类的每个项,地址的格式是category/%,进入后,显示此分类的产品,产品对应模版是product.tpl.php 。

转载于:https://www.cnblogs.com/ginikeer/p/3549339.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值