Php 使用Drupal 6读取模块代码中的节点字段值

Php 使用Drupal 6读取模块代码中的节点字段值,php,drupal,drupal-6,drupal-modules,cck,Php,Drupal,Drupal 6,Drupal Modules,Cck,我已经创建了一个自定义模块,正在使用hook_block以编程方式创建一些块 我的问题是如何访问模块中当前节点的字段值,包括CCK字段 我基本上希望从CCK字段中获取一个值,并在为该页面构建块时使用该值。获取当前节点是一件棘手的事情。标准做法是这样做: if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') { $node = node_load(arg(1)); // Collect out

我已经创建了一个自定义模块,正在使用hook_block以编程方式创建一些块

我的问题是如何访问模块中当前节点的字段值,包括CCK字段


我基本上希望从CCK字段中获取一个值,并在为该页面构建块时使用该值。

获取当前节点是一件棘手的事情。标准做法是这样做:

if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') {
  $node = node_load(arg(1));
  // Collect output.
}
// Text field. Structure also works for number fields.
$text = $node->field_my_text_field[0]['value']
// Node Reference field.
$nref = $node->field_my_nref_field[0]['nid']
// User Reference field.
$uref = $node->field_my_uref_field[0]['uid']
arg()
将元素从Drupal路径中拉出。由于所有节点(无论路径别名可能显示什么)都显示在节点/#,通过检查“node”和第二个元素是数字,可以很好地保证您可以控制节点。选中第三个path元素可以避免在节点编辑表单和挂起特定节点的其他页面上进行处理

CCK值加载到节点中,通常如下所示:

if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') {
  $node = node_load(arg(1));
  // Collect output.
}
// Text field. Structure also works for number fields.
$text = $node->field_my_text_field[0]['value']
// Node Reference field.
$nref = $node->field_my_nref_field[0]['nid']
// User Reference field.
$uref = $node->field_my_uref_field[0]['uid']

“0”数组元素指定字段的增量。任何给定的字段实际上都可以处理多个值,即使您将字段限制为单个值,CCK中的数组结构也会假定这种可能性。

在Drupal 6中,有一个内置的Drupal函数来获取节点对象

if ($node = menu_get_object()) {
  …
}

请在此阅读更多信息。

太棒了,谢谢。我很惊讶没有一个内置的Drupal函数来获取节点id!