workflowfield.formatter.inc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * @file
  4. * Defines a Workflow formatter.
  5. * You won't find a DefaultFormatter, because:
  6. * - The 'default' formatter provided by the List module;
  7. * - The 'workflow' formatter is only representing the WorkflowDefault Widget.
  8. *
  9. * All hooks are wrapper functions for a D8-style WorkflowDefaultWidget object.
  10. */
  11. /**
  12. * Implements hook_field_formatter_info().
  13. */
  14. function workflowfield_field_formatter_info() {
  15. return WorkflowDefaultWidget::settings();
  16. }
  17. /**
  18. * Implements hook_field_formatter_view().
  19. *
  20. * Shows current State + Widget on a Node View page or a Workflow History tab.
  21. */
  22. function workflowfield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode = LANGUAGE_NONE, $items = array(), $display = array()) {
  23. global $user; // @todo #2287057: OK?
  24. // @todo: Perhaps global user is not always the correct user.
  25. // E.g., on ScheduledTransition->execute()? But this function is mostly used in UI.
  26. $field_name = isset($field['field_name']) ? $field['field_name'] : '';
  27. $field_id = isset($field['id']) ? $field['id'] : 0;
  28. $current_sid = workflow_node_current_state($entity, $entity_type, $field_name);
  29. $current_state = workflow_state_load_single($current_sid);
  30. $list_element = array();
  31. if ($field_name) {
  32. // First compose the current value with the normal formatter from list.module.
  33. $list_element = workflow_state_formatter($entity_type, $entity, $field, $instance, $current_sid);
  34. }
  35. elseif (!empty($field['settings']['widget']['current_status'])) {
  36. $list_element = workflow_state_formatter($entity_type, $entity, $field, $instance, $current_sid);
  37. }
  38. // Check permission, so that even with state change rights,
  39. // the form can be suppressed from the node view (#1893724).
  40. if (!user_access('show workflow state form')) {
  41. return $list_element;
  42. }
  43. if ($entity_type == 'comment') {
  44. // No Workflow form allowed on a comment display.
  45. // (Also, this avoids a lot of error messages.)
  46. return $list_element;
  47. }
  48. // Only build form if user has possible target state(s).
  49. if (!$current_state->showWidget($entity_type, $entity, $field_name, $user, FALSE)) {
  50. return $list_element;
  51. }
  52. // Add the form/widget to the formatter, and include the nid in the form id,
  53. // to allow multiple forms per page (in listings, with hook_forms() ).
  54. // Ultimately, this is a wrapper for WorkflowDefaultWidget.
  55. $entity_id = entity_id($entity_type, $entity);
  56. $form_id = implode('_', array('workflow_transition_form', $entity_type, $entity_id, $field_id));
  57. $element = drupal_get_form($form_id, $field, $instance, $entity_type, $entity);
  58. return $element;
  59. }