You are here

function workflownode_node_view in Workflow 7.2

Implements hook_node_view().

File

workflow_node/workflownode.module, line 82
Hooks and functions for the 'conventional' (version D5/D6/D7.1) Workflow Node, remnants of nodeapi.

Code

function workflownode_node_view($node, $view_mode, $langcode) {
  global $user;
  $form = array();
  $entity_type = 'node';

  // This hook is only for nodes.
  $entity_id = $node->nid;
  $field_name = '';

  // This hook is only for workflow_node.
  $entity_bundle = $node->type;

  // Skip if there are no Node API workflows.
  if (!workflow_get_workflow_type_map_by_type($entity_bundle)) {
    return;
  }

  // Check permission, so that even with state change rights,
  // the form can be suppressed from the node view (#1893724).
  if (!user_access('show workflow state form', $user)) {
    return;
  }

  // Get the current sid. $field_name is updated with relevant value.
  $current_sid = workflow_node_current_state($node, $entity_type, $field_name);
  $current_state = workflow_state_load_single($current_sid);

  // Show current state at the top of the node display.
  $field = $instance = array();
  $node->content['workflow_current_state'] = workflow_state_formatter($entity_type, $node, $field, $instance, $current_sid);
  $node->content['workflow_current_state']['#weight'] = -99;
  if ($current_state && $current_state
    ->showWidget('node', $node, '', $user, FALSE)) {
    $workflow = $current_state
      ->getWorkflow();

    // Show the current state and the Workflow form to allow state changing.
    // N.B. This part is replicated in hook_node_view, workflow_tab_page, workflow_vbo.
    if ($workflow) {
      $field = _workflow_info_field($field_name, $workflow);
      $field_id = $field['id'];
      $instance = field_info_instance($entity_type, $field_name, $entity_bundle);

      // If no instance is found, restore the array.
      if (!is_array($instance)) {
        $instance = array();
      }
      if (!$field['id']) {

        // This is a Workflow Node workflow. Set widget options as in v7.x-1.2
        $field['settings']['widget']['comment'] = isset($workflow->options['comment_log_node']) ? $workflow->options['comment_log_node'] : 1;

        // vs. ['comment_log_tab'];
        $field['settings']['widget']['current_status'] = FALSE;
      }
    }

    // By including the nid in the form id. For backwards compatiblity, do not add entity_type, field_id.
    $form_id = implode('_', array(
      'workflow_transition_form',
      $entity_id,
    ));
    $form += drupal_get_form($form_id, $field, $instance, $entity_type, $node);
    $form['#weight'] = 99;
    $node->content['workflow'] = $form;
  }
}