Drupal 如何在不知道字段的情况下从节点获取第一个图像字段';她叫什么名字?

Drupal 如何在不知道字段的情况下从节点获取第一个图像字段';她叫什么名字?,drupal,drupal-6,imagefield,Drupal,Drupal 6,Imagefield,我正在工作的站点使用许多内容类型,几乎所有内容类型都使用一个或多个图像字段。 图像字段不在内容类型之间共享,因此存在大量图像字段 我需要的是从一个节点获取第一个图像字段,假设有更多的图像字段链接到一个节点,并且不知道这些字段的名称。第一个被认为是具有较低权重的这应该为每个内容类型构建一个“最轻”的图像字段数组 <?php module_load_include('inc', 'content', 'includes/content.node_form'); $content_types =

我正在工作的站点使用许多内容类型,几乎所有内容类型都使用一个或多个图像字段。 图像字段不在内容类型之间共享,因此存在大量图像字段


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

这应该为每个内容类型构建一个“最轻”的图像字段数组

<?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数组

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

实际上,我没有测试代码,但是理解了它的逻辑,所以我现在可以毫无问题地构建自己的代码了。非常感谢。实际上,我没有测试代码,但是理解了它的逻辑,所以我现在可以毫无问题地构建自己的代码。非常感谢。