clone.module 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Implementation of hook_help().
  4. */
  5. function clone_help($path, $arg) {
  6. switch ($path) {
  7. case 'admin/help#clone':
  8. $output = '<p>'. t('The clone module allows users to make a copy of an existing node and then edit that copy. The authorship is set to the current user, the menu and url aliases are reset, and the words "Clone of" are inserted into the title to remind you that you are not editing the original node.') .'</p>';
  9. $output .= '<p>'. t('Users with the "clone node" permission can utilize this functionality. A new tab will appear on node pages with the word "Clone".') .'</p>';
  10. return $output;
  11. case 'node/%/clone':
  12. $method = variable_get('clone_method', 'prepopulate');
  13. if ($method == 'prepopulate') {
  14. return t('This clone will not be saved to the database until you submit.');
  15. }
  16. }
  17. }
  18. /**
  19. * Implementation of hook_permission().
  20. */
  21. function clone_permission() {
  22. return array(
  23. 'clone node' => array('title' => t('Clone any node')),
  24. 'clone own nodes' => array('title' => t('Clone own nodes.')),
  25. );
  26. }
  27. /**
  28. * Implementation of hook_menu().
  29. */
  30. function clone_menu() {
  31. $items['admin/config/content/clone'] = array(
  32. 'access arguments' => array('administer site configuration'),
  33. 'page callback' => 'drupal_get_form',
  34. 'page arguments' => array('clone_settings'),
  35. 'title' => 'Node clone module',
  36. 'file' => 'clone.pages.inc',
  37. 'description' => 'Allows users to clone (copy then edit) an existing node.',
  38. );
  39. $items['node/%node/clone'] = array(
  40. 'access callback' => 'clone_access_cloning',
  41. 'access arguments' => array(1),
  42. 'page callback' => 'clone_node_check',
  43. 'page arguments' => array(1),
  44. 'title' => 'Clone content',
  45. 'title callback' => 'clone_action_link_title',
  46. 'title arguments' => array(1),
  47. 'weight' => 5,
  48. 'file' => 'clone.pages.inc',
  49. 'type' => MENU_LOCAL_ACTION,
  50. 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  51. );
  52. return $items;
  53. }
  54. function clone_access_cloning($node) {
  55. global $user;
  56. // Check basic permissions first.
  57. $access = clone_is_permitted($node->type) && (user_access('clone node') || ($user->uid && ($node->uid == $user->uid) && user_access('clone own nodes')));
  58. // Make sure the user can view the original node content, and create a new one..
  59. $access = $access && node_access('view', $node) && node_access('create', $node->type);
  60. // Let other modules alter this.
  61. drupal_alter("clone_access", $access, $node);
  62. return $access;
  63. }
  64. function clone_is_permitted($type) {
  65. $omitted = variable_get('clone_omitted', array());
  66. return empty($omitted[$type]);
  67. }
  68. /**
  69. * Menu title callback.
  70. */
  71. function clone_action_link_title($node) {
  72. // A hack to present a shorter title in contextual links.
  73. if (current_path() != 'node/' . $node->nid) {
  74. return t('Clone');
  75. }
  76. if (variable_get('clone_use_node_type_name', 0)) {
  77. return t('Clone this !type', array('!type' => drupal_strtolower(node_type_get_name($node))));
  78. }
  79. return t('Clone content');
  80. }
  81. /**
  82. * Implementation of hook_node_type_delete().
  83. */
  84. function clone_node_type_delete($info) {
  85. variable_del('clone_reset_'. $info->type);
  86. }
  87. /**
  88. * Implementation of hook_node_type_update().
  89. */
  90. function clone_node_type_update($info) {
  91. if (!empty($info->old_type) && $info->old_type != $info->type) {
  92. if (variable_get('clone_reset_'. $info->old_type, FALSE)) {
  93. variable_del('clone_reset_'. $info->old_type);
  94. variable_set('clone_reset_'. $info->type, TRUE);
  95. }
  96. }
  97. }
  98. /**
  99. * Implements hook_views_api.
  100. */
  101. function clone_views_api() {
  102. return array(
  103. 'api' => 3,
  104. 'path' => drupal_get_path('module', 'clone') .'/views',
  105. );
  106. }
  107. /**
  108. * Implementation of hook_admin_paths().
  109. */
  110. function clone_admin_paths() {
  111. if (variable_get('node_admin_theme')) {
  112. $paths = array(
  113. 'node/*/clone' => TRUE,
  114. );
  115. return $paths;
  116. }
  117. }
  118. /**
  119. * Implements hook_form_BASE_FORM_ID_alter().
  120. */
  121. function clone_form_node_form_alter(&$form, $form_state, $form_id) {
  122. // Add the clone_from_original_nid value for node forms triggered by cloning.
  123. // This will make sure the clone_from_original_nid property is still
  124. // attached to the node when passing through hook_node_insert().
  125. if (!empty($form['#node']->clone_from_original_nid)) {
  126. $form['clone_from_original_nid'] = array(
  127. '#type' => 'value',
  128. '#value' => $form['#node']->clone_from_original_nid,
  129. );
  130. }
  131. }
  132. /**
  133. * Implements hook_form_FORM_ID_alter().
  134. */
  135. function clone_form_node_admin_content_alter(&$form, $form_state, $form_id) {
  136. $destination = drupal_get_destination();
  137. // The property attribute changes in the $form array depending on the user role.
  138. $property = isset($form['admin']['nodes']['#options']) ? '#options' : '#rows';
  139. if (empty($form['admin']['nodes'][$property])) {
  140. return;
  141. }
  142. // Expose a Clone operation on each node.
  143. foreach($form['admin']['nodes'][$property] as $nid => &$row){
  144. $node = node_load($nid);
  145. if (clone_access_cloning($node)) {
  146. $row['operations']['data']['#links']['clone'] = array(
  147. 'title' => t('clone'),
  148. 'href' => 'node/' . $nid . '/clone',
  149. 'query' => $destination,
  150. );
  151. }
  152. }
  153. }