diff --git a/workbench_moderation.module b/workbench_moderation.module old mode 100644 new mode 100755 index b652b6f..51c5247 --- a/workbench_moderation.module +++ b/workbench_moderation.module @@ -210,6 +210,56 @@ function workbench_moderation_menu_alter(&$items) { } /** + * Implements hook_entity_load(). + * + * Hack for working with panelizer. Replace the loaded node by it's current + * version, when on panelizer editing pages. + */ +function workbench_moderation_entity_load(&$entities, $type) { + static $me = FALSE; + if ($me || $type != 'node' || !drupal_match_path($_GET['q'], 'node/*/panelizer*') || _workbench_moderation_in_shutdown_mode()) { + return; + } + $me = TRUE; + + // Look up the latest vid for the loaded nodes. + $query = db_select('node_revision', 'r'); + $query->condition('r.nid', array_keys($entities)) + ->orderBy('r.vid', 'DESC') + ->groupBy('nid') + ->fields('r', array('nid')) + ->addExpression('MAX(vid)', 'vid'); + + $vids = $query->execute()->fetchAllKeyed(); + foreach ($entities as $entity_id => $entity) { + if (isset($vids[$entity_id]) && ($entity->vid != $vids[$entity_id])) { + // Loaded revision is not the latest. Reload the latest. + $entities[$entity_id] = node_load($entity_id, $vids[$entity_id]); + } + } + $me = FALSE; +} + +/** + * Implements hook_form_alter(). + * + * Alter the panelizer forms to create new revision as appropriate. + */ +function workbench_moderation_form_alter(&$form, $form_state, $form_id) { + if (preg_match('/^panelizer_/', $form_id) && + !empty($form['revision_information']) && + !empty($form_state['entity']) && + $form_state['entity']->panelizer->entity_type == 'node') { + $form['revision_information']['revision']['#default_value'] = FALSE; + if (isset($form_state['entity']->workbench_moderation['published'])) { + if ($form_state['entity']->workbench_moderation['published']->vid == $form_state['entity']->workbench_moderation['current']->vid) { + $form['revision_information']['revision']['#default_value'] = TRUE; + } + } + } +} + +/** * Redirects 'node/%node/revisions' to node/%node/moderation * * workbench_moderation_menu_alter() changes the page callback @@ -1540,6 +1590,7 @@ function workbench_moderation_moderate($node, $state) { // We do this in a shutdown function to avoid race conditions when // running node_save() from within a node submission. if (!empty($node->workbench_moderation['published'])) { + drupal_register_shutdown_function('_workbench_moderation_in_shutdown_mode', TRUE); drupal_register_shutdown_function('workbench_moderation_store', $node); } @@ -1549,6 +1600,27 @@ function workbench_moderation_moderate($node, $state) { } /** + * Register that we are in shutdown mode. + * + * @param boolean $update_status + * Set the shutdown status. TRUE if we are in shutdown mode. + * + * @return + * Boolean indicating if the shutdown mode has been activated. + */ +function _workbench_moderation_in_shutdown_mode($update_status = NULL) { + static $shutdown_mode = FALSE; + + // Register the shutdown mode, and only change if we have not told it, that + // it is in shutdown mode. + if (isset($update_status) || !$shutdown_mode) { + $shutdown_mode = $update_status; + } + + return $shutdown_mode; +} + +/** * Shutdown callback for saving a node revision. * * This function is called by drupal_register_shutdown_function().