I would like to create a page which would allow a user to change fields on multiple nodes at once (in a table) and then update the changes to all the nodes in one go. What may complicate things further is that some of these fields are additional flexinode fields.

I.e.

+-------------------------------+
|Node   | Field      | Field    |
+-------------------------------+
|node1  | [Entry 1]  | [Entry 2]|
|node2  | [Entry 1]  | [Entry 2]|
|node3  | [Entry 1]  | [Entry 2]|
|node4  | [Entry 1]  | [Entry 2]|
|node5  | [Entry 1]  | [Entry 2]|
+-------------------------------+
           [Update nodes]

Could anyone point me in the direction of some snippetts/examples of how I could go about doing this in 4.7? I'm still getting my head around the new Forms stuff, and I work best with examples!

Thanks.

Comments

gushie’s picture

Does the lack of reply mean that it's never been done before so there are no examples, or it would be very difficult to do? I did try searching, but had difficulty finding anything relevant in the search results.

aw-1’s picture

I would like some opinions or tips on that subject as well. Has it been done, is there an example (must not be an official Drupal module)? Or if there is none: is it for technical reasons ("broken by design"), or has such a function simply not been desired until now?

Some basic thoughts: Maybe it is possible to implement a kind of "multiNodeEdit" module, which basically is a wrapper for other modules. To display multiple forms, multiNodeEdit invokes several calls of the others modules' underlying hook_form(), and upon submit multiple calls of its hook_update() function are executed.
One problem is probably the passing of parameters from Drupal to the hooks of multiNodeEdit, because it needs an array of nodes.
(I should add that I am at a early stage of understanding the module concept. Just wanted to share some thoughts, if they are nonsense please let me know.)

gushie’s picture

I did finally come up with a solution after looking at the code for the admin->content (since it can mass update several nodes at once) and I discovered that the '#tree' attribute is quite important too! Once I discovered the forms quickstart guide and got my head around it, everything fell into place.

Here is a cut down version of what I did (without permission checks! Do not use this as-is!)

$result = db_query("SELECT {node}.nid FROM {node} WHERE ...");	
$form['#tree'] = 1;
while ($node = db_fetch_object($result)) {
    $node = node_load($node->nid);
    $form['nid'][$node->nid] = array( '#type' => 'hidden', '#value' => $node->nid);
    $form['title'][$node->nid] = array('#type' => 'textfield', '#default_value' => $node->title);
    $form['custom'][$node->nid] = array('#type' => 'textfield', '#default_value' => $node->flexinode_1);
}
$form['submit'] = array('#type' => 'submit', '#value' => 'Save changes');
$output = drupal_get_form('myform', $form);
print $output;

function myform_submit($form_id, $form_values){
    foreach (element_children($form_values['nid']) as $nid) {
        $node = node_load($nid);
        $node->title = $form_values['title'][$nid];
        $node->flexinode_1 = $form_values['custom'][$nid];		  
        node_save($node);
    }
}
function theme_myform($form){
    $header['nid'] = NULL;
    $header['title'] = 'Title';
    $header['custom'] = 'Custom field';
    foreach (element_children($form['nid']) as $key) { 
        $row = array(); 
        $row[] = form_render($form['nid'][$key]);
        $row[] = form_render($form['title'][$key]);
        $row[] = form_render($form['custom'][$key]);
        $rows[] = $row;
     }
     $output .= theme('table', $header, $rows);
     $output .= form_render($form);
     return $output;	
}
Anonymous’s picture

Did you ever add permission checks to this solution or is it still a hack for testing purposes?

Thnx,
Matt