2

One of the great things about the Entity and Field APIs is the ability to add fields to any entity (nodes, users, etc.) and subsequently any bundle (e.g. node content types).

But is there a way, using the Field API or the Entity API, to make fieldable nodes? That is, have field instances attach to individual nodes instead of content types/bundles. So node 1 might have a file field and a text field, node 2 might have a number field but no text field, etc.

If there isn't, is there any work being done using other concepts that would achieve the same result? The closest I can think of is Webform, but I'm sure that it's not intended to be a way to create content, and it doesn't take advantage of the Field API for its fields.

3

You could use something like http://drupal.org/project/conditional_fields but that's all done from a front-end point of view, which means Drupal doesn't really know about your structure.

http://drupal.org/project/properties looks interesting and may suit depending on your needs.

I had a similar issue recently and decided to split up into three separate content types instead of just one.

2

It's hard to say no it's not possible, as I might be missing something. But based on the data stored for fields and instances of fields, I'd say the current setup doesn't support this.

The best solution I can think off, would be to create a special field, that would allow for values of different kinds, much like you can add several images using the same field. I'm not sure if doing something like that would be a good idea, it would certainly be complex and time consuming, and probably couldn't use the code for all the fields that already have been defined.

1

I did this by attaching entities to nodes on creation.

  1. Create an entity type: I used contrib's Entity API as the framework for doing this.
  2. Create a custom node content type.
  3. Create an implementation of hook_node_insert() for your custom content type that creates a bundle for the entity type you created in step 1.

At this point, the node is technically fieldable: you can do any number of things, whether its create fields programmatically or implement some sort of UI to let users add their own fields.

There are two gotchas with this approach:

  1. Since the node and entity type are two separate structures, you need to make sure the bundle you create is linked to the node: there's some promising work being done with the Relation module, but I opted for just adding a property to the entity bundle that stores the node ID (you could also store the bundle ID in the node if you wanted), then loading the bundle information on hook_node_load().

  2. Versioning bundles is heavy wizardry I chose not to get into: there is no API for it, and while in theory if you created a record of every change you made to the bundle it should work, it might possibly instead awaken the Old Gods. I opted for turning off revision support on the nodes I made fieldable this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy