You are here

function _workflow_rules_field_set_state in Workflow 7

Action implementation: set current state, ignoring current user permission.

1 string reference to '_workflow_rules_field_set_state'
_workflow_rules_rules_field_action_info in workflow_rules/workflow_rules.field.inc
Implements subfunction of hook_rules_action_info().

File

workflow_rules/workflow_rules.rules.inc, line 154
Rules integration for the Workflow module

Code

function _workflow_rules_field_set_state($entity, $field, $sids, $comment = NULL, array $settings = array()) {
  global $user;
  $force = TRUE;

  // "ignoring current user permission."
  // Select the last state on the list.
  $new_sid = array_pop($sids);

  // This is a poorman's effort to convert a token into a field name.
  $token = isset($settings['field:select']) ? $settings['field:select'] : '';
  $field_name = _workflow_rules_token_replace($token);
  $field = field_info_field($field_name);
  $instance = array();
  if ($field && isset($entity->{$field_name})) {
    $entity_type = 'node';

    // @todo: add Entity support in workflow_rules.
    $entity_id = $entity->nid;
    $old_sid = workflow_node_current_state($entity, $entity_type, $field);

    // Todo : entity support.
    $transition = new WorkflowTransition($entity_type, $entity, $field_name, $old_sid, $new_sid, $user->uid, REQUEST_TIME, $comment);
    if ($error = $transition
      ->isAllowed($force)) {
      drupal_set_message($error, 'error');
      $sid = 0;
    }
    else {
      $sid = $transition
        ->execute($force);
    }

    // Save the entity, but not through entity_save(), since this will check permissions again and trigger rules.
    if ($sid == $new_sid) {
      $entity->{$field_name}['und'] = array();
      $entity->{$field_name}['und'][0]['workflow']['workflow_options'] = $new_sid;
      $entity->{$field_name}['und'][0]['workflow']['workflow_comment'] = $comment;
      field_attach_update($entity_type, $entity);
    }
  }
}