next.action.inc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @file
  4. * VBO action to modify entity values (properties and fields).
  5. */
  6. /**
  7. * Implements hook_action_info().
  8. *
  9. * Registers custom VBO actions as Drupal actions.
  10. */
  11. function workflow_vbo_next_action_info() {
  12. return array(
  13. 'workflow_vbo_next_state_action' => array(
  14. 'type' => 'entity',
  15. 'label' => t('Change workflow state of post to next state'),
  16. 'configurable' => FALSE,
  17. 'triggers' => array('any'),
  18. ),
  19. );
  20. }
  21. /**
  22. * Implements a Drupal action. Move a node to the next state in the workflow.
  23. *
  24. * @param $entity
  25. * @param array $context
  26. */
  27. function workflow_vbo_next_state_action($entity, array $context) {
  28. global $user;
  29. // Get the entity type, entity and entity ID.
  30. if (isset($context['entity_type'])) {
  31. // In a VBO Action.
  32. $entity_type = $context['entity_type'];
  33. }
  34. else {
  35. // In an Advanced Action.
  36. $entity_type = str_replace(array('_insert', '_update' , '_delete'), '', $context['hook']);
  37. }
  38. // Change the state of latest revision, not current revision.
  39. if (isset($context[$entity_type])) {
  40. $entity = $context[$entity_type];
  41. }
  42. elseif (!isset($entity)) {
  43. $entity = $context['node'];
  44. }
  45. // In 'after saving new content', the node is already saved. Avoid second insert.
  46. // Todo: clone?
  47. unset($entity->is_new);
  48. list($entity_id, , $entity_bundle) = entity_extract_ids($entity_type, $entity);
  49. if (!$entity_id) {
  50. watchdog('workflow_vbo', 'Unable to get current entity ID - entity is not yet saved.');
  51. return;
  52. }
  53. // Get the current State Id. Also, $field_name will be set magically, by reference.
  54. $field_name = NULL;
  55. $current_sid = workflow_node_current_state($entity, $entity_type, $field_name);
  56. // Get the Comment. It is empty.
  57. $comment = '';
  58. // Only 'normal' state transitions are valid.
  59. $force = FALSE;
  60. // Get the node's new State Id (which is the next available state).
  61. $workflow = workflow_get_workflows_by_type($entity_bundle, $entity_type);
  62. if (!$workflow) {
  63. watchdog('workflow_vbo', 'Unable to get current workflow of entity %id.',
  64. array('%id' => $entity_id));
  65. return;
  66. }
  67. $new_sid = $workflow->getNextSid($entity_type, $entity, $field_name, $user, $force);
  68. if (!$new_sid) {
  69. watchdog('workflow_vbo', 'Unable to get current workflow state of entity %id.',
  70. array('%id' => $entity_id));
  71. return;
  72. }
  73. // Fire the transition.
  74. $transition = new WorkflowTransition();
  75. $transition->setValues($entity_type, $entity, $field_name, $current_sid, $new_sid, $user->uid, REQUEST_TIME, $comment);
  76. workflow_execute_transition($entity_type, $entity, $field_name, $transition, $force);
  77. }