workflow.block.inc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * @file
  4. * Provide block with Workflow form.
  5. *
  6. * Credits to workflow_extensions module.
  7. */
  8. /**
  9. * Implements hook_block_info().
  10. *
  11. * Re-implements the block from workflow_extensions module.
  12. */
  13. function workflow_block_info() {
  14. $blocks['workflow_transition'] = array(
  15. 'info' => t('Workflow transition form'),
  16. 'cache' => DRUPAL_NO_CACHE, // DRUPAL_CACHE_PER_ROLE will be assumed.
  17. );
  18. return $blocks;
  19. }
  20. /**
  21. * Implements hook_block_view().
  22. */
  23. function workflow_block_view($delta) {
  24. $block = array();
  25. $form = array();
  26. // @todo: how to make this work for non-nodes, like terms?
  27. $entity = NULL;
  28. if ((arg(0) == 'node') && (arg(1) !== NULL) ) {
  29. $entity_type = arg(0);
  30. $entity_id = arg(1);
  31. $entity = entity_load_single($entity_type, $entity_id);
  32. }
  33. if ($entity) {
  34. list($entity_id, , $entity_bundle) = entity_extract_ids($entity_type, $entity);
  35. if (is_null($field_name = workflow_get_field_name($entity, $entity_type, NULL, $entity_id))) {
  36. return $block;
  37. }
  38. // Get the current sid.
  39. $current_sid = workflow_node_current_state($entity, $entity_type, $field_name);
  40. $current_state = ($current_sid) ? workflow_state_load_single($current_sid) : NULL;
  41. $workflow = ($current_state) ? $current_state->getWorkflow() : NULL;
  42. if (!$workflow) {
  43. return $block;
  44. }
  45. // Show the current state and the Workflow form to allow state changing.
  46. // N.B. This part is replicated in hook_node_view, workflow_tab_page, workflow_vbo, transition_edit.
  47. // @todo: support multiple workflows per entity.
  48. // For workflow_tab_page with multiple workflows, use a separate view. See [#2217291].
  49. $field = _workflow_info_field($field_name, $workflow);
  50. $field_id = $field['id'];
  51. $instance = field_info_instance($entity_type, $field_name, $entity_bundle);
  52. if (!$field_id) {
  53. // This is a Workflow Node workflow. Set widget options as in v7.x-1.2
  54. $field['settings']['widget']['comment'] = isset($workflow->options['comment_log_tab']) ? $workflow->options['comment_log_tab'] : 1; // vs. ['comment_log_node'];
  55. $field['settings']['widget']['current_status'] = TRUE;
  56. }
  57. $form_id = implode('_', array('workflow_transition_form', $entity_type, $entity_id, $field_id));
  58. $form += drupal_get_form($form_id, $field, $instance, $entity_type, $entity);
  59. $block['content'] = $form;
  60. if ($block['content']) {
  61. $block['subject'] = t('Current state: @state', array('@state' => $current_state->label()));
  62. }
  63. }
  64. return $block;
  65. }