2015-05-19 98 views
0

我正在開發一個自定義模塊,該模塊允許我創建一個自定義文本字段,我需要爲HTTP請求插入一個URL。在Drupal自定義模塊中獲取文本字段的值

我現在想要做的唯一事情就是從文本字段獲取值並顯示在節點的某個位置。

這裏是.install代碼:

function graph_field_enable() { 
    $field = array(
    'field_name' => 'graph_field', 
    'type' => 'text', 
); 
    field_create_field($field); 


/** 
    * Bind field to a entity bundle. 
    */ 
    $instance = array(
    'field_name' => $field['field_name'], 
    'entity_type' => 'node', 
    'bundle' => 'station', 
); 
    field_create_instance($instance); 
} 
/** 
* Implements hook_disable(). 
* 
* Remove field from node bundle (content type) and then delete the field. 
*/ 

function graph_field_disable() { 
    $instance = array(
    'field_name' => 'graph_field', 
    'entity_type' => 'node', 
    'bundle' => 'station', 
); 
    field_delete_instance($instance); 
    field_delete_field($instance['field_name']); 
    print 'Removed ' . $instance['field_name'] . "\n"; 
} 

的.module文件只包含:

<?php 

我很新的Drupal和我想我恨它。

回答

1

檢查:

$node = node_load($nid); 
print_r($node->graph_field); 

或者你可以打印整個節點:

print_r($node); 
+0

我在哪裏必須把這個代碼?什麼是$ nid變量? –

+0

'$ nid'是節點ID。把它放在你想顯示'graph_field'內容的地方。 –

1

要在任何節點的信息編程,裝入其節點ID的節點。
$ nid ='1'; //這是您網站的第一個節點
$ node_info = node_load($ nid);
print_r($ node_info-> field_custom_field [LANGUAGE_NONE] [0] ['value']); //這將打印field_custom_field的值。你可以在這裏使用任何字段名稱。
並檢查這些信息,直接把代碼放在hook_init函數中進行測試,稍後可以使用你想要的地方。

0

(對不起,我想幫助割肉出局Neha Singhania的答案,但沒有足夠的代表尚未對此發表評論。)

如果您還沒有表示節點,負載關聯數組使用節點ID與它:

$node = node_load($nid); 

一旦你得到了,你可以使用這兩種訪問任何文本字段的值,我相信:

$node->field_textfieldname['und'][0]['value']; 
$node->field_textfieldname[LANGUAGE_NONE][0]['value']; 

第一數組中的鍵指定了什麼語言(在這些情況下,未定義/無),第二個鍵/索引指出了字段的哪個值(如果有多個值字段),第三個是實際的肉和土豆。對於文本字段,你還會看到:

$node->field_textfieldname[LANGUAGE_NONE][0]['safe_value']; 

這一點,我相信,創建/更新node_save(NID $),它洗刷值用於任何非法/不安全的值。

許多字段類型都是一樣的,但不是全部。但方法是一樣的。例如,如果你想要一個字段類型的entity_reference的值,它會看起來像這樣。

$node->field_entityrferencefieldname['und'][0]['target_id']; 

其中target_id是它所引用的任何內部實體(節點ID,用戶ID等)的整數/ ID號。我會強烈建議安裝devel模塊,它爲您提供dpm()函數;基本上,它是一個漂亮的print_r版本,並將顯示爲Drupal消息。

如果你堅持使用Drupal,你會覺得你有時討厭它。很多。我知道。但它仍然非常值得。 (而且,從我自己的經驗,似乎使它很容易找到一份工作......)

此外,這是一個問題,也許應該去上drupal.stackexchange.com

相關問題