EntityWorkflowUIController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * @file
  4. * Contains workflow_admin_ui\includes\Entity\EntityWorkflowUIController.
  5. */
  6. class EntityWorkflowUIController extends EntityDefaultUIController {
  7. /**
  8. * Provides definitions for implementing hook_menu().
  9. */
  10. public function hook_menu() {
  11. $items = parent::hook_menu();
  12. // Set this on the object so classes that extend hook_menu() can use it.
  13. $id_count = count(explode('/', $this->path));
  14. $wildcard = isset($this->entityInfo['admin ui']['menu wildcard']) ? $this->entityInfo['admin ui']['menu wildcard'] : '%entity_object';
  15. $plural_label = isset($this->entityInfo['plural label']) ? $this->entityInfo['plural label'] : $this->entityInfo['label'] . 's';
  16. $entityType = $this->entityInfo['entity class'];
  17. // @todo: Allow modules to insert their own action links to the 'workflow',
  18. // $workflow_operations = module_invoke_all('workflow_operations', 'workflow', NULL);
  19. $item = array(
  20. 'file path' => isset($this->entityInfo['admin ui']['file path']) ? $this->entityInfo['admin ui']['file path'] : drupal_get_path('module', $this->entityInfo['module']),
  21. 'access arguments' => array('administer workflow'),
  22. 'type' => MENU_LOCAL_TASK,
  23. );
  24. $items[$this->path . '/manage/' . $wildcard . '/states'] = $item + array(
  25. 'file' => 'workflow_admin_ui/workflow_admin_ui.page.states.inc',
  26. 'title' => 'States',
  27. 'weight' => '11',
  28. 'page callback' => 'drupal_get_form',
  29. 'page arguments' => array('workflow_admin_ui_states_form', $id_count + 1, $id_count + 2),
  30. );
  31. $items[$this->path . '/manage/' . $wildcard . '/transitions'] = $item + array(
  32. 'file' => 'workflow_admin_ui/workflow_admin_ui.page.transitions.inc',
  33. 'title' => 'Transitions',
  34. 'weight' => '12',
  35. 'page callback' => 'drupal_get_form',
  36. 'page arguments' => array('workflow_admin_ui_transitions_form', $id_count + 1, $id_count + 2),
  37. );
  38. $items[$this->path . '/manage/' . $wildcard . '/labels'] = $item + array(
  39. 'file' => 'workflow_admin_ui/workflow_admin_ui.page.labels.inc',
  40. 'title' => 'Labels',
  41. 'weight' => '13',
  42. 'page callback' => 'drupal_get_form',
  43. 'page arguments' => array('workflow_admin_ui_labels_form', $id_count + 1, $id_count + 2),
  44. );
  45. $items[$this->path . '/manage/' . $wildcard . '/permissions'] = $item + array(
  46. 'file' => 'workflow_admin_ui/workflow_admin_ui.page.permissions.inc',
  47. 'title' => 'Permission summary',
  48. 'weight' => '14',
  49. 'page callback' => 'workflow_admin_ui_view_permissions_form',
  50. 'page arguments' => array($id_count + 1, $id_count + 2),
  51. // @todo: convert to drupal_get_form('workflow_admin_ui_view_permissions_form');
  52. // 'page callback' => 'drupal_get_form',
  53. // 'page arguments' => array('workflow_admin_ui_view_permissions_form', $id_count + 1, $id_count + 2),
  54. );
  55. return $items;
  56. }
  57. protected function operationCount() {
  58. // Add more then enough colspan.
  59. return parent::operationCount() + 8;
  60. }
  61. /*
  62. public function operationForm($form, &$form_state, $entity, $op) {}
  63. */
  64. public function overviewForm($form, &$form_state) {
  65. // Add table and pager.
  66. $form = parent::overviewForm($form, $form_state);
  67. // Allow modules to insert their own action links to the 'table', like cleanup module.
  68. $top_actions = module_invoke_all('workflow_operations', 'top_actions', NULL);
  69. // Allow modules to insert their own workflow operations.
  70. foreach ($form['table']['#rows'] as &$row) {
  71. $url = $row[0]['data']['#url'];
  72. $workflow = $url['options']['entity'];
  73. foreach ($actions = module_invoke_all('workflow_operations', 'workflow', $workflow) as $action) {
  74. $action['attributes'] = isset($action['attributes']) ? $action['attributes'] : array();
  75. $row[] = l(strtolower($action['title']), $action['href'], $action['attributes']);
  76. }
  77. }
  78. // @todo: add these top actions next to the core 'Add workflow' action.
  79. $top_actions_args = array(
  80. 'links' => $top_actions,
  81. 'attributes' => array('class' => array('inline', 'action-links')),
  82. );
  83. $form['action-links'] = array(
  84. '#type' => 'markup',
  85. '#markup' => theme('links', $top_actions_args),
  86. '#weight' => -1,
  87. );
  88. if (module_exists('workflownode')) {
  89. // Append the type_map form, changing the form by reference.
  90. // The 'type_map' form is only valid for Workflow Node API.
  91. module_load_include('inc', 'workflow_admin_ui', 'workflow_admin_ui.page.type_map');
  92. workflow_admin_ui_type_map_form($form);
  93. }
  94. // Add a submit button. The submit functions are added in the sub-forms.
  95. $form['submit'] = array(
  96. '#type' => 'submit',
  97. '#value' => t('Save'),
  98. '#weight' => 100,
  99. );
  100. return $form;
  101. }
  102. /*
  103. * Avoids the 'Delete' action if the Workflow is used somewhere.
  104. */
  105. protected function overviewTableRow($conditions, $id, $entity, $additional_cols = array()) {
  106. // Avoid the 'delete' operation if the Workflow is used somewhere.
  107. $status = $entity->status;
  108. // @see parent::overviewTableRow() how to determine a deletable entity.
  109. if (!entity_has_status($this->entityType, $entity, ENTITY_IN_CODE) && !$entity->isDeletable()) {
  110. // Set to a state that does not allow deleting, but allows other actions.
  111. $entity->status = ENTITY_IN_CODE;
  112. }
  113. $row = parent::overviewTableRow($conditions, $id, $entity, $additional_cols);
  114. // Just to be sure: reset status.
  115. $entity->status = $status;
  116. return $row;
  117. }
  118. /**
  119. * Overrides the 'revert' action, to not delete the workflows.
  120. *
  121. * @see https://www.drupal.org/node/2051079
  122. * @see https://www.drupal.org/node/1043634
  123. */
  124. public function applyOperation($op, $entity) {
  125. $label = entity_label($this->entityType, $entity);
  126. $vars = array('%entity' => $this->entityInfo['label'], '%label' => $label);
  127. $id = entity_id($this->entityType, $entity);
  128. $edit_link = l(t('edit'), $this->path . '/manage/' . $id . '/edit');
  129. switch ($op) {
  130. case 'revert':
  131. $defaults = workflow_get_defaults($entity->module);
  132. workflow_revert($defaults, $entity->getName());
  133. watchdog($this->entityType, 'Reverted %entity %label to the defaults.', $vars, WATCHDOG_NOTICE, $edit_link);
  134. return t('Reverted %entity %label to the defaults.', $vars);
  135. case 'delete':
  136. case 'import':
  137. default:
  138. return parent::applyOperation($op, $entity);
  139. }
  140. }
  141. }