我在Drupal中有一个显示名称,徽标,电话号码和电话类型的视图。我已经创建了一个php脚本来从视图中提取它,以便我可以在Android应用程序中运行它并将数据拉入其中(作为JSON)我已成功创建了带有node_id和公司名称(标题)的JSON输出但是当我尝试获取手机信息时,我得到了空值,我附上了以下代码。
foreach($view->result AS $company) {
$companylist = array();
$data = $company->_field_data['nid']['entity'];
$companylist['company'] = $company->node_title;
$companylist['nid'] = $company->nid;
$data2 = field_get_items('node',$data,'field_data_field_companyphone_node_entity_type');
$phone = entity_load('field_collection_item',array($data2[LANGUAGE_NONE][0]['value']));
$companylist['type'] = $phone[0];
$companylist['phone'] =$phone[1];
$return[]=$companylist;
}
}
return $return;
}
答案 0 :(得分:0)
最后通过在我的代码中使用包装器找到了一个解决方案我还为字段集合的每个循环创建了一个内部,希望这将有助于将来更新的代码包含在下面
$fc_fields = field_get_items('node', $node, 'field_companyphone');
if (!empty($fc_fields)){
$field = field_view_value('node',$node, 'field_companyphone', $fc_fields[0]);
foreach ($field['entity']['field_collection_item'] as $id => $field_collection){
// load the field collection item entity
$field_collection_item = field_collection_item_load($id);
// wrap the entity and make it easier to get the values of fields
$field_wrapper = entity_metadata_wrapper('field_collection_item', $field_collection_item);
// all values from a field collection
$field_collection_phone_type_item = $field_wrapper->field_companyphone_type->value(); // an array of phone type data
$companylist['type'] = $field_collection_phone_type_item->name;
$companylist['phone'] = $field_wrapper->field_phone_phone->value();
}
}
$return[]=$companylist;