Programmatically create node w/ flexifield?
Jeremy - August 17, 2009 - 19:24
| Project: | Flexifield |
| Version: | 6.x-1.0-alpha5 |
| Component: | Documentation |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
I'm try to programatically create a node that has some flexifield content. For normal CCK content, I do something like:
$node = new stdClass();
$node->type = 'custom';
$node->status = 1;
$node->title = 'foo';
$node->field_bar[0]['value'] = 'test value';
$node = node_submit($node);
node_save($node);This populates the type, status, title, and a custom 'bar' field.
I was hoping to populate a flexifield I might be able to do something like:
$node = new stdClass();
$node->type = 'custom';
$node->status = 1;
$node->title = 'foo';
$node->field_flexisample[0]['value']['field_one'][0]['value'] = 'test value one';
$node->field_flexisample[0]['value']['field_two'][0]['value'] = 'test value two';
$node = node_submit($node);
node_save($node);This doesn't work.
What is the correct way to programmatically create a node w/ a flexifield?

#1
Same here. I'm using flexifield with node reference.
I use the following code:
<?php$node->field_age_interval[0]['type'] = "flexi_age_intervals"; // content type name
$node->field_age_interval[0]['value']['field_flexi_field_age_interval'][0]['nid'] = 131; // value
$node->field_age_interval[0]["_weight"] = 0;
?>
Any suggestions?
#2
I ended up upgrading to CCK 3 to solve my problem, utilizing multigroup to do what I needed to do. It then proved simple to programatically create nodes.
#3
Interesting. I'll give it a try.
Thanks alot.
#4
Jeremy,
Based on your suggestion, I looked into CCK 3 and Multigroup. However, it appears that CCK 3 Multigroup has the same problem of adding empty items that Flexifield has.
Please see my entry concerning empty items in Flexifield: http://drupal.org/node/442834#comment-2246818
Also, the open issue concerning empty fields in CCK 3 Multigroup is here: http://drupal.org/node/522564
"What do empty items have to do with programmatically adding nodes with Flexifield?" you ask.
It's related only by my coding investment. I'm achieving everything I want with Flexifield except two things:
1) Getting rid of the empties
2) Automatically filling in my "Key" field with information in the node pointed to by the "Song" node reference.
The node_save method above looks like a great way to do the update for #2, but, as you say, that doesn't work. So, I'm looking for a way to code a solution with Flexifield.
I really wouldn't mind that much for the moment doing the MySQL table manipulation directly (I know, I know, but I'm desperate to get this working quickly). However, I have to admit I'm a bit lost. As you can see in my post (the one I referenced above), there are at least three tables involved and I still can't delete a Flexifield item. In general, I've rapidly gone from a Drupal user, to themer, to coder and my understanding is like Swiss cheese. (No offense to the Swiss, of course). I'm familiar with LAMP in general, just not the inner workings of Drupal and particularly not CCK and Flexifield. So, any help you or anyone else could give me would be awesome...even pointing me to documentation and saying "RTFM"...because I've been looking for the right M.
Blessings,
Andrew
#5
Let the truth be told. node_save does not work to change info as in #2 above (or add it as in your case). However, it DOES work to delete whole Flexifield items. See http://drupal.org/node/442834#comment-2253380
Since that works, I'm thinking there must be a way to tell the API that fields have changed (mark them as dirty?) or are new when you call node_save. I'm pretty sure I saw a code snippet once that marked the whole node as new before the call to node_save. That would help in your situation, but not in mine. Can anyone out there help?