I have a module in mind that I'd like to build. I've already created a template and the css rules to make it work, and have tested it by manually creating a custom content type.

What I'm trying to do is create the custom content type form in the module, but I need it to include a couple image upload fields.

I'm fine with making it dependent on cck/imagefield and using those mechanisms, I just haven't been able to find any useful information on how to do that.

Comments

Anonymous’s picture

This is what has worked for me:

1) Install the CCK, filefield, and imagefield modules.
2) Go to: YOURSITEADDRESS/admin/content/node-type/YOURNODETYPE/fields
3) Add a new field (field type = file, widget = image)

This creates an image upload field associated with your node type.

Hope that helps!

kasmel04’s picture

I'm not doing this for the purpose of building a site, I actually already have a site where I've deployed the template as a content type using contemplate and created the form fields manually.

What I'm trying to accomplish is to implement cck fields in a custom node form generated by a module.

This is what I have so far

<?php

function mappage_form(&$node) {
  $type = node_get_types('type', $node);

  $fields = array('title','body','field_horiz','field_vert','field_mapobj','field_maplink');

      foreach ($fields as $name) {
      $field = content_fields($name);
      $form['#field_info'][$name] = $field;
      $form += (array) content_field_form($form, $form_state, $field);
      }
   return $form;

}

?>

I'm not sure what [#field_info] is for, or what to put in there, or if this is correct for drupal 6.

I got this far from looking at http://groups.drupal.org/node/16542

Just hoping for a little more help to figure out the rest

Anonymous’s picture

what you're doing has worked for me. Here's a snippet from a form definition function in one of my custom modules:

  $form = array();
  // Must have this in order to call content_field_form.
  module_load_include('inc', 'content', 'includes/content.node_form');
  // These are the CCK fields we want to include in the form.
  $fields = array( 'field_classad_photo_1', );
  foreach ($fields as $name) {
    // Create the field by calling the CCK API.
    $field = content_fields($name);
    // This is non-display container for passing the field information along.
    $form['#field_info'][$name] = $field;
    // If you set the field value here it will be used as default for the field, see line 67 of file content.node_form.inc.
    $form_state['values'][$name] = $node->field_classad_photo_1;
    // This does the element rendering.
    $element = (array)content_field_form($form, $form_state, $field);
    // Add the field to the form.
    $form += $element;
  }
  ...
  return $form;

And there's a bunch of submit handling code as well.

kasmel04’s picture

That helped a lot! I now have a far greater appreciation for the API.

Now to get the submit code worked out. I hope to not have to ask too many more questions there.

Anonymous’s picture

if you can't get the submit code working, post what you have and we'll take a look at it together.

kasmel04’s picture

This code builds the form for a content type

function mymodule_form(&$node) {
  $type = node_get_types('type', $node);
  module_load_include('inc', 'content', 'includes/content.node_form');

$form = array();

  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => -5
  );

$form['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' => check_plain($type->body_label),
    '#default_value' => $node->body,
    '#required' => FALSE
  );
  $form['body_filter']['filter'] = filter_form($node->format);

$fields = array('field_1','field_2','field_3','field_4','field_5','field_6');

foreach ($fields as $name) {
      $field = content_fields($name);
      echo $name;
 
	 $form['#field_info'][$name] = $field;
 	 $form_state['values'][$name] = $node->$name;
 	 $element = (array)content_field_form($form, $form_state, $field);
 	 $form += $element;
 
   }
    return $form;
}

The above works beautifully. I created the fields manually using CCK, and now am trying to implement them in this module. Having tried several methods for getting the content to update upon form submission, I expect that I'm missing something altogether.

The title and body fields are saved just fine, but the CCK fields are ignored.

kasmel04’s picture

that's in there for scaffolding...I was playing with different fields types/naming conventions

Anonymous’s picture

Getting the value of these fields should follow the standard approach of using $form_state['values']. Try something like this:

function mymodule_form_submit($form, &$form_state) {
    drupal_set_message( print_r($form_state['values']['field_1'], true) );
}

to see if you are getting the value for field_1 passed into the submit handler.

cachobong’s picture

$form += (array) content_field_form works for fields that are not grouped, what should we use to handle field groups? HELP! I've been pulling my hair since last night for this. and now i'm almost bald!