简体   繁体   English

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

[英]Get the value of a Text field in Drupal custom module

I am developing a custom module that allows me to create a custom text field where I need to insert a URL for a HTTP Request. 我正在开发一个自定义模块,该模块允许我创建一个自定义文本字段,我需要在该字段中插入HTTP请求的URL。

The only thing I would like to do right now is to get the value from the text field and show somewhere in the node. 我现在唯一想做的就是从文本字段中获取值并显示在节点中的某个位置。

Here is the .install code : 这是.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";
}

The .module file only contains : .module文件仅包含:

<?php

I'm very new to Drupal and i think i hate it. 我对Drupal非常陌生,我想我讨厌它。

Check this: 检查一下:

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

Or you can print the entire node: 或者,您可以打印整个节点:

print_r( $node );

To get the information of any node programmatically, load the node with its node id. 要以编程方式获取任何节点的信息,请使用其节点ID加载该节点。
$nid = '1';//This is the first node of your site $ nid ='1'; //这是您网站的第一个节点
$node_info = node_load($nid); $ node_info = node_load($ nid);
print_r($node_info->field_custom_field[LANGUAGE_NONE][0]['value']);//This will print the value of field_custom_field. print_r($ node_info-> field_custom_field [LANGUAGE_NONE] [0] ['value']); //这将打印field_custom_field的值。 You can use any field name here. 您可以在此处使用任何字段名称。
And to check this information directly put code in hook_init function for testing, later on you can use where you want. 并要检查此信息,直接将代码放在hook_init函数中进行测试,以后可以在需要的地方使用。

(Sorry, I want to help flesh out Neha Singhania answer, but don't have enough rep yet to comment on it.) (对不起,我想帮助充实Neha Singhania的答案,但没有足够的代表对此事发表评论。)

If you don't already have the associative array that represents the node, load it using the node id with: 如果您还没有代表该节点的关联数组,请使用节点ID通过以下命令加载它:

$node = node_load($nid);

Once you've got that, you can access any textfield value using either of these, I believe: 我相信,一旦掌握了这一点,就可以使用其中任何一个访问任何文本字段值:

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

The first key in the array specifies what language (undefined/none in those cases), the second key/index says which value of the field (in case you have a multiple value field), and the 3rd is the actual meat and potatoes. 数组中的第一个键指定哪种语言(在这些情况下为undefined / none),第二个键/索引指定字段的哪个值(如果您有多个值字段),第三个键为实际的肉和土豆。 For text field, you'll also see: 对于文本字段,您还将看到:

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

This is, I believe, created/updated on node_save($nid), and it scrubs the value for any illegal/unsafe values. 我相信这是在node_save($ nid)上创建/更新的,并且清除了任何非法/不安全值的值。

It's the same for many field types, but not all. 许多字段类型都是相同的,但不是全部。 But the method's the same. 但是方法是一样的。 For instance, if you wanted the value for a field type of entity_reference, it'd look something like this. 例如,如果您想要entity_reference字段类型的值,它将看起来像这样。

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

Where target_id is an integer/id number of whatever internal entity (node id, user id, etc.) it's referencing. 其中target_id是其引用的任何内部实体(节点ID,用户ID等)的整数/ ID号。 I'd HIGHLY recommend installing the devel module, which gives you the dpm() function; 我强烈建议安装devel模块,该模块为您提供dpm()函数。 basically, it's a pretty version of print_r, and will display as as Drupal message. 基本上,它是print_r的漂亮版本,将显示为Drupal消息。

If you stick with Drupal, you're gonna think you hate it at times. 如果您坚持使用Drupal,您有时会认为自己讨厌它。 A lot. 很多。 I know. 我知道。 But it's still pretty worth it. 但这仍然值得。 (And, from my own experience, seems to make it REALLY easy to find a job...) (而且,根据我自己的经验,似乎真的很容易找到工作...)

Also, this is a question that should probably go on drupal.stackexchange.com 另外,这个问题可能应该在drupal.stackexchange.com上进行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM