Background: We have a process where a user with a role of editor, logs in then manually assigns new nodes, created by visitors, to another user with the role of assigned_author. When this happens the author of the node is changed from anonymous to the selected author. The author is selected from a user reference field and the action i.e. assign from a taxonomy reference field. There can be a large backlog of nodes to assign, therefore an automatic way to do it was developed.
The built solution: Create a block that is only available to editor role. The block calculates the number of new nodes, if there are more than ten, load each one, change the value of the author field to a random author and set the action field to assign, then submit and save the node. While the editor is logged in any other new nodes are automatically assigned via the block as the editor navigates to different pages. When the editor logs out, new nodes remain unassigned until the editor logs back in.
Here is the relevant section of the module code
function mymodule_block_view($delta = '') {
$block = array();
global $user;
//do some stuff to get values
if($new_count['ndscount'] >= 10){
//$nodevalues are the nodes to modify
if($nodevalues){
foreach ($nodevalues as $key => $nid) {
$pr_author = $author_list[$authorgroup];
$node = node_load($nid);
$node->field_pr_author['und'][0]['target_id'] = drupal_convert_to_utf8($pr_author,"ISO-8859-1");
$node->field_pr_actions['und'][0]['tid'] = $pr_assign;
//problem here
$node = node_submit($node);
node_save($node);
}
}
}
}
Everything is fine on the saved nodes except for the field group values.
Example of what is typed into the node creation form:
field 1 field 2 field 3
Peter Piper
Sam M Sheppard
Bob Baker
Mary K Millner
This is what shows up on the saved node after automatic assignment via the block code:
field 1 field 2 field 3
Peter M Piper
Sam K Sheppard
Bob Baker
Mary Millner
The values are not staying the order they were entered. In the log there is an error:
Field(s) field_author_mid_name are not present in pass-through variable element from field_group_pre_render.
This is coming from the field_group_multiple.module:
if (!empty($field_not_in_elements)) {
watchdog(FIELD_GROUP_MULTIPLE, 'Field(s) %fields are not present in pass-through variable element from field_group_pre_render.', array('%fields' => implode(", ",$field_not_in_elements)), WATCHDOG_ERROR);
}
When a node with the field value in the wrong order is corrected , there is a notice
Notice: Array to string conversion in array_diff_assoc() (line 364 of /app/drupal/includes/entity.inc).
The field group values stay in the correct order if the block is turned off or I comment out the lines saving the node, and manually assign the nodes.
Comments
Comment #1
VenDG CreditAttribution: VenDG commentedComment #2
VenDG CreditAttribution: VenDG commentedDoes anyone have any idea how I can solve this?
Comment #3
egfguedes CreditAttribution: egfguedes commentedI have the same problem.
Anybody have suggestion?
tks
Comment #4
alvar0hurtad0Hi,
The problem is with the empty fields, I had the same problem and i Solved it forcing all values to not-empty.
Comment #5
VenDG CreditAttribution: VenDG commentedCould you explain some more?
In my particular case, the middlename field is supposed to be empty if there isn't one. Making all values not-empty, suggests that I would have to add code/do something to figure out where to remove the extra values.