root/branches/kester/drupal-6/sites/default/modules/node_form_merge/node_form_merge.module @ 421

Revision 421, 3.0 KB (checked in by kester, 5 months ago)

generic module to merge node form(s) onto a form and handle validation and submitting

Line 
1<?php
2/**
3 * @file
4 * Helper functions for hook_form_alter to merge in node forms content fields.
5 */
6
7/**
8 * Merge node forms onto a single form.
9 *
10 * @param array $form
11 *   original form as passed to hook_form_alter.
12 * @param array $form_state
13 *   original form_state passed to hook_form_alter.
14 * @param object or array $nodes
15 *   a variable number nodes to be merged into the form.
16 *
17 * $node can be the node object retrieved by node_load() if it exists.
18 * Otherwise, $node must contain, at least:
19 *   type - the node type
20 *   uid - the user id
21 *   name - the user name
22 *   title - the node title
23 * as these are needed for saving the new node.
24 */
25function node_form_merge(&$form, &$form_state) {
26  $nodes = func_get_args();
27  if (count($nodes) < 3) {
28    // no nodes passed just required args
29    return;
30  }
31  else {
32    array_shift($nodes);
33    array_shift($nodes);
34  }
35
36  require_once drupal_get_path('module', 'node') .'/node.pages.inc';
37
38  foreach ($nodes as $node) {
39    if (is_array($node)) {
40      $node = (object) $node;
41    }
42    // generate form
43    $node_form = drupal_retrieve_form($node->type .'_node_form', $form_state, $node);
44    drupal_prepare_form($node->type .'_node_form', $node_form, $form_state);
45
46    // merge fields
47    $form['#field_info'] = array_merge($node_form['#field_info'], $form['#field_info']);
48    foreach ($node_form['#field_info'] as $field_name => $info) {
49      $form[$field_name] = $node_form[$field_name];
50    }
51    if (module_exists('fieldgroup') && $groups = fieldgroup_groups($node->type)) {
52      foreach ($groups as $group_name => $info) {
53        // add fieldsets with all their fields too
54        $form[$group_name] = $node_form[$group_name];
55      }
56    }
57
58    // Node if might be need by some #ahah callbacks
59    // merging array keys not already on the #node
60    $form['#node'] = array_merge((array) $node_form['#node'], (array)$form['#node']);
61
62    // store the orginal for validation and submission
63    $form['#form_merge_data'][$node->type] = $node;
64
65    // finally we do want to process the added fields
66    $form['#validate'][] = 'node_form_merge_validate';
67  }
68}
69
70/**
71 * Validates, and submits, the merged node forms.
72 */
73function node_form_merge_validate($form, &$form_state) {
74  // recover embedded form
75  foreach ($form['#form_merge_data'] as $type => $node_form) {
76    $node = $node_form['#node'];
77    // in reverse from before get the field info and put it into the 'form'
78    foreach ($node_form['#field_info'] as $field_name => $info) {
79      $node_form['values'][$field_name] = $form_state['values'][$field_name];
80    }
81    // and the fieldsets if there were any
82    if (module_exists('fieldgroup') && $groups = fieldgroup_groups($type)) {
83      foreach ($groups as $group_name => $group) {
84        foreach ($group['fields'] as $field_name => $info) {
85          $node_form['values'][$field_name] = $form_state['values'][$field_name];
86        }
87      }
88    }
89
90    // this is run with op as save as well
91    $node_form['values']['op'] = $form_state['values']['op'];
92    drupal_execute($type .'_node_form', $node_form, $node);
93  }
94}
Note: See TracBrowser for help on using the browser.