- Issue created by @kevgrob
When unpublishing a previously published node via the Unpublish link on the node/%/moderation page, any custom fields that have their values modified within a hook_node_presave function will not retain the newly modified values after the node is saved.
NOTE: Whenever an Article node is unpublished from the admin/content page via UPDATE OPTIONS, by selecting Unpublish selected content and then selecting the checkbox beside the node, and lastly clicking the Update button, the functionality does work as expected and the updated value for the field is permanently stored.
<?php
/**
* @file
* Test custom code using Drupal hooks.
*
*/
/**
* Implements hook_node_presave().
*/
function test_custom_node_presave($node) {
if (isset($node->field_toggle)) {
// Node is unpublished
if (!$node->is_new && $node->status == 0) {
$node->field_toggle[LANGUAGE_NONE][0]['value'] = 0;
}
}
}
On the surface and there's probably more debugging that may need to be done, as I don't know yet or how this would affect things further in the workbench_moderation module, I was able to track the surface cause down to line 1719 in the workbench_moderation.module file. If I comment out that assignment to node->original, then the updated toggle value does get saved, however as stated earlier, this may affect things further in the WM module and I am hoping those more knowledgeable can shed some light on this. Removing this line doesn't affect the workbench_moderation_state_current or workbench_moderation_state_new values, so the comment associated with that line doesn't appear to make any sense.
On line 1718, the comment states, Set the current and new moderation state value.
Then line 1719 assigns the current node object to the node->original value. Is there a particular reason why this was done here, instead of letting the the node_save function create node->original as it normally does from a completely unchanged entity? The new moderation state value is set in the workbench_moderation_moderate function and the current moderation state value is set in the workbench_moderation_set_node_state function.
Including the entire workbench_moderation_moderate function below for more context:
/**
* Provide quick moderation of nodes.
*
* Access is controlled by the menu router to these pseudo-form callbacks.
* This function is also abstracted so that it can be called from any node
* context.
*
* @see _workbench_moderation_moderate_access()
* @see workbench_moderation_menu()
* @see workbench_moderation_node_update()
*
* @param $node
* The node being acted upon.
* @param $state
* The new moderation state requested.
*/
function workbench_moderation_moderate($node, $state) {
// Set the current and new moderation state value.
$node->original = $node;
// Ensure that published nodes are flagged properly.
if ($state == workbench_moderation_state_published()) {
$node->status = NODE_PUBLISHED;
}
// If the published version is being unpublished, account for that.
elseif (isset($node->workbench_moderation['published']) && $node->workbench_moderation['published']->vid == $node->vid) {
$node->status = NODE_NOT_PUBLISHED;
}
// Set the state property for saving.
$node->workbench_moderation_state_new = $state;
// Save the node and let drafty handle it.
node_save($node);
}
Because of the forced update to node->original, the line from the node_save function that would normally load the unchanged entity will not be called, because the condition will never be met. Providing lines from node.inc for context below:
/**
* Saves changes to a node or adds a new node.
*
* @param $node
* The $node object to be saved. If $node->nid is
* omitted (or $node->is_new is TRUE), a new node will be added.
*/
function node_save($node) {
$transaction = db_transaction();
try {
// Load the stored entity, if any.
if (!empty($node->nid) && !isset($node->original)) {
$node->original = entity_load_unchanged('node', $node->nid);
}
// remainder of function omitted...
}
}
Active
3.0
Code