這些天在做系列網站的時候,遇到多個子網站、每個子網站都有多個内容類型,希望在首頁展示内容類型、包含的字段名稱以及頁面數量,以前是人工來讀取的,但網站多了就很麻煩,而且以後不便更新,這次用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
臨時在網上找的一些資料拼湊起來的,可以用就行,程序寫的很爛,時間緊,也沒有寫注釋,望各位看官海涵!
评论