国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > 建站經(jīng)驗(yàn) > 正文

獲取Drupal自定義字段值并顯示的方法

2024-08-30 19:06:45
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Drupal有很強(qiáng)大的自定義字段的功能,如果我們要將自定義字段的值打印出來(lái),直接 $nianling = $node->field_nianling[LANGUAGE_NONE][0]['value']; 可能會(huì)出錯(cuò),現(xiàn)在我們來(lái)介紹一個(gè)更好的方法.

在Drupal獲取一個(gè)自定義字段的值最常用的方法是:

$nianling = $node->field_nianling[LANGUAGE_NONE][0]['value'];

不過(guò)當(dāng)field_nianling內(nèi)容為空時(shí),會(huì)出現(xiàn)類似這樣的錯(cuò)誤:Notice:Undefined index: value in eval(),如果我們?cè)谠谌≈抵凹觽€(gè)判斷,如這樣:

  1. if (isset(field_nianling[LANGUAGE_NONE])) { 
  2.     $nianling = $node->field_nianling[LANGUAGE_NONE][0]['value']; 
  3. //Vevb.com 

是可以解決問(wèn)題,但如果字段多時(shí),代碼看起來(lái)就不夠簡(jiǎn)潔,而且如果自定義字段是列表字段,就有可能還需要根據(jù)列表的 key 獲取相應(yīng)的 value 值。那么有沒有更好的方法來(lái)獲取自定義字段的值呢?

Drupal已經(jīng)給我們提供了一個(gè)

  1. <a href="https://api.drupal.org/api/drupal/modules%21field%21field.module/function/field_get_items/7">field_get_items</a>  

函數(shù),原型如下:

  1. function field_get_items($entity_type$entity$field_name$langcode = NULL) 

函數(shù)說(shuō)明:

返回當(dāng)前語(yǔ)言的此字段項(xiàng)目.

參數(shù):

  1. $entity_type: The type of $entity; e.g., 'node' or 'user'
  2.  
  3. $entity: The entity containing the data to be displayed. 
  4.  
  5. $field_name: The field to be displayed. 
  6.  
  7. $langcode: (optional) The language code $entity->{$field_name} has to be displayed in. Defaults to the current language. 
  8.  
  9. Return value 
  10.  
  11. An array of field items keyed by delta if available, FALSE otherwise. 

得到了 field_get_items 的返回字段項(xiàng)目,就可以使用:

  1. <a href="https://api.drupal.org/api/drupal/modules!field!field.module/function/field_view_value/7">field_view_value</a> 

來(lái)渲染單個(gè)項(xiàng)目值:

  1. field_view_value($entity_type$entity$field_name$item$display = array(), $langcode = NULL) 
  2. Returns a renderable array for a single field value. 
  3.  
  4. Parameters 
  5.  
  6. $entity_type: The type of $entity; e.g., 'node' or 'user'
  7.  
  8. $entity: The entity containing the field to display. Must at least contain the id key and the field data to display. 
  9.  
  10. $field_name: The name of the field to display. 
  11.  
  12. $item: The field value to display, as found in $entity->field_name[$langcode][$delta]. 
  13.  
  14. $display: Can be either the name of a view mode, or an array of display settings. See field_view_field() for more information. 
  15.  
  16. $langcode: (Optional) The language of the value in $item. If not provided, the current language will be assumed. 

返回值指定字段的渲染數(shù)組

下面是一個(gè)顯示 field_jinsheng 字段的例子代碼:

  1. $jinsheng = field_get_items('node'$node'field_jinsheng'); 
  2. $output = field_view_value('node'$node'field_jinsheng'$jinsheng[0]) ; 
  3. $output = drupal_render( $output); 
  4. print $output;

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沛县| 宁都县| 内黄县| 益阳市| 兰西县| 容城县| 阿合奇县| 侯马市| 三穗县| 宣化县| 房产| 石河子市| 辽宁省| 湘阴县| 甘孜| 青铜峡市| 信阳市| 蓬莱市| 瓦房店市| 西和县| 敦煌市| 黑水县| 梨树县| 成武县| 循化| 湘西| 澳门| 碌曲县| 漠河县| 垦利县| 武功县| 讷河市| 海安县| 怀宁县| 丰城市| 宝坻区| 临海市| 克拉玛依市| 芦溪县| 庄浪县| 郸城县|