这些天在做系列网站的时候,遇到多个子网站、每个子网站都有多个内容类型,希望在首页展示内容类型、包含的字段名称以及页面数量,以前是人工来读取的,但网站多了就很麻烦,而且以后不便更新,这次用Drupal的API调用及SQL语句来实现了。
站内读取的例子:http://hangye.mingluji.com/it/,源代码:
<?php
$node_types=node_type_get_names();
print '<ul>';
foreach ($node_types as $node_type) {
if ($node_type!=='基本页面' && $node_type!=='文章') {
$query = "SELECT COUNT(*) amount FROM {node} n WHERE n.type = :type";
$result = db_query($query, array(':type' => $node_type))->fetch();
$amount= $result->amount;
$line= '<strong><li>'.l("列表$node_type","list/$node_type").'中有'.$amount.'条公司信息,包含字段:<br /></strong>';
$fields=field_info_instances("node", $node_type);
foreach ($fields as $key=>$field) {
//print "$key, $field";
$line.= $field['label'].'、';
}
$line.= '。</li>';
$line=str_replace('、。','。',$line);
print $line;
}
}
print '</ul>';
?>
站外读取的例子:http://hangye.mingluji.com/node/45,源代码:
<?php
$industry='it';
$other_database = array(
'database' => 'mingluji_hangye_'.$industry,
'username' => 'username',
'password' => 'password',
'host' => 'localhost',
'driver' => 'mysql',
);
Database::addConnectionInfo($industry, 'default', $other_database);
db_set_active($industry);
$query = "SELECT type FROM node_type WHERE disabled =0 ORDER BY (0+type) ASC";
$result = db_query($query)->fetchCol();
$node_types=$result;
//print_r ($node_types);
print '<ul>';
foreach ($node_types as $node_type) {
if ($node_type!=='page' && $node_type!=='article') {
$query = "SELECT COUNT(*) amount FROM {node} n WHERE n.type = :type";
$result = db_query($query, array(':type' => $node_type))->fetch();
$amount= $result->amount;
$line= '<strong><li>'.l("列表$node_type","http://hangye.mingluji.com/$industry/list/$node_type").'中有'.$amount.'条公司信息,包含字段:<br /></strong>';
$query = "SELECT data FROM field_config_instance WHERE bundle ='$node_type'";
$result = db_query($query)->fetchCol();
$fields=$result;
//print_r ($fields);
foreach ($fields as $key=>$field) {
$arr=unserialize($field);
$line.= $arr['label'].'、';
}
$line.= '。</li>';
$line=str_replace('、。','。',$line);
print $line;
}
}
print '</ul>';
db_set_active();
?>
调试页面:http://hangye.mingluji.com/kafei/node/2294
临时在网上找的一些资料拼凑起来的,可以用就行,程序写的很烂,时间紧,也没有写注释,望各位看官海涵!
评论