在Drupal网站有时候有多个内容类型之间需要互相连接,例如内容类型company中的字段field_address,需要查找内容类型location中field_street相同的node,然后在company的显示模板中field_address做一个指向location中这个node的链接。
在Drupal 7中可以通过Entity查询来实现,不过因为location中的field_street可能有很多是重复的,我们只能取其一,就可以在查询中限制只找到第一个range(0,1),具体代码如下:
$address = $field_address[0]['value'];
$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'location')
->fieldCondition('field_street', 'value', $address, '=')
->propertyCondition('status', 1)
->range(0,1)
->execute();
if (!empty($entities['node'])) {
$nid_array = array_keys($entities['node']);
$nid_location = array_shift($nid_array);
}
$address_display = l($address,"node/".$nid_location);
上面是在node--company.tpl.php中的一部分代码,最后实现了指向location内容类型中field_street中内容等于$address的node的链接。更多资料可以在Google中搜索得到。
评论