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
问题:
回答1:
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.
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.
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.
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.
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.
Under Filters, add a filter for Node: Type so the view will only show products and not other types of nodes.
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.
Under Arguments, add Node: ID. The view will now display only nodes with IDs that match the argument passed to the view.
Check Allow multiple terms per argument, which will let you look up more than one node at a time.
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.