Help with CCK

Starnox - May 14, 2008 - 11:19

Been trying to follow this: http://drupal.org/node/106716

I'm having a hard time following what bits are actually needed. Say I wanted a CCK image field on my hook_form(); how would I go about coding that. If someone could post the code for that I should be able to get my head around it.

Cheers,
~Jordan

Not sure why, but here you go...

sunjournal - May 14, 2008 - 18:59

You would probably want to use another module, like imagefield, to create an image field, so let's say you wanna override the title field on a form for argument's sake:

function hook_form_alter($form_id, &$form) {
if($form_id == 'photo_element_node_form'){ //change to the id of the form you are working on
$form['title'] = array(
'#type' => 'textfield',
'#title' => 'Title',
'#default_value' => $form['title']['#default_value'],
'#required' => FALSE,
'#weight' => -6,
);
}

If you are altering multiple forms consider using a case statement instead of this simple if statement.

HTH

Thanks but I can't see how

Starnox - May 15, 2008 - 10:19

Thanks but I can't see how you are using CCK fields in the above. I want to find out how I can create a content type for my module with CCK fields, but I am getting lost in which code snippets I need to use to get them visible in the hook_form();

Maybe I am over complicating

Starnox - May 15, 2008 - 11:02

Maybe I am over complicating things, or getting the wrong end of the stick.

So I can easily create content types with CCK in the browser through Drupal. By say adding an 'Image' field.

How do I do the same as this yet get my module to automatically add the 'Image' field to the content type it creates on install?

So that the moment through hook_form(); I have a field for title and body, yet I want to add the 'Image' field onto this.

Maybe the link above is for creating your own custom CCK field? When I want to use one which is already available, yet I don't know how to add it to my custom content type.

I am not sure it is possible

pembeci - May 15, 2008 - 22:10

As you guessed that handbook page describes how to develope a custom CCK field. That's not what you need. I am not sure how you can do what you want but you can check this admin page: admin/content/types/export.
I think the code produced after exporting a field can be used in your custom module but I never dug this deep into CCK before so I don't know exactly how.

Another option may be defining your node type by using CCK, making your module dependent on CCK and then at your module's database installation create the preconfigured CCK type at the database. Does that make sense?

Yes is there any

Starnox - May 16, 2008 - 09:36

Yes is there any documentation on this?

I am not aware of any

pembeci - May 16, 2008 - 17:18

If you know what changes are taking place in the database when you create a CCK node type, you can always use MySQL's export functionality to capture those queries (by using phpmyadmin for instance). To do that you can study the database before and after. In addition, I suggest checking CCK module's code for any sql queries involving INSERT. I would do that for you but these days I am short on time.

Another path

pembeci - May 16, 2008 - 17:20

Find an existing module which does a similar thing and study its code. You can ask the developer's mailing list for such a module.

 
 

Drupal is a registered trademark of Dries Buytaert.