Drupal 中对表的节点引用 [英] Node Reference to Table in Drupal

查看:26
本文介绍了Drupal 中对表的节点引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Drupal 和 CCK,
我有一个名为文章的内容类型.
这篇文章有 5 个节点参考.
我正在 CCK 中使用表字段,并尝试将其连接到
参考文献,因此每篇文章 [包含一个表格字段]
将有一个包含 5 列的表格,每个产品一列
以及可以根据用户需要更改的内容

但是我不太确定该怎么做,我试过了
在参考中选择产品后,将产品添加到列中
通过jquery,它似乎很慢..有没有以前的解决方案?

要清除的图像

解决方案

您可以使用 查看

a>,但在我们开始之前,最好对节点进行一些更改.

  1. 不要为每个产品使用 5 个单独的节点引用字段,而是添加一个引用产品的节点引用字段.在字段的配置中(在内容类型的Manage fields 选项卡上找到),在 下将 Number of values 设置为 5全局设置.

  2. 然后,去掉tableview的CCK字段.您不需要它,因为您要创建的视图会执行您想要做的事情.

  3. 现在,转到网站建设 ->视图 -->添加.输入视图的名称;让我们说列表.您可以将 DescriptionTags 更改为您想要的任何内容,但将 View type 设置为 Node.

  4. 现在,设置视图.在 Fields 下,添加要显示在产品表中的字段;比方说节点:标题节点:正文.

  5. Filters 下,为 Node: Type 添加一个过滤器,这样视图将只显示产品而不显示其他类型的节点.

  6. Basic Settings下,将StyleUnformatted更改为Table.

    您现在将拥有一个视图,该视图将在表格中显示所有可用产品.下一步是将该表限制为仅显示特定节点引用的产品.为此,您将创建一个参数.

  7. 参数下,添加节点:ID.视图现在将仅显示 ID 与传递给视图的参数匹配的节点.

  8. 选中每个参数允许多个术语,这将使您一次查找多个节点.

  9. 由于您不会手动传递这些参数,因此视图会自动生成它要查找的参数.在如果参数不存在时采取的操作下选择提供默认参数.

有几个选项可用,但没有一个与您想要的匹配:即所引用节点的节点 ID.因此,选择 PHP Code 以便您可以提供自定义参数.使用以下代码:

$arguments = array();$node = node_load(arg(1));如果($node->field_product){foreach ($node->field_product 作为 $product) {if ($product['nid']) $arguments[] = $product['nid'];}}return implode(',', $arguments);

这将查找页面的节点 ID (arg(1)),检查它是否具有产品节点引用字段 ($node->field_product,将 field_product 更改为您的字段的短名称),然后构建一个包含每个引用节点 ID 的参数.它以 Views 期望的格式返回参数列表:1,2,3.

现在视图已经完成:剩下要做的就是让视图出现在页面上.您可以创建一个显示,然后将该块添加到站点建设下的区域 ->.如果您转到引用产品的页面,该块将与引用块表一起显示.

如果您希望视图成为节点本身的一部分,请查看视图参考模块,它创建一个引用视图的 CCK 字段,就像节点引用引用节点一样.

I have Drupal with CCK,
I have a content type named Article.
This Article has 5 Node references.
I'm using the table field in CCK and I'm trying to connect it to
the references, so each article [that holds a table field]
would have a table with 5 columns, one for each product
and content that can change according to what the user wants

However I'm not really sure how to do so, I tried
adding the products to the columns once they are chosen in the reference
via jquery, it seemed sluggish.. is there any former solution to this?

Image to clearify

解决方案

You can do this with a View, but before we get to that, it's probably better if you make a few changes to your node.

  1. Instead of using 5 separate node reference fields for each product, add one node reference field that references products. In the configuration for the field (found on the Manage fields tab for the content type) , set Number of values to 5 under Global settings.

  2. Then, get rid of the tableview CCK field. You won't need it as the view you're going to create will do what you're looking to do.

  3. Now, go to Site Building -> Views --> Add. Enter in a name for the view; let's say listing. You can change the Description and Tags to whatever you want, but leave the View type set to Node.

  4. Now, set up the view. Under Fields, add the fields you want to show up in your product table; let's say the Node: Title and the Node: Body.

  5. Under Filters, add a filter for Node: Type so the view will only show products and not other types of nodes.

  6. Under Basic Settings, change the Style from Unformatted to Table.

    You'll now have a view that'll display every product available in a table. The next stage of this is to limit that table to only display products that a particular node references. To do that, you'll create an argument.

  7. Under Arguments, add Node: ID. The view will now display only nodes with IDs that match the argument passed to the view.

  8. Check Allow multiple terms per argument, which will let you look up more than one node at a time.

  9. Since you won't be passing those arguments manually, you'll have the view automatically generate the arguments its looking for. Select Provide default argument under Action to take if argument is not present.

There are a few options available, but none match the one you want: that is, the node IDs for the nodes referenced. So, choose PHP Code so you can supply a custom argument. Use the following code:

$arguments = array();
$node = node_load(arg(1));

if ($node->field_product) {
  foreach ($node->field_product as $product) {
    if ($product['nid']) $arguments[] = $product['nid'];
  }
}

return implode(',', $arguments);

This will look for the page's node ID (arg(1)), check to see if it has the product node reference field ($node->field_product, change field_product to the short name of your field), then build an argument containing the ID of each node referenced. It returns the argument list in the format that Views expects: 1,2,3.

Now the view is complete: the only thing left to do is make the view appear on the page. You can create a Block display, then add that block to a region under Site building -> Blocks. If you go to a page that references products, the block will appear with the table of referenced blocks.

If you want the view to be a part of the node itself, look into the View Reference module, which creates a CCK field that references a view much like Node Reference references a node.

这篇关于Drupal 中对表的节点引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆