I have done modules back in Dr5 where i needed to add a field (or a few) to existing cck node types. Basic approach has been:
- node admin form alter to enable for that node type
- node form alter to add field to node form
- custom submit callback to add content to custom db table
- load value in nodeapi(load) to add to node object
- write custom views handler
- etc, etc...
Need to do this now for Dr6 and don't think anything new in CCK that helps simplify any of these steps; but someone on irc last night suggested that cck could do all of this now (don't think he actually knew what he was talking about - but thought i should ask).
One thing i thought would be an improvement over what i used to do, was allow the field to show in the node type's manage fields admin, and sure enough, a little digging and 6 lines of code and all my custom fields can now be re-arranged from manage fields (hook_content_extra_fields). Guy on irc suggested this was a hack and this led me to believe possibly CCK is maybe now at the point where i can define a content field (via api) and not worry about db storage or views handlers - as opposed to just defining a form field.
If any of this is available in Dr6? Could you suggest the hooks in question, api docs or preferably an example module that takes advantage?
Maybe this is something on the way in Dr7? :) Sure would be cool.
Comments
Comment #1
markus_petrux commentedThe APIs to create fields programmatically are located in includes/content.crud.inc. You may want to look at how the Content Copy module works (import/export content types with CCK fields and field groups). One module that I know of that provides a custom content type that needs to be imported is APK.
You may also want to look at the modules repository for a lot of CCK field implementation that extend those provided by CCK itself.
The CCK project page contains a few links to documentation to get started, also install the Advanced Help module. It provides a lot of information about CCK itself, Views, and other modules.
Comment #2
liquidcms commentedthanks Markus, checking those out now.
but, in your answer i didn't see the simple Yes/No answer. Perhaps you don't quite get my question? I am not trying to create a custom content type - i know there are plenty of examples around for that. I am simply trying to create a module which ADDS numerous custom fields to any existing node type that i set it to.
and more specifically, since i do know how to do that as well... but just wondering if cck has come along to the point that i don't need to do all the steps listed above - especially code my own db storage and views integration.
my guess is that CCK doesn't help me too much here (in Drupal 6); but that it may in Drupal 7.
i think you are possibly taking another approach; which i will look at as well.. which is create the fields in cck, export them, then on the node admin form for specific node type - set checkbox that on form submit imports these fields to the selected node type. I am just about to go through and look at that option - although it does seem a bit cludgy; i think that is the only option available right now.
Comment #3
liquidcms commentedwell even though i think importing cck field defs is a bit hacky - it sure is simple and i get field display management, views integration, access control and so many other features inherent to cck - seems a shame not to go this route.
and with the content_copy module enabled the code is trivial, simply add a submit callback on the node type admin form to fire off this code when my "enable custom fields" checkbox is selected:
where $type is passed from the submit callback.
hmm.. i wonder how easy the "remove fields" code will be??
Comment #4
markus_petrux commentedRe: "I am simply trying to create a module which ADDS numerous custom fields to any existing node type that i set it to."
Yes, you can.
See includes/content.crud.inc. Note that CRUD means Create Read Update Delete, field instances in this case. ;-)
If you look at the code in import function of Content Copy module, you'll see it uses CRUD methods to perform the task.
Another example. This is a snippet from Node Relationships module:
Note how it uses content_field_instance_create(), content_field_instance_read() and content_field_instance_delete().
These methods allow you to manage existing implementations of CCK fields in content types.
If you need a CCK field that is not implemented elsewhere, then you need to implement it yourself, of course. For this, look at how other CCK fields are implemented. Start with simple fields such as Text or Number.
Comment #5
liquidcms commentedinteresting... checking it out.
thanks for your time.. :)