1

I'm using Workbench Moderation module because it matches my needs. I have a front-end form that initiate the node with some fields. Editor should review the node and fill all the required fields remaining.

So, I need to make sure that all fields are now filled with data then the state should be changed to Needs Review.

I implemented hook_workbench_moderation_transition to trigger the state change. If the next state was Needs Review, then I would make sure that all required fields were filled with data otherwise I would change the state back to the previous one and redirect to the node edit form.

What I've done so far

    function workflow_handler_workbench_moderation_transition($node, $previous_state, $new_state){
        if ($new_state == 'needs review'){
          //check if node was not reviewed before , then redirect to form
          $instatnces = field_info_instances('node',$node->type);
          //should check first if conditional_fields module is existed
          $CFS = conditional_fields_load_dependencies('node',$node->type);
          foreach ($instatnces as $key => $field_options) {
            $field = field_get_items('node', $node, $key);
            if ($field_options['required'] && !$field && !array_key_exists($key, $CFS['dependents'])) {
               //set workbench state to previous one
               workbench_moderation_moderate($node, $previous_state);
               drupal_set_message(t('Please make sure that all required fields has been reviewed'),'error');
               $url = url("node/$node->nid/edit");
               header("Location:$url");
               drupal_exit();
            }
         }
      }
   }

Problem exists when using workbench_moderation_moderate in the context above. For some reason, when I got redirected to the form to edit, and after saving, my edits were not saved on the node object and so if I opened the edit form again, I couldn't find my edits. Any ideas?

  • I spent 24 hour trying to solve this but n vain. Somebody helps me, please! – Mouneer Nov 14 '15 at 8:02
0

The Problem was that workbench_moderation_moderate() calls internally hook_workbench_moderation_transition() so I decided to do it with another technique that should cover the two ways that workbench moderation uses to change the state

First way to be covered, the links that exist in workbench moderation back end views. I implemented hook_menu_alter to intercept the request from those links to add my custom logic.

function my_custom_logic($node, $state){
   //add your logic here
}

/**
* Implements hook_menu_alter()
*/
function workflow_handler_menu_alter(&$items){
  $items["node/%node/moderation/%/change-state/%"] = array(
    'page callback' => 'workflow_handler_moderate_callback',
    'title' => 'Change moderation state',
    'page arguments' => array(1, 5),
    'load arguments' => array(3),
    'access callback' => 'workflow_handler_moderate_access',
    'access arguments' => array(1, 5),
    'type' => MENU_CALLBACK,
  );
}

function ei_workflow_handler_moderate_access($node, $state){
  return _workbench_moderation_moderate_access($node, $state);
}

function workflow_handler_moderate_callback($node, $state){
  my_custom_logic($node, $state);
  workbench_moderation_moderate_callback($node, $state);
}

Second way to be handled, the form that workbench adds in every node details page. I implemented hook_form_alter to add my submit handler before workbench moderation one and I added my logic again there.

/**
* Implements hook_form_alter()
*/
function ei_workflow_handler_form_alter(&$form, &$form_state,$form_id){
    if($form_id == 'workbench_moderation_moderate_form'){
      $form['submit']['#submit'][] ='my_custom_logic_handler';
    }
}

function my_custom_logic_handler ($form, &$form_state) {
  my_custom_logic($form_state['values']['node'], $form_state['values']['state']);
}

Hopefully this helps anyone who encounter that issue in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.