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

Get the value of a Text field in Drupal custom module

我正在开发一个自定义模块,它允许我创建一个自定义文本字段,我需要在其中为 HTTP 请求插入 URL。

我现在唯一想做的就是从文本字段中获取值并显示在节点中的某处。

这是 .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 );

要以编程方式获取任何节点的信息,请使用节点 ID 加载节点。
$nid = '1';//这是你站点的第一个节点
$node_info = node_load($nid);
print_r($node_info->field_custom_field[LANGUAGE_NONE][0]['value']);//这会打印出[=21=的值].您可以在此处使用任何字段名称。
而查看这些信息直接将代码放在hook_init函数中进行测试,以后你可以在你想要的地方使用。

(抱歉,我想帮助充实 Neha Singhania 答案,但还没有足够的代表对此发表评论。)

如果您还没有表示节点的关联数组,请使用节点 ID 加载它:

$node = node_load($nid);

一旦你掌握了它,你就可以使用其中任何一个访问任何文本字段值,我相信:

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

数组中的第一个键指定什么语言(undefined/none 在这些情况下),第二个 key/index 说明字段的哪个值(如果您有多个值字段),以及第三个是真正的肉和土豆。对于文本字段,您还会看到:

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

我相信这是 created/updated 在 node_save($nid) 上,它擦除任何 illegal/unsafe 值的值。

很多字段类型都是一样的,但不是全部。但是方法是一样的。例如,如果您想要 entity_reference 字段类型的值,它看起来像这样。

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

其中 target_id 是它引用的任何内部实体(节点 ID、用户 ID 等)的 integer/id 编号。我强烈建议安装 devel 模块,它为您提供 dpm() 功能;基本上,它是 print_r 的漂亮版本,并将显示为 Drupal 消息。

如果您坚持使用 Drupal,您有时会认为自己讨厌它。很多。我知道。但它仍然非常值得。 (而且,根据我自己的经验,似乎真的很容易找到工作...)

此外,这个问题可能应该继续drupal.stackexchange.com