我正在开发的网站使用许多内容类型,并且几乎所有内容类型都使用一个或多个图像字段。图像字段在内容类型之间不共享,因此它们的数量很大。
我需要的是从节点获取第一个图像字段,假设有更多图像字段链接到节点并且不知道任何这些字段的名称。第一个被认为是较低的weight
。
我正在开发的网站使用许多内容类型,并且几乎所有内容类型都使用一个或多个图像字段。图像字段在内容类型之间不共享,因此它们的数量很大。
我需要的是从节点获取第一个图像字段,假设有更多图像字段链接到节点并且不知道任何这些字段的名称。第一个被认为是较低的weight
。
这应该为您构建每个内容类型的“最轻”图像字段数组。
<?php
module_load_include('inc', 'content', 'includes/content.node_form');
$content_types = array('page', 'story', 'product', 'some_content_type');
$lightest_imagefields = array(); // arranged by content type
foreach ($content_types as $content_type_name) {
$content_type_data = content_types($content_type_name);
$last_weight = NULL;
foreach ($content_type_data['fields'] as $field_name => $field_data) {
if ($field_data['widget']['type'] == 'imagefield_widget' && (is_null($last_weight) || (int)$field_data['widget']['weight'] < $last_weight)) {
$lightest_imagefields[$content_type_name] = $field_name;
$last_weight = (int)$field_data['widget']['weight'];
}
}
}
/** Hypothetical Usage:
* $node = load_some_node_i_want();
* $node->$lightest_imagefields[$node->type]; // Access this node's lightest imagefield.
*/
然后,当您加载“产品”内容类型的节点时,您知道哪个图像字段最轻(即$node->$lightest_imagefields[$node->type]
)。
希望能帮助到你。(不过,在使用之前先测试一下!)