I'm having a hard time figuring this out. Could someone look at my schematic, and see if I'm going about it the right way, and maybe help me find the proper calls to make?
This is with HEAD CVS. I had it working with 4.6, but it seems to all be different now, and I'm still trying to make sense of the new process.
I'm trying to create an edit page for a node that will have several fields it will take care of (this part already works). After that, on the same page, I want to display the form to add an image node. Once a new image has been uploaded & the appropriate image node created, I want the new $nid of the image node returned for use by my other node.
Here's a rough flowchart in psuedocode:
function image_tab_display_tab
{
Always display the other variables (already works)
switch ($op)
case ($op is "Submit")
{
Make any changes to variables from above (already works)
Validate the new image
If the new image validates
{
Save the new image node
Associate the new nid (works when I can get proper nid)
Show a new form to upload a new image if desired
}
else
{
Show any error messages from validation
Show the preview form for the image node
}
case ($op is "Preview")
{
validate new image
show any error messages from validation
show preview form for the image node
}
default:
{
Show the "add image" form
}
}
}
For 4.6, I used node_submit somewhere in there, but it of course doesn't work any longer. Also, node_add doesn't seem to format the form properly anymore.
I'm having real problems figuring this out. Here's what I have right now, but it completely fails.
function image_tab_display_tab() {
$op = $_POST['op'];
$edit = $_POST['edit'];
$node = node_load(array('nid' => arg(1)));
$edit = array2object($edit);
$output = '';
if ($node->nid) {
switch ($op) {
case t('Submit'): {
// Do some unrelated things (this part works)
// ...
$edit = node_validate($edit);
if ($edit->nid && !form_get_errors()) {
if (node_access('update', $edit)) {
node_save($edit);
}
}
else if (!form_get_errors()) {
if (node_access('create', $edit)) {
node_save($edit);
}
}
// do some stuff with the returned $edit->nid
}
case t('Preview'): {
// Do some unrelated things (this part works)
// ...
// This doesn't work properly
$output .= node_preview(node_validate($edit));
// This works as expected
drupal_set_title(t("$node->title (preview new image)"));
break;
}
default: {
// put some unrelated fields; this part shows up okay
// ...
// This puts in the upload field okay, but is missing many fields
$output .= node_add('image');
drupal_set_title(t('%node-title (images)', array('%node-title' => $node->title)));
}
}
print theme('page', $output);
}
When I execute this code, it displays a broken form for uploading an image. When I upload an image, it creates an empty node with nothing associated, and returns the value of the empty node (at least the association part is working). If I preview it, I can verify that it gets at least the title & description meant for the new node, which is also saved properly. It's the type & uploaded file that are getting mangled in the process. And, of course, it doesn't actually preview the uploaded image, and mangles the form.
Help!
- Aaron
Culture Fix Web Identity & Design
Digital Folk Art (my blog)