1

请与最近的 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']['#项目'][0]['url']

..还有另一个...

['field_faculty_program'][4]['entity']['field_collection_item'][1854]['field_faculty_member'][0]['entity']['field_collection_item'][1855]['field_link']['#项目'][0]['url']

我应该使用什么方法来收集所有“url”字符串以放置在数组中?

4

1 回答 1

0

您实际上仍然可以使用 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

      }

    }
  }

}

希望有帮助!

斯科特

于 2015-10-16T08:21:28.583 回答