diff --git a/workflow.module b/workflow.module index e5198bb..ea585c1 100644 --- a/workflow.module +++ b/workflow.module @@ -65,6 +65,7 @@ function workflow_admin_paths_alter(&$paths) { function workflow_node_tab_access($node = NULL) { global $user; if ($workflow = workflow_get_workflow_type_map_by_type($node->type)) { + // A 'conventional' workflow is added to the node type. if ($workflow = workflow_get_workflows_by_wid($workflow->wid)) { $roles = array_keys($user->roles); if ($node->uid == $user->uid) { @@ -79,6 +80,34 @@ function workflow_node_tab_access($node = NULL) { } } } + elseif ($node) { + // A 'Field API' workflow field is added to the node type. + // todo: add some kind of per-page buffer, since this function is called several times per page: + // once for the page view, and multiples times for each menu/breadcrumb. + $info = entity_get_info('node'); // $node->type); +//dpm($node); +//dpm($info); + if (isset($node->content)) { + // We're building the form in workflow_tab_form(). +//dpm(__FUNCTION__ . ' form'); +//dpm(debug_backtrace()); +//dpm($node->content); + foreach ($node->content as $field_name => $properties) { + if (isset($properties['#field_type']) && $properties['#field_type'] == 'workflowfield') { + return TRUE; + } + } + } + else { + // We're building the menu is _menu_check_access(). + // todo: a method to determine if one of the node fields is a Workflow_field. + // for now, we always set TRUE. ( !!!! ) +//dpm(__FUNCTION__ . ' menu'); +//dpm($settings = field_bundle_settings('node', $node->type)); + return TRUE; + } + } + return FALSE; } @@ -230,25 +259,9 @@ function workflow_node_load($nodes, $types) { function workflow_node_insert($node) { // Skip if there are no workflows. if ($workflow = workflow_get_workflow_type_map_by_type($node->type)) { - // If the state is not specified, use first valid state. - // For example, a new node must move from (creation) to some - // initial state. - if (empty($node->workflow)) { - $choices = workflow_field_choices($node); - if ($choices) { - $keys = array_keys($choices); - $sid = array_shift($keys); - } - else { - // This should never happen, but it did during testing. - drupal_set_message(t('There are no workflow states available. Please notify your site administrator.'), 'error'); - return; - } - } - if (!isset($sid)) { - $sid = $node->workflow; - } - // And make the transition. + // Get new state from value of workflow form field, stored in $node->workflow. + $sid = isset($node->workflow) ? $node->workflow : workflow_first_state($node); + // Make the transition. workflow_transition($node, $sid); } } @@ -260,9 +273,8 @@ function workflow_node_update($node) { // Skip if there are no workflows. if ($workflow = workflow_get_workflow_type_map_by_type($node->type)) { // Get new state from value of workflow form field, stored in $node->workflow. - if (!isset($sid)) { - $sid = $node->workflow; - } + $sid = isset($node->workflow) ? $node->workflow : workflow_first_state($node); + // Make the transition. workflow_transition($node, $sid); } } @@ -875,15 +887,22 @@ function workflow_execute_transition($node, $sid, $comment = NULL, $force = FALS * * @param object $node * The node to check. + * @param integer $current_sid + * If > 0, a Field API field is used, and Workflow type doesn't need to be calculated. + * If == 0, then a 'conventional' workflow is used, and the Node type is checked. * @return * Array of transitions. */ -function workflow_field_choices($node, $force = FALSE) { +function workflow_field_choices($node, $force = FALSE, $workflow = NULL, $current_sid = 0) { global $user; $choices = FALSE; - if ($workflow = workflow_get_workflow_type_map_by_type($node->type)) { + + if (!$workflow) { + $workflow = workflow_get_workflow_type_map_by_type($node->type); + } + if ($workflow) { + $current_sid = empty($current_sid) ? workflow_node_current_state($node) : $current_sid; $roles = array_keys($user->roles); - $current_sid = workflow_node_current_state($node); // If user is node author or this is a new page, give the authorship role. if (($user->uid == $node->uid && $node->uid > 0) || (arg(0) == 'node' && arg(1) == 'add')) { @@ -962,6 +981,43 @@ function workflow_node_previous_state($node) { } /** + * Gets the first valid state if a node doesn't have one, yet. + * param + * @param $node + * + * @return + * An integer; the first valid state ID of a new node. + */ +function workflow_first_state($node, $wid = 0) { + // Skip if there are no workflows. + if ($wid) { +return $states = workflow_get_workflow_states_by_wid($wid); +// if ($states) { +//dpm($states); +// $keys = array_keys($states); +// $sid = array_shift($keys); +// } + } + else + if ($workflow = workflow_get_workflow_type_map_by_type($node->type)) { + // If the state is not specified, use first valid state. + // For example, a new node must move from (creation) to some + // initial state. + $choices = workflow_field_choices($node); + if ($choices) { + $keys = array_keys($choices); + $sid = array_shift($keys); + } + else { + // This should never happen, but it did during testing. + drupal_set_message(t('There are no workflow states available. Please notify your site administrator.'), 'error'); + return; + } + return $sid; + } +} + +/** * See if a transition is allowed for a given role. * * @param int $tid diff --git a/workflow.pages.inc b/workflow.pages.inc