views:

123

answers:

1

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 alt text

A: 

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.

Mark Trapp
I built the view, but I don't know how to add it with the block thing... the name of the content type is named Table...I also tried going to the content-types and searching for a way to add a view for it... didn't see it.. but I think i'm really close because I did almost every step (right until the site building -> Blocks part)
Asaf
When you edit the view, you should see a list of tabs with a drop-down menu and an "Add Display" button. Select "Block" from the menu, click "Add Display" and save the view. You'll now have a block called "viewname: Block" available when you go to Site Building -> Blocks.
Mark Trapp
ok I'm getting really close, the thing is I need to products to be each product in a column not in a row and I want to be abled to add content to the table generated (or fetch from a specific part of the product)
Asaf
@Asaf you can add whatever fields you like; just specify them within the Field settings for the view. As for changing the orientation of the table, you're going to have to get creative and figure out how to theme it so that'll work. Views works with the same orientation as the data appears in the Database: columns correspond to fields, rows correspond to records.
Mark Trapp
Your answer was down right perfect, you have earned the bounty.thank you very much, just one last question, I see that I need to edit the table style so the rows and columns would be mixed (so instead of a product being a row, it would be a column), any ideas on that?
Asaf
If you click on "Theme information" under "Basic settings" when editing the view, it'll give you template hints. You want to create a template file in your theme with one of the file names next to "Style output". If you click on "Style output", it'll give you the default PHP to put in that template file. Modify that template as desired.
Mark Trapp