在Drupal获取一个自定义字段的值最常用的方法是:
$nianling = $node->field_nianling[LANGUAGE_NONE][0]['value'];
不过当field_nianling内容为空时,会出现类似这样的错误: Notice: Undefined index: value in eval() ,如果我们在在取值之前加个判断,如这样:
if (isset(field_nianling[LANGUAGE_NONE])) {
$nianling = $node->field_nianling[LANGUAGE_NONE][0]['value'];
}
是可以解决问题,但如果字段多时,代码看起来就不够简洁,而且如果自定义字段是列表字段,就有可能还需要根据列表的 key 获取相应的 value 值。那么有没有更好的方法来获取自定义字段的值呢?
Drupal已经给我们提供了一个 ://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_get_items/7">field_get_items 函数,原型如下:
function field_get_items($entity_type, $entity, $field_name, $langcode = NULL)
函数说明:
返回当前语言的此字段项目。
参数:
$entity_type: The type of $entity; e.g., 'node' or 'user'.
$entity: The entity containing the data to be displayed.
$field_name: The field to be displayed.
$langcode: (optional) The language code $entity->{$field_name} has to be displayed in. Defaults to the current language.
Return value
An array of field items keyed by delta if available, FALSE otherwise.
得到了 field_get_items 的返回字段项目,就可以使用 来渲染单个项目值:
field_view_value($entity_type, $entity, $field_name, $item, $display = array(), $langcode = NULL)
Returns a renderable array for a single field value.
Parameters
$entity_type: The type of $entity; e.g., 'node' or 'user'.
$entity: The entity containing the field to display. Must at least contain the id key and the field data to display.
$field_name: The name of the field to display.
$item: The field value to display, as found in $entity->field_name[$langcode][$delta].
$display: Can be either the name of a view mode, or an array of display settings. See field_view_field() for more information.
$langcode: (Optional) The language of the value in $item. If not provided, the current language will be assumed.
返回值 指定字段的渲染数组
下面是一个显示 field_jinsheng 字段的例子代码:
$jinsheng = field_get_items('node', $node, 'field_jinsheng');
$output = field_view_value('node', $node, 'field_jinsheng', $jinsheng[0]) ;
$output = drupal_render( $output);
print $output;
原神祈愿模拟器最新版
原神祈愿模拟器手机版是一款完整汉化的趣味原神抽卡模拟小游戏,
宝宝森林美食完整版
宝宝森林美食游戏最新版是一款十分有趣的休闲益智游戏,帮助宝宝
g沙盒仇恨官方英文版(gorebox)
G沙盒仇恨英文原版是一款最近非常火热的沙盒模拟类游戏,在这里
迷你世界测试服最新版2024
迷你世界测试服2021最新版,即迷你世界的先遣服版本,用户能
闪耀暖暖最新版2024
闪耀暖暖手游这是非常好玩的换装手游,游戏内容丰富有趣,游戏环