使用 Drupal 访问节点中字段集合的数组值?
Accessing array values of a field collection in a node with Drupal?
请与最近使用 Drupal 的用户交流。
我想用 Drupal 站点上字符串 "url" 的所有示例创建一个数组。
我以前使用方法 "field_get_items" 来做一些非常相似的事情,但我现在试图访问一个深入节点数组的多个级别的字段集合,但我不确定该方法会工作。
$website_urls = array();
$faculty_members = field_get_items('node', $node, 'field_faculty_member');
for ($i = 0; $i < count($faculty_members); $i++) {
$value = field_view_value('node', $node, 'field_faculty_member', $faculty_members[$i]);
$field_collection = $value['entity']['field_collection_item'][key($value['entity']['field_collection_item'])];
$website_urls[] = render($field_collection['field_link']['#items'][0]['url']);
}
一个 url 位置的示例是...
['field_faculty_program'][0]['entity']['field_collection_item'][1842]['field_faculty_member'][0]['entity'][ 'field_collection_item'][1843]['field_link']['#items'][0]['url']
..还有一个...
['field_faculty_program'][4]['entity']['field_collection_item'][1854]['field_faculty_member'][0]['entity'][ 'field_collection_item'][1855]['field_link']['#items'][0]['url']
我应该使用什么方法来收集所有 'url' 个字符串以放置在数组中?
您实际上仍然可以使用 field_get_items() 函数,但最终将它传递给 'field_collection_item' 而不是节点类型。
像这样的东西应该可以工作:
if ($items = field_get_items('node', $node, 'field_faculty_member')) {
//loop through to get the ids so we can take
//advantage of field_collection_item_load_multiple for
//greater efficiency
$field_collection_item_ids = array();
foreach ($items as $item) {
$field_collection_item_ids[] = $item['value'];
}
if ($field_collection_items = field_collection_item_load_multiple($field_collection_item_ids)) {
foreach ($field_collection_items as $subitem) {
//now we load the items within the field collection
if ($items = field_get_items('field_collection_item', $subitem, 'field_faculty_member')) {
//And you can then repeat to go deeper and deeper
//e.g. a field collection item within a field collection
//for instance to get the urls within your faculty members
//item. Best to break this into functions or a class
//to keep your code readable and not have so many nested
//if statements and for loops
}
}
}
}
希望对您有所帮助!
斯科特
请与最近使用 Drupal 的用户交流。
我想用 Drupal 站点上字符串 "url" 的所有示例创建一个数组。
我以前使用方法 "field_get_items" 来做一些非常相似的事情,但我现在试图访问一个深入节点数组的多个级别的字段集合,但我不确定该方法会工作。
$website_urls = array();
$faculty_members = field_get_items('node', $node, 'field_faculty_member');
for ($i = 0; $i < count($faculty_members); $i++) {
$value = field_view_value('node', $node, 'field_faculty_member', $faculty_members[$i]);
$field_collection = $value['entity']['field_collection_item'][key($value['entity']['field_collection_item'])];
$website_urls[] = render($field_collection['field_link']['#items'][0]['url']);
}
一个 url 位置的示例是...
['field_faculty_program'][0]['entity']['field_collection_item'][1842]['field_faculty_member'][0]['entity'][ 'field_collection_item'][1843]['field_link']['#items'][0]['url']
..还有一个...
['field_faculty_program'][4]['entity']['field_collection_item'][1854]['field_faculty_member'][0]['entity'][ 'field_collection_item'][1855]['field_link']['#items'][0]['url']
我应该使用什么方法来收集所有 'url' 个字符串以放置在数组中?
您实际上仍然可以使用 field_get_items() 函数,但最终将它传递给 'field_collection_item' 而不是节点类型。
像这样的东西应该可以工作:
if ($items = field_get_items('node', $node, 'field_faculty_member')) {
//loop through to get the ids so we can take
//advantage of field_collection_item_load_multiple for
//greater efficiency
$field_collection_item_ids = array();
foreach ($items as $item) {
$field_collection_item_ids[] = $item['value'];
}
if ($field_collection_items = field_collection_item_load_multiple($field_collection_item_ids)) {
foreach ($field_collection_items as $subitem) {
//now we load the items within the field collection
if ($items = field_get_items('field_collection_item', $subitem, 'field_faculty_member')) {
//And you can then repeat to go deeper and deeper
//e.g. a field collection item within a field collection
//for instance to get the urls within your faculty members
//item. Best to break this into functions or a class
//to keep your code readable and not have so many nested
//if statements and for loops
}
}
}
}
希望对您有所帮助!
斯科特