The site I am working on uses many content types and almost all of them use one or more image fields.
Image fields are not shared between content types, so there is a big number of them.
What I need is to get the first image field from a node, assuming there are more image fields linked to a node and without knowing the name of any of these fields. The first one is considered to be the one with a lower weight
.
This should build you an array of the "lightest" imagefield per content type.
<?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.
*/
Then when you load a node of, say, the "product" content type, you know which imagefield is the lightest (i.e $node->$lightest_imagefields[$node->type]
).
Hope it helps. (test it before using it, though!)