I'm working on a module that will depend on a couple of content types created by CCK. I can create the nodes programmatically and save or update them, but I was thinking about the module installation process. I was hoping I could create the content type at module installation and not force the administrtor to do it. Seeing what the cck import/export module was doing I tried this:

$values['macro'] = "\$content[type]  = array (  'name' => 'Test Content Type',  'type' => 'testtype',  'description' => '',  'title_label' => 'Title',  'body_label' => 'Body',  'min_word_count' => '0',  'help' => '',  'node_options' =>   array (    'status' => true,    'promote' => true,    'sticky' => false,    'revision' => false,  ),  'comment' => '2',  'upload' => '1',  'old_type' => 'testtype',  'orig_type' => '',  'module' => 'node',  'custom' => '1',  'modified' => '1',  'locked' => '0',);";
drupal_execute('content_copy_import_form', $values);

The big long string comes from the cck export of the content type.

Unfortunately this is failing without any meaningful error messages. I'll probably try fooling around with this outside of hook_install and hook_update_n to make the debugging a little easier, but if anyone has gone down this path, I'd value your insignt!

Comments

ac4lt’s picture

The code I pasted in above was messed up in more than one way. After some sleep, I tried this:

$values['type_name'] = '<create>'; 
$values['macro'] = "\$content[type]  = array (  'name' => 'Test Content Type',  'type' => 'testtype',  'description' => '',  'title_label' => 'Title',  'body_label' => 'Body',  'min_word_count' => '0',  'help' => '',  'node_options' =>   array (    'status' => true,    'promote' => true,    'sticky' => false,    'revision' => false,  ),  'comment' => '2',  'upload' => '1',  'old_type' => 'testtype',  'orig_type' => '',  'module' => 'node',  'custom' => '1',  'modified' => '1',  'locked' => '0',);";
drupal_set_message($values['macro']);
drupal_execute('content_copy_import_form', $values);

This results in the message:
"An error has occured adding the content type testtype.
Please check the errors displayed for more details.:

And in the watchdog log:
call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'node_type_form' was given in /home/ac4ltorg/public_html/drupal/includes/form.inc on line 218.

I'm guessing this means the 'macro' form argument is messed up but it's what came out of the export with the linefeeds removed and the '$' escaped. Puzzling...

Alex Regenbogen’s picture

I'm using the same trick, but in my install profile, to have instant CCK-types after installing Drupal.

This is what I'm using, and above this section in the profile I had already set $values, so it was polluted....
Note: The unset line does the trick....

// ADD CUSTOM CONTENT TYPES
  unset($values);
  $values['type_name'] ='<create>';
  $values['macro'] = implode("\n", file(dirname(__file__)."/member.cck"));
  drupal_execute("content_copy_import_form", $values);

I'm also using the exports from 'content_copy' in a flat file, so that I can share the easily amoungst our different profiles.

I'm currently only one step away from having the wished for near-perfect installer.... After the successful import by content_copy, it get to do a :drupal_goto('admin/content/types/'. $content_info['content types'][$type_name]['url_str'] .'/fields');, which redirects to an admin-only page....

And since we're only just installing, we're not yet logged in....
But I'm reluctant to hack my way thru CCK (upgrade-drop-in-possibility).....

Any help there would be very much appreciated!

joahking’s picture

traxer’s picture

content_types.inc (which defines node_type_form()) is only included when the drupal path is admin/content/types (see node.module,v 1.776.2.1, line 1208).

This helps: include_once './'. drupal_get_path('module', 'node') .'/content_types.inc';

--
~/.singatrue: file not found

joshk’s picture

yeah, I've gotten to the same dead-end. Ideally I think there should be some kind of check to see whether or not the drupal_goto() should fire. I will submit a patch to this effect.

------
Personal: Outlandish Josh
Professional: Chapter Three

------
Personal: Outlandish Josh
Professional: Pantheon

joshk’s picture

Aha! All I had to do was update the module to the latest dev version. Someone already submitted the issue and it was fixed:

http://drupal.org/node/115238

The current DRUPAL-5 cvs version returns the url at the end of the _submit function rather than calling drupal_goto. For install profiles, this fixes the issue!

------
Personal: Outlandish Josh
Professional: Chapter Three

------
Personal: Outlandish Josh
Professional: Pantheon

sillygwailo’s picture

Hope you don't mind, but I changed the code tags to php tags to make it a little more readable.

(Username formerly my full name, Richard Eriksson.)