content_type_extras.node_type_form.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * Function for altering forms at admin/structure/types/manage/*.
  4. */
  5. function content_type_extras_node_type_form(&$form) {
  6. // Not sure what this js was for (or if it ever actually existed), but I wanted
  7. // to keep the code here just in case I remember why it was here. :)
  8. // drupal_add_js(drupal_get_path('module', 'content_type_extras') . '/js/content_type_extras.admin.js');
  9. // We need to set the weights of the 'Preview' button radios
  10. $form['submission']['node_preview']['#weight'] = 2;
  11. $type = $form['type']['#default_value'];
  12. // We need to check whether the description field should be required or not.
  13. $form['description']['#required'] = content_type_extras_get_setting('content_type_extras_descriptions_required', $type);
  14. // Add Preview button text option
  15. $form['submission']['content_type_extras_preview_button'] = array(
  16. '#type' => 'textfield',
  17. '#title' => "'Preview' button value",
  18. '#description' => t(''),
  19. '#default_value' => content_type_extras_get_setting('content_type_extras_preview_button', $type),
  20. '#weight' => $form['submission']['node_preview']['#weight'] + 1,
  21. '#states' => array(
  22. 'invisible' => array(
  23. 'input[name=node_preview]' => array('value' => '0'),
  24. ),
  25. ),
  26. );
  27. // Add the option to have a "Save and New" button added to the content type
  28. $form['submission']['content_type_extras_save_and_new'] = array(
  29. '#type' => 'radios',
  30. '#title' => "'Save and New' button",
  31. '#description' => t('If enabled, a button will be added to the bottom of node edit forms that will quickly allow the administrator to add a new content of the same type.'),
  32. '#options' => array(
  33. t('Disabled'),
  34. t('Enabled'),
  35. ),
  36. '#default_value' => content_type_extras_get_setting('content_type_extras_save_and_new', $type),
  37. '#weight' => $form['submission']['node_preview']['#weight'] + 2,
  38. );
  39. $form['submission']['content_type_extras_save_and_new_button'] = array(
  40. '#type' => 'textfield',
  41. '#title' => "'Save and New' button value",
  42. '#description' => t(''),
  43. '#default_value' => content_type_extras_get_setting('content_type_extras_save_and_new_button', $type),
  44. '#weight' => $form['submission']['content_type_extras_save_and_new']['#weight'] + 1,
  45. '#states' => array(
  46. 'visible' => array(
  47. 'input[name=content_type_extras_save_and_new]' => array('value' => '1'),
  48. ),
  49. ),
  50. );
  51. // Add the option to have a "Save and Edit" button added to the content type
  52. $form['submission']['content_type_extras_save_and_edit'] = array(
  53. '#type' => 'radios',
  54. '#title' => "'Save and Edit' button",
  55. '#description' => t('If enabled, a button will be added to the bottom of node edit forms that will quickly allow the administrator to save the current node and continue editing it.'),
  56. '#options' => array(
  57. t('Disabled'),
  58. t('Enabled'),
  59. ),
  60. '#default_value' => content_type_extras_get_setting('content_type_extras_save_and_edit', $type),
  61. '#weight' => $form['submission']['node_preview']['#weight'] + 4,
  62. );
  63. $form['submission']['content_type_extras_save_and_edit_button'] = array(
  64. '#type' => 'textfield',
  65. '#title' => "'Save and Edit' button value",
  66. '#description' => t(''),
  67. '#default_value' => content_type_extras_get_setting('content_type_extras_save_and_edit_button', $type),
  68. '#weight' => $form['submission']['content_type_extras_save_and_edit']['#weight'] + 1,
  69. '#states' => array(
  70. 'visible' => array(
  71. 'input[name=content_type_extras_save_and_edit]' => array('value' => '1'),
  72. ),
  73. ),
  74. );
  75. // Add the option to have a "Cancel" button added to the content type
  76. $form['submission']['content_type_extras_cancel'] = array(
  77. '#type' => 'radios',
  78. '#title' => "Cancel button",
  79. '#description' => t('If enabled, a button will be added to the bottom of node edit forms that will allow the administrator to go back to the previous page without saving any changes.<br><strong>NOTE:</strong> This feature requires that the admin has javascript enabled.'),
  80. '#options' => array(
  81. t('Disabled'),
  82. t('Enabled'),
  83. ),
  84. '#default_value' => content_type_extras_get_setting('content_type_extras_cancel', $type),
  85. '#weight' => $form['submission']['node_preview']['#weight'] + 6,
  86. );
  87. // Set weight of help text area
  88. $form['submission']['help']['#weight'] = $form['submission']['node_preview']['#weight'] + 7;
  89. // Set 'Title field label'
  90. $form['submission']['title_label']['#default_value'] = content_type_extras_get_setting('title_label', $type);
  91. // Set 'Preview' button default
  92. $form['submission']['node_preview']['#default_value'] = content_type_extras_get_setting('node_preview', $type);
  93. // Set 'Display author and date information.'
  94. $form['display']['node_submitted']['#default_value'] = content_type_extras_get_setting('node_submitted', $type);
  95. // Set 'Publishing options'
  96. $form['workflow']['node_options']['#default_value'] = content_type_extras_get_setting('node_options', $type);
  97. if (module_exists('comment')) {
  98. // A new content type is being added
  99. if (empty($form['#node_type']->name)) {
  100. $comment_settings = content_type_extras_get_setting('comment', $type);
  101. $form['comment']['comment']['#default_value'] = $comment_settings['comment'];
  102. $form['comment']['comment_default_mode']['#default_value'] = $comment_settings['default_mode'];
  103. $form['comment']['comment_default_per_page']['#default_value'] = $comment_settings['default_per_page'];
  104. $form['comment']['comment_anonymous']['#default_value'] = $comment_settings['anonymous'];
  105. $form['comment']['comment_subject_field']['#default_value'] = $comment_settings['subject_field'];
  106. $form['comment']['comment_form_location']['#default_value'] = $comment_settings['form_location'];
  107. $form['comment']['comment_preview']['#default_value'] = $comment_settings['preview'];
  108. }
  109. }
  110. if (module_exists('xmlsitemap')) {
  111. // XML Sitemap stores its variables a little differently, so we have to adjust for it here.
  112. $xmlsitemap_settings = content_type_extras_get_setting('xmlsitemap_settings', 'node_' . $type);
  113. $form['xmlsitemap']['status']['#default_value'] = $xmlsitemap_settings['status'];
  114. $form['xmlsitemap']['priority']['#default_value'] = $xmlsitemap_settings['priority'];
  115. }
  116. if (module_exists('scheduler')) {
  117. // Scheduler stores its variables a little differently, so we have to adjust for it here.
  118. $scheduler_settings = content_type_extras_get_setting('scheduler_settings', 'node_' . $type);
  119. $form['scheduler']['publish']['scheduler_publish_enable']['#default_value'] = $scheduler_settings['publish_enable'];
  120. $form['scheduler']['publish']['scheduler_publish_touch']['#default_value'] = $scheduler_settings['publish_touch'];
  121. $form['scheduler']['publish']['scheduler_publish_require']['#default_value'] = $scheduler_settings['publish_require'];
  122. $form['scheduler']['publish']['scheduler_publish_revision']['#default_value'] = $scheduler_settings['publish_revision'];
  123. $form['scheduler']['unpublish']['scheduler_unpublish_enable']['#default_value'] = $scheduler_settings['unpublish_enable'];
  124. $form['scheduler']['unpublish']['scheduler_unpublish_require']['#default_value'] = $scheduler_settings['unpublish_require'];
  125. $form['scheduler']['unpublish']['scheduler_unpublish_revision']['#default_value'] = $scheduler_settings['unpublish_revision'];
  126. }
  127. // Get all available user roles
  128. $roles = user_roles();
  129. $admin_role = variable_get('user_admin_role', 0);
  130. if ($admin_role != 0) {
  131. $roles[$admin_role] .= t(' <em>(administrator role)</em>');
  132. }
  133. $create_roles = array();
  134. $edit_roles = array();
  135. $delete_roles = array();
  136. // If we are on an existing content type form
  137. if (!empty($form['name']['#default_value'])) {
  138. $create_roles = user_roles(FALSE, 'create ' . $form['#node_type']->type . ' content');
  139. $edit_roles = user_roles(FALSE, 'edit any ' . $form['#node_type']->type . ' content');
  140. $delete_roles = user_roles(FALSE, 'delete any ' . $form['#node_type']->type . ' content');
  141. }
  142. // We are creating a new content type
  143. else {
  144. $user_permissions = content_type_extras_get_setting('user_permissions', '');
  145. $selected_perms = content_type_extras_get_selected_roles($user_permissions);
  146. $create_roles = $selected_perms['create_roles'];
  147. $edit_roles = $selected_perms['edit_roles'];
  148. $delete_roles = $selected_perms['delete_roles'];
  149. }
  150. $permission_select = content_type_extras_get_default('content_type_extras_user_permissions_select');
  151. if ($permission_select == 'cte') {
  152. // We need to remove FPA's implementation
  153. unset($form['fpa_fieldset']);
  154. $form['user_permissions'] = array(
  155. '#type' => 'fieldset',
  156. '#title' => t('User permissions'),
  157. '#description' => t('The below permissions duplicate the permissions set on the <a href="/admin/people/permissions">Permissions</a> page. It is provided here for convenience.'),
  158. '#group' => 'additional_settings',
  159. '#tree' => TRUE,
  160. 'create_roles' => array(
  161. '#type' => 'checkboxes',
  162. '#title' => t('Roles that can CREATE content of this type'),
  163. '#options' => $roles,
  164. '#default_value' => array_keys($create_roles),
  165. ),
  166. 'edit_roles' => array(
  167. '#type' => 'checkboxes',
  168. '#title' => t('Roles that can EDIT any content of this type'),
  169. '#options' => $roles,
  170. '#default_value' => array_keys($edit_roles),
  171. ),
  172. 'delete_roles' => array(
  173. '#type' => 'checkboxes',
  174. '#title' => t('Roles that can DELETE any content of this type'),
  175. '#options' => $roles,
  176. '#default_value' => array_keys($delete_roles),
  177. ),
  178. );
  179. }
  180. $form['extras'] = array(
  181. '#type' => 'fieldset',
  182. '#title' => t('Extra settings'),
  183. '#group' => 'additional_settings',
  184. 'content_type_extras_title_hide' => array(
  185. '#type' => 'checkbox',
  186. '#title' => t('Hide node titles from displaying'),
  187. '#description' => t('If checked, node titles for this content type will be hidden by default. Users with the <a href="/admin/people/permissions#module-content_type_extras">appropriate permission</a> can override this on a per-node basis.'),
  188. '#default_value' => content_type_extras_get_setting('content_type_extras_title_hide', $type),
  189. '#weight' => 0,
  190. ),
  191. );
  192. if (!empty($form['#node_type']->is_new)) {
  193. // I decided to make this option only available when creating content types because:
  194. // 1. Once the content type is created, the admin can manage the body field just like
  195. // any other field under "Manage fields"
  196. // 2. We would have to provide some kind of data checking on existing content types
  197. // to see if there was already data entered in the body field for that content type
  198. // and, if so, how the admin wanted to handle that...that just doesn't make sense!
  199. $form['extras']['content_type_extras_remove_body'] = array(
  200. '#type' => 'checkbox',
  201. '#title' => t('Remove body field from this content type'),
  202. '#default_value' => content_type_extras_get_setting('content_type_extras_remove_body', $type),
  203. '#weight' => 10,
  204. );
  205. }
  206. if (module_exists('pathauto')) {
  207. $form['extras']['pathauto_node'] = array(
  208. '#type' => 'textfield',
  209. '#title' => t('Path alias'),
  210. '#description' => t('This is a shortcut method to using the normal !link.', array('!link' => l(t('pathauto settings'), 'admin/config/search/path/patterns'))) . '<br>' . t('If this field is left blank the default node pattern of') . ' <strong>' . variable_get('pathauto_node_pattern') . '</strong> ' . t('will be used.'),
  211. '#default_value' => content_type_extras_get_setting('pathauto_node', $type . '_pattern'),
  212. '#weight' => 20,
  213. );
  214. $form['extras']['token_help'] = array(
  215. '#title' => t('Replacement patterns'),
  216. '#type' => 'fieldset',
  217. '#collapsible' => TRUE,
  218. '#collapsed' => TRUE,
  219. 'help' => array(
  220. '#theme' => 'token_tree',
  221. '#token_types' => array('node'),
  222. ),
  223. '#weight' => 30,
  224. );
  225. }
  226. // Custom form submission handler
  227. // See why this is done in the notes for the redirect function in
  228. // content_type_extras.module
  229. $form['#submit'][] = 'content_type_extras_node_type_form_submit_redirect';
  230. }
  231. /**
  232. * Form submission for $form_id node_type_form
  233. */
  234. function content_type_extras_node_type_form_submit(&$form, &$form_state) {
  235. $values = $form_state['values'];
  236. $user_permissions = $values['user_permissions'];
  237. $selected_perms = content_type_extras_get_selected_roles($user_permissions);
  238. foreach ($user_permissions as $action => $group) {
  239. list($type, $trash) = explode('_', $action);
  240. $set_perms = array();
  241. if ($type == 'create') {
  242. $set_perms = array(
  243. 'create ' . $values['type'] . ' content',
  244. );
  245. }
  246. else {
  247. $set_perms = array(
  248. $type . ' own ' . $values['type'] . ' content',
  249. $type . ' any ' . $values['type'] . ' content',
  250. );
  251. }
  252. foreach ($group as $rid => $setting) {
  253. if ($setting) {
  254. user_role_grant_permissions($rid, $set_perms);
  255. }
  256. else {
  257. user_role_revoke_permissions($rid, $set_perms);
  258. }
  259. }
  260. }
  261. if (!empty($values['content_type_extras_remove_body'])) {
  262. // I'm not sure of a better way to not have a body field, other than to
  263. // delete it after it has been created by Core.
  264. $instance = field_read_instance('node', 'body', $values['type']);
  265. field_delete_instance($instance);
  266. }
  267. // Remove variable that is automatically created, it's not needed
  268. variable_del('content_type_extras_remove_body_' . $values['type']);
  269. // We have to rename the pathauto variable. Drupal by default creates one, but
  270. // it's not named properly for pathauto to recognize it.
  271. variable_set('pathauto_node_' . $values['type'] . '_pattern', variable_get('pathauto_node_' . $values['type']));
  272. variable_del('pathauto_node_' . $values['type']);
  273. }
  274. function content_type_extras_get_selected_roles($permissions, $perm_type = NULL) {
  275. $selected_perms = array();
  276. foreach ($permissions as $type => $group) {
  277. foreach ($group as $rid => $value) {
  278. $selected_perms[$type] = array();
  279. if ($value != 0) {
  280. $selected_perms[$type][$rid] = $rid;
  281. }
  282. }
  283. }
  284. if (!empty($perm_type)) {
  285. return $selected_perms[$perm_type];
  286. }
  287. return $selected_perms;
  288. }