欢迎各位兄弟 发布技术文章
这里的技术是共享的
Privatemsg(本文所用的是7.x-1.3 版本) 是一个站内私信模块,功能非常强大,而且还提供了不少钩子和API供用户扩展。我在一个咨询的项目中使用了这个模块作为站内短信咨询,用户提问时,需要一个选项就是:是否公开咨询的问题。而 Privatemsg模块本身支持字段扩展,所以添加这个字段并不难,下面步骤:
打开admin/config/messaging/privatemsg/fields,添加一个布尔字段,字段为:是否公开、机读名为 public、字段类型为“单开/关复选框”(不知是谁翻译的,费解!!),然后点击“保存”。
打开 admin/config/messaging/privatemsg/display ,将这个字段全部隐藏起来,因为我们是想用它作为一个标识,并不想在查看显示。
到此,字段就添加成功了,现在我想在首页上显示所有公开的咨询,那怎么办呢?查看了Privatemsg API 文档,尝试使用钩子注入查询语句不成功,于是就复制了privatemsg_sql_list函数直接修改了,修改后的代码如下:
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 | function privatemsg_extend_sql_list( $account , $argument = 'list' ) { $query = db_select( 'pm_message' , 'pm' )->extend( 'TableSort' )->extend( 'PagerDefault' ); $query ->join( 'pm_index' , 'pmi' , 'pm.mid = pmi.mid' ); //扩展的字段,检查是否公开 $query ->join( 'field_data_field_public' , 'fdp' , 'pm.mid = fdp.entity_id' ); $query ->condition( 'fdp.field_public_value' , 1); //扩展的字段 end // Create count query; $count_query = db_select( 'pm_message' , 'pm' ); $count_query ->addExpression( 'COUNT(DISTINCT pmi.thread_id)' , 'count' ); $count_query ->join( 'pm_index' , 'pmi' , 'pm.mid = pmi.mid' ); $count_query // ->condition('pmi.recipient', $account->uid) ->condition( 'pmi.type' , array ( 'hidden' , 'user' )) ->condition( 'pmi.deleted' , 0); $query ->setCountQuery( $count_query ); // Required columns $query ->addField( 'pmi' , 'thread_id' ); $query ->addExpression( 'MIN(pm.subject)' , 'subject' ); $query ->addExpression( 'MAX(pm.timestamp)' , 'last_updated' ); $query ->addExpression( 'MAX(pm.has_tokens)' , 'has_tokens' ); $query ->addExpression( 'SUM(pmi.is_new)' , 'is_new' ); // Load enabled columns $fields = array_filter (variable_get( 'privatemsg_display_fields' , array ( 'participants' ))); if (in_array( 'count' , $fields )) { // We only want the distinct number of messages in this thread. $query ->addExpression( 'COUNT(distinct pmi.mid)' , 'count' ); } if (in_array( 'participants' , $fields )) { // Query for a string with uids, for example "1,6,7". This needs a subquery on PostgreSQL. if (db_driver() == 'pgsql' ) { $query ->addExpression("array_to_string( array (SELECT DISTINCT pmia.type || '_' || pmia.recipient FROM {pm_index} pmia WHERE pmia.type <> 'hidden' AND pmia.thread_id = pmi.thread_id AND pmia.recipient <> :current), ',' )", 'participants' , array ( ':current' => $account ->uid)); } else { $query ->addExpression("(SELECT GROUP_CONCAT(DISTINCT CONCAT(pmia.type, '_' , pmia.recipient)) FROM {pm_index} pmia WHERE pmia.type <> 'hidden' AND pmia.thread_id = pmi.thread_id AND pmia.recipient <> :current)", 'participants' , array ( ':current' => $account ->uid)); } } if (in_array( 'thread_started' , $fields )) { $query ->addExpression( 'MIN(pm.timestamp)' , 'thread_started' ); } return $query //->condition('pmi.recipient', $account->uid) ->condition( 'pmi.type' , array ( 'hidden' , 'user' )) ->condition( 'pmi.deleted' , 0) ->groupBy( 'pmi.thread_id' ) ->orderByHeader(_privatemsg_list_headers( array_merge ( array ( 'subject' , 'last_updated' ), $fields ))) ->limit(variable_get( 'privatemsg_per_page' , 25)); } |
来自 http://www.cuitu.net/content/ru-he-wei-privatemsg-de-xiao-xi-tian-jia-zi-duan-bing-xian-shi