如何访问字段集合中的项目(Drupal 7)

时间:2017-01-20 18:12:23

标签: php arrays loops drupal drupal-7

我正在尝试在字段集合中的数组中访问图像uri但我不知道如何访问它们。如果我查看devel模块,我可以看到数组中第一个图像uri的位置如下所示:

['field_text_and_image'][0]['entity']['field_collection_item'][2474]['field_about_accreditation_image'][0]['#item']['uri']

我很困惑,因为我有' field_get_items'访问我正在瞄准的字段集合的方法......

$text_and_image_field = field_get_items('node', $node, 'field_text_and_image');

...如果我打印/渲染这个变量,我希望在页面上看到一个数组,但是没有任何东西被创建。但是,我在页面上做了一个条件,检查' $ text_and_image_field' $ text_and_image_field'字段集合存在,如果它确实创建了一个元素,它确实创建了一个显示该字段存在的元素。我似乎无法访问它的任何内容。

那么,为什么现场集合不打印任何东西,我怎样才能遍历' field_about_accreditation'数组打印出所有图像uri的?

编辑*

我已经对这个问题采取了一些措施,并意识到我犯了一个错误,就是试图渲染' $ text_and_image_field'什么时候我应该使用print_r,现在这样做给了我这个数组值:

Array ( [0] => Array ( [value] => 2474 [revision_id] => 174439 ) )

基于访问字段集合的旧代码,每隔一次为字段集合分配值时发生的事情是编写以下语句:

$value = field_view_value('node', $node, '$field_text_and_image', $text_and_image_field[$i]);
$field_collection = $value['entity']['field_collection_item'][key($value['entity']['field_collection_item'])];

然而,当我尝试打印出$ value(我预期会是' 2474')时,不会显示任何内容。

2 个答案:

答案 0 :(得分:0)

Hi字段集合存储为单独的实体,因此您需要执行实体加载以获取数据。

$text_and_image_field = entity_load('field_collection_item', array($entity_id));

在这种情况下,看起来实体id是2474,你可以根据需要操作数组。

你的结束代码可能是这样的:

$imageuri = entity_load('field_collection_item', array(2474))[2474]->field_about_accreditation_image['und'][0]['uri'];

答案 1 :(得分:0)

让'假设您已加载$node,并且其中包含字段集合字段field_images。然后你可以得到它的第一个元素:

$collection_entity_id = $node->field_photo['und'][0]['value'];

现在你有了实体ID,你必须加载完整的实体:

$full_entity = field_collection_item_load($collection_entity_id);

你加载实体后,你可以通常的方式访问实体字段:

$title = $full_box->title;
$image_path =- $full_entity->field_image_path['und'][0]['value'];

是的,我知道这不是获取价值的最简洁的方法,请不要揉搓我的脸。