How to add more fields to a linker table e.g. where a node contains a node-reference field?

Events happening in the community are now at Drupal community events on www.drupal.org.
danaktivix's picture

Apologies for asking daft questions. I'm just about to attempt my first CCK customisation, and would dearly love some advice. I've been through the CCK developer's guide, and am confused!

So: I have two custom node types - organisation, and project. Each organisation is involved in zero or more projects; each project involves one or more organisations. So it's a many to many relationship. And so I've used the CCK node reference module. Great!

Problem: I want to add more fields to the connection - so that, e.g., every instance of a project in an organisation can also have a 'money' field. Here's where I need to customise, and I'm not sure where to start. For many to many relationships, there needs to be a linker table - and one has been created by CCK. It's the 'content_field_{field-name}' table. In my case, it's 'projects this organisation is involved in'. It stores all the multiple {field-name}_nids of the nodes that connect.

So it looks just like a linker table - and I figure I will add more columns here.

But now I'm stuck:

  1. Where do I add new fields, so that they will be present for each instance of the node reference field when someone edits or creates a node?
  2. How does it get added to the database - in such a way that it connects to the table I need it to, so that it's associated with that particular many-to-many instance?
  3. How do I load in data when re-editing a node?

I am assuming there are hooks for doing this, but I'm afraid I've got rabbit-in-headlights syndrome and don't really know where to begin. Should I be making the changes directly in nodereference.module?

As I say, I've been through the 'sample CCK field module' tutorial, and just don't seem to get it. Doh. Maybe I should just wait til morning, see if light dawns on the problem - but any pointers would be really appreciated... I quite understand if the answer's 'go away and try harder to work it out', but I thought someone might go, 'oh yeah - I've done that. Here's one way...'

If there's no replies, I'll probably try writing again with slightly more intelligent questions!

Thanks,

Dan

Comments

no easy answer

noah@groups.drupal.org's picture

Hi Dan,

I'm very interested in doing the same kind of thing, and from what I've read, there's no easy answer. The good news is that many other people seem to be interested as well, and there are several fledgling approaches (I found Robert Douglass' summary helpful). You might want to take a look into the Relationships & site structuring group -- I'm going to.

Anyone else out there been working on this?

Noah

Somewhat related, maybe easier, issue

jaysmall's picture

My issue with node reference has more to do with the ability to display data (other than a link, and a title as link text) in a detail view from nodes I have chosen as related in a noderef field.

Here's the case, in a shopping-oriented site:

Three data types: Store, Event, Item.

Administration procedures will presume that Store records get entered before any related Events or Items.

When I'm entering a new Item, I want to be able to relate that item to one or more Stores where it is for sale. And I want to be able to relate it to one or more Events where it will be featured (sale/promotion events, for instance).

When I'm entering an Event, I want to be able to relate that event to one or more stores where it is taking place.

So I create a noderef in the Item content type for Stores, and another for Events. And I create a noderef in Events to point to Stores.

Now I have found ways (http://drupal.org/node/70145) to create blocks that display only in a Store detail view and show teasers for only related Events and Items for that Store. And the same method allows me to show teasers to related Items in the Event detail view.

But if I have selected related Stores and Events in my Items data entry, all I seem to be able to display in an Item detail view is a list of simple links to each related Store or Event, with each node's title as the link text.

I would like, for example, to display Store addresses and other fields in Item details and Event details -- not just the link to the Store detail view.

Is there a pattern I can use in Argument Handling Code in Views that would accomplish this? I have not been able to figure it out, and I suspect I'm overlooking something easy.

Thanks in advance for all advice and pointers.

-- Jay Small
http://smallinitiatives.com/contact-jay-small/

You might have a look at the

sun's picture

You might have a look at the patches in the Theming nodereferences issue. I've submitted one that allows to load the full referenced/referred node in the past. Since then there have been many follow-ups.

Another issue that is unfortunately not yet solved is Reverse link on node record for nodereference field, which should be used in conjunction to the previous one.

Daniel F. Kudwien
unleashed mind

Daniel F. Kudwien
netzstrategen

One of the follow-up patches worked

jaysmall's picture

Dan,

Thank you for this pointer -- very helpful in my case. The patch provided in http://drupal.org/node/65133#comment-191144 did the trick.

With the patch installed, I can select whether to display teaser or full node of the referenced node in the view of the referencing node. So, in my case, I can show shop details in an item detail view where I have selected a shop in noderef during item edit.

My dev site is still a ways from production, so I can't really measure the performance impact of pulling in the full referenced node to generate this merged view. It does not seem to add a hideous number of ticks to page loads, though, so I am optimistic.

FWIW, in case others want to attempt, my installation is 5.1 with the current stable CCK as of this writing (5.x-1.4).

-- Jay Small
http://smallinitiatives.com/contact-jay-small/

I have a similar setup in

jdw23's picture

I have a similar setup in which I have two node types: MagazineIssue and MagazineArticle. After I create a MagazineIssue node, I then link MagazineArticle nodes to a particular MagazineIssue via a node reference field.

Now when I go to display a MagazineIssue node, I want to display a menu of all linked MagazineArticles. The only way I've found to do this so far is by running custom SQL statements in my node-MagazineIssue.tpl.php file, taking the nid of the MagazineIssue and finding all MagazineArticles where the node references that nid.

It works, but I'd love to find a more efficient and less expensive way. I'm very interested in seeing where this thread goes.

Unfortunately there is no

yched's picture

Unfortunately there is no easy way to do what you're describing.
The best option would involve a 'richer' relation-field module, allowing for 'qualified' relations - like noah stated in his comment. Unfortunately, I'm not aware that much progress has been made in this area since the last posts in the thread he mentions, a few weeks / months ago. Anyway, this approach means a whole new field module - you can't get that by 'simply' extending nodereference.

Another possible way would be to be able to 'group and mix' fields in such a way that on node edit / display, instead of the regular :
- field A / value 1
- field A / value 2
- ...
- field A / value n
- field B / value 1
- field B / value 2
- ...
- field B / value m

you get something like
- field A / value 1
- field B / value 1
- field A / value 2
- field B / value 2
- ...
- field A / value n
- field B / value n

That way, if field A is your noderef field, and field B is your 'money' field, then in a way you have a way to sort of 'qualify' the relation. The feature request in http://drupal.org/node/119102 is about that. Unfortunately again, current CCK API does not really allow for a clean and strong implementation of the idea, since each field module control how many form elements it displays (in the example above, no way to ensure that n and m are and stay equal...)

Content Construction Kit (CCK)

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: