Could be it's there and I just haven't found it. Is there a token available to, say the Subscription module that indicates the current WB moderation state?

Here's my use case. I don't think it's so odd, so it's entirely possible I've just missed the way to do it.

I have authors & editors and publishers. When an editor or publisher changes the state, I need an email sent to the editor, the publisher and the original author. The Subscriptions module seems perfect for this, except I can't seem to find the right token to indicate the current WB moderation state.

Thanks in advance, and thanks for the great work.

Comments

cvining’s picture

OK, I've hacked a *_tokens.inc file so that the WB moderation status is available as a token. I don't think if I've adhered to Best Drupal Practices as there are extra tokens there that aren't needed, but it does the job for me and I'm moving on. I thought I'd contribute it here in case anyone else wants the hack. Just drop the following file in place and clear the cache.

Thanks for all the fish!

-- Cronin

../sites/all/modules/workbench_moderation/workbench_moderation.tokens.inc


/**
 * @file
 * Token integration for the workbench_moderation module.
 */

/**
 * Implements hook_token_info().
 */
function workbench_moderation_token_info() {
  // Node tokens.
  $info['tokens']['node']['workbench-moderation-new-state'] = array(
    'name' => t('Current Workbench moderation state'),
    'description' => t('Current Workbench moderation for the node.'),
    'type' => 'array',
  );

  $info['tokens']['node']['workbench-moderation-previous-state'] = array(
    'name' => t('Previous Workbench moderation state'),
    'description' => t('Previous Workbench moderation for the node.'),
    'type' => 'array',
  );


  return $info;
}

/**
 * Implements hook_tokens().
 */
function workbench_moderation_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  // Node tokens.
  if ($type == 'node' && !empty($data['node'])) {
    $node = $data['node'];
    $state = '';
    if (is_object($node) && property_exists($node, 'workbench_moderation') && isset($node->workbench_moderation['current'])){
       $state = $node->workbench_moderation['current'];
    } 
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'workbench-moderation-new-state':
            $replacements[$original] = $state->state;
            break;
        case 'workbench-moderation-previous-state':
            $replacements[$original] = $state->from_state;
            break;
       }
    }

  }

  return $replacements;
}

milesw’s picture

Status: Active » Needs review
StatusFileSize
new2.41 KB

@cvining Thanks for the code.

Here's a patch based on #1, but it adds tokens for both moderation state machine names and labels.

sgdev’s picture

@milesw, thanks for creating a patch! I made a couple of adjustments to the version you posted:

- The tokens are now grouped into a "Workbench Moderation" option to make them easier to find, and to allow other tokens to be included in the same group.

- The foreach in hook_tokens was moved inside the conditional. The reason for doing so is $state is undefined otherwise, which might throw some errors. If the $state object doesn't exist, no other processing is done.

Review when you get a chance and let me know if you have any feedback.

capysara’s picture

Applied the patch and it worked for me! Thanks!

acbramley’s picture

Title: WB state token? » Provide token support for WBM states
Version: 7.x-1.x-dev » 8.x-1.x-dev
StatusFileSize
new1.28 KB

Needed this for 8.x-1.x so have hacked together a super simple patch.

sgdev’s picture

Version: 8.x-1.x-dev » 7.x-3.x-dev
Status: Needs review » Needs work

Sorry to switch this back to 7.x, but given the new major release of 7.x-3.0, the patch needs to be reworked. #3 is no longer applicable.