2011-07-11 44 views
1

我正在使用的网站使用了许多内容类型,并且几乎所有这些内容类型都使用一个或多个图像字段。 图像字段不在内容类型之间共享,所以它们的数量很大。如何在不知道字段名称的情况下从节点获取第一个图像字段?

我需要的是从一个节点获取第一个图像域,假设有更多的图像域链接到一个节点并且不知道任何这些域的名称。第一个被认为是一个较低的weight

回答

1

这应该为您创建每个内容类型的“最亮”图像字段的数组。

<?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. 
*/ 

然后,当你加载的,比如说一个节点,“产品”的内容类型,你知道哪个的ImageField最轻的(即$node->$lightest_imagefields[$node->type])。

希望它有帮助。 (在使用它之前测试它,虽然!)

+0

其实我没有测试代码,但了解它的逻辑,所以我现在可以没有问题地构建自己的代码。谢谢! – ovi

相关问题