I have used Drupal for a while now, but now I'm stuck with CCK-fields. The thing is, that I need to have a CCK-field combined of a userreference and a taxonomy term, meaning: A node needs to have multiple values of a CCK-field having two values: user and taxonomy-term. In detail I want to have departments (as nodes), with referenced to users (contact person) having some role (in the department, roles are saved as taxonomy terms).

I found a post regarding this issue here. dnorman describes how to create an own CCK-field, which is an compound of four DB-fields. This is nice, but don't relly fit my needs, as I need to use references (user and taxonomy).

I could just copy the code of the userreference and taxonomy_field-module to get this done, but I think this would be rather hackish. Update to these CCK-fields won't get ported back to my field and duplicating code just doesn't feel right.

Is there any clean way to get compound fields? Would it be possible to create a "compound CCK" module for Drupal, which puts multiple values into a single table? (as CCK already does for fields which only appear once)

Any other hint on getting this issue fixed without some hack?

Comments

marcvangend’s picture

Although I'm not sure if I completely understand what you're tryingto do, I think the computed field module (http://drupal.org/project/computed_field) can be your answer. You will have to write some php yourself, I hope that's not a problem.

ddanier’s picture

Although I'm not sure if I completely understand what you're tryingto do [...]

I try to have a new CCK-field constructed out of two existing fields. Meaning: I have a taxonomy-field and an userreference-field, which both have some foreign key to some other database table. Now what I want is a CCK-field that holds both of these database-fields in one database-table.

tanonomy(term_id) + userreference(user_id) => new-cck-field(term_id, user_id)

[...] I think the computed field module (http://drupal.org/project/computed_field) can be your answer. You will have to write some php yourself, I hope that's not a problem.

Not really, as this does different things. Writing PHP should not be a problem.

marcvangend’s picture

OK, but why can't computed field do that?
It's been a while since I used computed field, but I think the code should be something like:
$node_field[0]['value'] = $node->field_user[0]['value'] . '-' . key($node->taxonomy);
Computed field is not only for mathematical computations!

ddanier’s picture

The problem ist, that the user is related to the term. I want to have users with different terms, which are not associated with the node itself.

I think this graph shows exactly what I'm looking for:
http://www.webmasterpro.de/~ddanier/drupal_compound.png

marcvangend’s picture

The link to the image seems to be broken - can you fix it?

ddanier’s picture

Sorry, our server had some trouble yesterday. This is fixed now.

marcvangend’s picture

Let's make sure I understand exactly what you mean...
In your graph, the cck-fields are drawn exactly like the node, user and term. However, cck-fields cannot exist on their own; they are always part of a node. So, in the 'What I need'-graph, to which node type does cck-field1 belong?

If I would have to draw what I propose, it would look like this:

-- node ------------------------------------------
|                                                |
|                    |-- cck1(user reference) <--+------ user
|   cck2(computed) --|                           |
|                    |-- category(term)       <--+------ term
|                                                |
--------------------------------------------------

You see a node with two cck fields and a category selector. The user reference node and de category are selected by the user. Cck2, the computed field, is automatically updated when the node is saved. It's value is a combination of the user and the term.

ddanier’s picture

Of course CCK-fields are never alone, what I have drawn is the DB-representation of what I need. On the database level CCK-fields get their own table(s), which makes them somewhat independet (of course they all have some foreign key to the node they belong to).

[...] is automatically updated when the node is saved. It's value is a combination of the user and the term.

The problem with this approach is, that there may be multiple users, all with there own terms associated with the node. And even if there where only one user the terms of the node might differ from the terms needed for the user-reference.

What I currently got as an idea is creating two node-types. One for the content itself and one for the relation betwean node and user/term (having two CCK-fields: userreference and taxonomy). Problem is: This is not really good, when it comes to editing things...and the database gets bloated with unneeded nodes, admin gets bloated with unneeded content type, ....

marcvangend’s picture

OK, I think I'm finally getting a clear picture. I'll try to put this in my own words. You want to:
- create nodes of a certain node type (for instance: magazine)
- add one or more user references to the node (for instance: John, Martha)
- categorize the user references (for instance: designer of the magazine (John), editor of the magazine (Martha))
Am I correct?

If you don't have too much different types of relationships, couldn't you just add multiple user reference fields, one for each type?

And by the way: How will you be using this information, what are you going to do with it?

ddanier’s picture

Am I correct?

Yes, you are. Nice example btw. :)

If you don't have too much different types of relationships, couldn't you just add multiple user reference fields, one for each type?

Thought about that, too. But sadly it's not possible. I'm not even sure, if a taxonomy-term will fit our needs, perhaps a normal text-field will fit better. So no way to put this into fixed categories.

And even if this were possible: The database layout would definitely be bad. As every user-category (I use your example here) needs to hold multiple users, every type would get its own database table. So every type will need its own SELECT/JOIN to fetch the data.

How will you be using this information, what are you going to do with it?

They are shown in a simple table. I know, I know....a textarea + some WYSIWYG-editor would be enough. Problem ist, our client wants to have some fixed input form, that doesn't leave much space for individual content. The rendered page should follow a strict template, that is equal on every node of this type.

Anyway, if this is not possible I will try to work around it somehow. Currently a possibility would be to create a second node-type containing a nodereference, a termreference and an userreference and then using this nodes to fetch all associated users. This would make things more complex of course, especially when creating nodes and fetching the data.

marcvangend’s picture

I'm not even sure, if a taxonomy-term will fit our needs, perhaps a normal text-field will fit better. So no way to put this into fixed categories.

You can also create a taxonomy field for 'free tagging', so without a fixed vocabulary.

But I agree, I think your best option is a second content type which links node, user and term together. A couple of modules ('add and reference', 'addnode') have attempted to let you create and reference a node at the same time, as subform in the creation of the referencing node. However I am not sure if they are a ready for production sites right now. If they are, it might improve the workflow for your client, bringing two separate forms back to one with a sub form.

ddanier’s picture

A couple of modules ('add and reference', 'addnode') have attempted to let you create and reference a node at the same time, as subform in the creation of the referencing node.

AFAIK none of these are ready for Drupal 6 yet. But I will crawl the module index again, perhaps there has been some progress.

Thanks for your support, you helped me a lot. ;-)

marcvangend’s picture

You're welcome... You have an interesting problem to solve, I like that :-)
When the site is finished, I'd like to hear what the final solution is. Thanks for sharing and good luck.

nachenko’s picture

Hi!

I almost found a solution. You can read it here, search comments by "nachenko"

http://www.darcynorman.net/2008/05/02/creating-a-custom-compound-field-f...

This is what I suggested:

Create a fieldgroup.
Put the CCK fields you want into there.
Create CCK fields from vocabularies if required using CCK taxonomy fields module (http://drupal.org/project/cck_taxonomy).
Put your CCK fields into the fieldgroup.
Get and install fieldgroup table module (http://drupal.org/project/fieldgroup_table)
Now, edit your fieldgroup and look at the last option: “Multiple values”.

The problem is that the module "fieldgroup table" seems to offer the right choice, but it doesn't work. The right concept in a unfinished code. I'll try to fix it, but can't make promises. ANyway, have a look at it.

Anonymous’s picture

This thread is pretty old, but here are some useful links I found, while attempting to do something similar. I ended up using these 2 tutorials to create my own custom "compound" CCK field:

http://www.poplarware.com/articles/cck_field_module
http://www.lullabot.com/articles/creating-custom-cck-fields

I'm pretty sure you can "add fields to fields" in Drupal 7, using the Entity API, but I haven't been able to find an "out of the box" solution for Drupal 6.