在Drupal自定义模块中获取文本字段的值

在Drupal自定义模块中获取文本字段的值

问题描述:

我正在开发一个自定义模块,该模块允许我创建一个自定义文本字段,我需要为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和我想我恨它。

检查:

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

或者你可以打印整个节点:

print_r($node); 
+0

我在哪里必须把这个代码?什么是$ nid变量? –

+0

'$ nid'是节点ID。把它放在你想显示'graph_field'内容的地方。 –

要在任何节点的信息编程,装入其节点ID的节点。
$ nid ='1'; //这是您网站的第一个节点
$ node_info = node_load($ nid);
print_r($ node_info-> field_custom_field [LANGUAGE_NONE] [0] ['value']); //这将打印field_custom_field的值。你可以在这里使用任何字段名称。
并检查这些信息,直接把代码放在hook_init函数中进行测试,稍后可以使用你想要的地方。

(对不起,我想帮助割肉出局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