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

发布于 2024-09-25 21:30:48 字数 129 浏览 0 评论 0原文

我创建了一个自定义模块,并使用 hook_block 以编程方式创建一些块。

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

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

I have created a custom module and am using hook_block to programmatically create some blocks.

My question is how can I access field values including CCK fields for the current node within my module?

I basically want to get a value from a CCK field and use the value when building my block for that page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等你爱我 2024-10-02 21:30:48

到达当前节点是后部的一种尴尬的疼痛。标准做法是执行如下操作:

if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') {
  $node = node_load(arg(1));
  // Collect output.
}

arg() 将元素从 Drupal 路径中拉出。由于所有节点(无论路径别名可能显示什么)都出现在 node/# 处,通过检查“node”并且第二个元素是数字,可以很好地保证您已经掌握了节点。检查第三个路径元素可以让您避免在节点编辑表单和挂起特定节点的其他页面上进行处理。

CCK 值被加载到节点中,通常看起来像这样:

// 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”数组元素指定字段的 delta。任何给定字段实际上都可以处理多个值,即使您将该字段限制为单个值,CCK 中的数组结构也假定这种可能性。

Getting at the current node is an awkward pain in the posterior. Standard practice is to do something like this:

if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') {
  $node = node_load(arg(1));
  // Collect output.
}

arg() pulls elements out of the Drupal path. Since all nodes (regardless of what a path alias might show you) appears at node/#, by checking for 'node' and that the second element is a number, you are fairly well guaranteed to have your hands on a node. Checking the third path element allows you to avoid processing on the node edit form and other pages that hang off a specific node.

CCK Values are loaded into the node, and usually look something like this:

// 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']

The "0" array element specifies the delta of the field. Any given field can actually process multiple values, and the array structure in CCK assumes this possibility even if you restrict the field to a single value.

嘦怹 2024-10-02 21:30:48

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

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

在这里阅读更多内容 http://api.drupal.org/api/function/menu_get_item/6< /a>.

In Drupal 6 there is a built-in Drupal function to get the node object.

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

Read more here http://api.drupal.org/api/function/menu_get_item/6.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文