2

I'm currently building a system to create rapports for a set of possible units. So far I have built a set of content types in CCK that acts like templates where the templates fields are used to add structures that the users creating the rapport will fill out.

I have, in my template, a list of "key value names" that will then be added to the rapport and the user can fill them out.

The Template:
a, b, c

The rapport:
* a = x
* b = y
* c = z

where x,y,z is entered by the user generating the rapport.

I found this here on drupal.stackexchange.com and I guess it's on the right track. But I don't understand what I would fill in 'hooks' with and how to limit the action to only trigger on(/before) node creation. There is also a number of sub-nodes that needs to be created based on the templates, but that part I haven't research yet, if it is connected please include information about it in your answer if possible.

  • For hooks you might want to use 'nodeapi' => array('presave') to trigger it before the node is saved. Alternatively, if you want to use rules for this I can show you how a custom rules event and action can be done. – jordojuice Aug 28 '11 at 6:22
1

Per the question you linked to, the Rules module will do this very well and can be used to call your custom actions when criteria are met during pre-save, validation, or other stages you might need.

It's worth pointing out that, since you want custom nodes created, you're likely to already need to be writing custom code. In this case a better approach might be to write an implementation of hook_nodeapi() (Drupal 6) or hook_node_{operation}() (Drupal 7) that means you can fire your own custom code at specific points during node creation.

Take a look at this question about how to make nodes programatically, and also this article with more detail about the process in Drupal 7 (only in Google cache). Extract of the article:

1 Initialize a node object

 $node = new stdClass(); // We create a new node object $node->type =
 "page"; // Or any other content type you want $node->title = "Your
 title goes jere"; $node->language = LANGUAGE_NONE; // Or any language
 code if Locale module is enabled. More on this below * $node->path =
 array('alias' => 'your node path'); // Setting a node path
 node_object_prepare($node); // Set some default values. $node->uid =
 1; // Or any id you wish

*We set LANGUAGE_NONE for $node->language, if you don’t have the locale module enabled, the node will not be assigned any particular language. So that's why we put here the constant LANGUAGE_NONE. In Drupal, nodes and fields can exist in more that one language, so if your site is multilingual you should specify the language code for your field. You can configure languages and get language codes by going this path in Drupal administration: Configuration -> Regional and language -> Languages.

2 Add a body field

// Let's add standard body field
$node->body[$node->language][0]['value'] = 'This is a body text';
$node->body[$node->language][0]['summary'] = 'Here goes a summary';
$node->body[$node->language][0]['format'] = 'filtered_html'; // If
field has a format, you need to define it. Here we define a default
filtered_html format for a body field

3 Add custom fields

// Let's add some CCK/Fields API field. This is pretty similar to the
body example  $node->field_custom_name[$node->language][0]['value'] =
'This is a custom field value'; // If your custom field has a format,
don't forget to define it here
$node->field_custom_name[$node->language][0]['format'] = 'This is a
custom field value'; // And etc. you can add as much fields here as
your content type has. The sky is the limit... and the server specs,
of course ;)

4 Add file / image fields

// Some file on our system $file_path =
drupal_realpath('somefile.png'); // Create a File object $file =
(object) array(   'uid' => 1,   'uri' => $file_path,   'filemime' =>
file_get_mimetype($file_path),   'status' => 1, );  $file =
file_copy($file, 'public://'); // Save the file to the root of the
files directory. You can specify a subdirectory, for example,
'public://images'  $node->field_image[LANGUAGE_NONE][0] =
(array)$file; //associate the file object with the image field:

5 Add a term to a node

$node->field_tags[$node->language][]['tid'] = 1;

field_tags here is the name of a term reference field attached to your content type, 1 is a term id you wish to assign to a node. Simple!

6 Save a node

$node = node_submit($node); // Prepare node for a submit
node_save($node); // After this call we'll get a nid
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.