如何从一个节点获取第一个图像字段而不知道该字段的名称? [英] How to get the first image field from a node without knowing the field's name?

查看:145
本文介绍了如何从一个节点获取第一个图像字段而不知道该字段的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的网站使用许多内容类型,几乎所有的内容都使用一个或多个图像字段。
图像字段不在内容类型之间共享,所以它们的数量很多。

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.

推荐答案

<?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] )。

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!)

这篇关于如何从一个节点获取第一个图像字段而不知道该字段的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆