content_type_extras.node_type_form.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. $form['submission']['content_type_extras_save_button'] = array(
  28. '#type' => 'textfield',
  29. '#title' => "'Save' button value",
  30. '#description' => t(''),
  31. '#default_value' => content_type_extras_get_setting('content_type_extras_save_button', $type),
  32. '#weight' => $form['submission']['node_preview']['#weight'] - 1,
  33. );
  34. // Add the option to have a "Save and New" button added to the content type
  35. $form['submission']['content_type_extras_save_and_new'] = array(
  36. '#type' => 'radios',
  37. '#title' => "'Save and New' button",
  38. '#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.'),
  39. '#options' => array(
  40. t('Disabled'),
  41. t('Enabled'),
  42. ),
  43. '#default_value' => content_type_extras_get_setting('content_type_extras_save_and_new', $type),
  44. '#weight' => $form['submission']['node_preview']['#weight'] + 2,
  45. );
  46. $form['submission']['content_type_extras_save_and_new_button'] = array(
  47. '#type' => 'textfield',
  48. '#title' => "'Save and New' button value",
  49. '#description' => t(''),
  50. '#default_value' => content_type_extras_get_setting('content_type_extras_save_and_new_button', $type),
  51. '#weight' => $form['submission']['content_type_extras_save_and_new']['#weight'] + 1,
  52. '#states' => array(
  53. 'visible' => array(
  54. 'input[name=content_type_extras_save_and_new]' => array('value' => '1'),
  55. ),
  56. ),
  57. );
  58. // Add the option to have a "Save and Edit" button added to the content type
  59. $form['submission']['content_type_extras_save_and_edit'] = array(
  60. '#type' => 'radios',
  61. '#title' => "'Save and Edit' button",
  62. '#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.'),
  63. '#options' => array(
  64. t('Disabled'),
  65. t('Enabled'),
  66. ),
  67. '#default_value' => content_type_extras_get_setting('content_type_extras_save_and_edit', $type),
  68. '#weight' => $form['submission']['node_preview']['#weight'] + 4,
  69. );
  70. $form['submission']['content_type_extras_save_and_edit_button'] = array(
  71. '#type' => 'textfield',
  72. '#title' => "'Save and Edit' button value",
  73. '#description' => t(''),
  74. '#default_value' => content_type_extras_get_setting('content_type_extras_save_and_edit_button', $type),
  75. '#weight' => $form['submission']['content_type_extras_save_and_edit']['#weight'] + 1,
  76. '#states' => array(
  77. 'visible' => array(
  78. 'input[name=content_type_extras_save_and_edit]' => array('value' => '1'),
  79. ),
  80. ),
  81. );
  82. // Add the option to have a "Cancel" button added to the content type
  83. $form['submission']['content_type_extras_cancel'] = array(
  84. '#type' => 'radios',
  85. '#title' => "Cancel button",
  86. '#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.'),
  87. '#options' => array(
  88. t('Disabled'),
  89. t('Enabled'),
  90. ),
  91. '#default_value' => content_type_extras_get_setting('content_type_extras_cancel', $type),
  92. '#weight' => $form['submission']['node_preview']['#weight'] + 6,
  93. );
  94. // Add the option to have the warning message appear when using the "Cancel" button
  95. $form['submission']['content_type_extras_cancel_hide_warning'] = array(
  96. '#type' => 'checkbox',
  97. '#title' => 'Hide cancel button warning message',
  98. '#description' => t('If checked, a javascript alert box will <strong>not</strong> display warning the user that their changes will not be saved.'),
  99. '#default_value' => content_type_extras_get_setting('content_type_extras_cancel_hide_warning', $type),
  100. '#weight' => $form['submission']['content_type_extras_cancel']['#weight'] + 1,
  101. '#states' => array(
  102. 'visible' => array(
  103. 'input[name=content_type_extras_cancel]' => array('value' => '1'),
  104. ),
  105. ),
  106. );
  107. // Set weight of help text area
  108. $form['submission']['help']['#weight'] = $form['submission']['node_preview']['#weight'] + 7;
  109. // Set 'Title field label'
  110. $form['submission']['title_label']['#default_value'] = content_type_extras_get_setting('title_label', $type);
  111. // Set 'Preview' button default
  112. $form['submission']['node_preview']['#default_value'] = content_type_extras_get_setting('node_preview', $type);
  113. // Set 'Display author and date information.'
  114. $form['display']['node_submitted']['#default_value'] = content_type_extras_get_setting('node_submitted', $type);
  115. // Set 'Publishing options'
  116. $form['workflow']['node_options']['#default_value'] = content_type_extras_get_setting('node_options', $type);
  117. if (module_exists('comment')) {
  118. // A new content type is being added
  119. if (empty($form['#node_type']->name)) {
  120. $comment_settings = content_type_extras_get_setting('comment', $type);
  121. $form['comment']['comment']['#default_value'] = $comment_settings['comment'];
  122. $form['comment']['comment_default_mode']['#default_value'] = $comment_settings['default_mode'];
  123. $form['comment']['comment_default_per_page']['#default_value'] = $comment_settings['default_per_page'];
  124. $form['comment']['comment_anonymous']['#default_value'] = $comment_settings['anonymous'];
  125. $form['comment']['comment_subject_field']['#default_value'] = $comment_settings['subject_field'];
  126. $form['comment']['comment_form_location']['#default_value'] = $comment_settings['form_location'];
  127. $form['comment']['comment_preview']['#default_value'] = $comment_settings['preview'];
  128. }
  129. }
  130. // A new content type is being added
  131. if (empty($form['#node_type']->name)) {
  132. if (module_exists('xmlsitemap')) {
  133. // XML Sitemap stores its variables a little differently, so we have to adjust for it here.
  134. $xmlsitemap_settings = content_type_extras_get_setting('xmlsitemap_settings', 'node_' . $type);
  135. $form['xmlsitemap']['status']['#default_value'] = $xmlsitemap_settings['status'];
  136. $form['xmlsitemap']['priority']['#default_value'] = $xmlsitemap_settings['priority'];
  137. }
  138. if (module_exists('scheduler')) {
  139. // Scheduler stores its variables a little differently, so we have to adjust for it here.
  140. $scheduler_settings = content_type_extras_get_setting('scheduler_settings', 'node_' . $type);
  141. $form['scheduler']['publish']['scheduler_publish_enable']['#default_value'] = $scheduler_settings['publish_enable'];
  142. $form['scheduler']['publish']['scheduler_publish_touch']['#default_value'] = $scheduler_settings['publish_touch'];
  143. $form['scheduler']['publish']['scheduler_publish_require']['#default_value'] = $scheduler_settings['publish_require'];
  144. $form['scheduler']['publish']['scheduler_publish_revision']['#default_value'] = $scheduler_settings['publish_revision'];
  145. $form['scheduler']['unpublish']['scheduler_unpublish_enable']['#default_value'] = $scheduler_settings['unpublish_enable'];
  146. $form['scheduler']['unpublish']['scheduler_unpublish_require']['#default_value'] = $scheduler_settings['unpublish_require'];
  147. $form['scheduler']['unpublish']['scheduler_unpublish_revision']['#default_value'] = $scheduler_settings['unpublish_revision'];
  148. }
  149. }
  150. // Get all available user roles
  151. $roles = user_roles();
  152. $admin_role = variable_get('user_admin_role', 0);
  153. if ($admin_role != 0) {
  154. $roles[$admin_role] .= t(' <em>(administrator role)</em>');
  155. }
  156. $create_roles = array();
  157. $edit_roles = array();
  158. $delete_roles = array();
  159. $edit_own_roles = array();
  160. $delete_own_roles = array();
  161. // If we are on an existing content type form
  162. if (!empty($form['name']['#default_value'])) {
  163. $create_roles = user_roles(FALSE, 'create ' . $form['#node_type']->type . ' content');
  164. $edit_roles = user_roles(FALSE, 'edit any ' . $form['#node_type']->type . ' content');
  165. $delete_roles = user_roles(FALSE, 'delete any ' . $form['#node_type']->type . ' content');
  166. $delete_own_roles = user_roles(FALSE, 'delete own ' . $form['#node_type']->type . ' content');
  167. $edit_own_roles = user_roles(FALSE, 'edit own ' . $form['#node_type']->type . ' content');
  168. }
  169. // We are creating a new content type
  170. else {
  171. $user_permissions = content_type_extras_get_setting('user_permissions', '');
  172. $selected_perms = content_type_extras_get_selected_roles($user_permissions);
  173. $create_roles = $selected_perms['create_roles'];
  174. $edit_roles = $selected_perms['edit_roles'];
  175. $edit_own_roles = $selected_perms['edit_own_roles'];
  176. $delete_roles = $selected_perms['delete_roles'];
  177. $delete_own_roles = $selected_perms['delete_own_roles'];
  178. }
  179. $permission_select = content_type_extras_get_default('content_type_extras_user_permissions_select');
  180. if ($permission_select == 'cte') {
  181. // We need to remove FPA's implementation
  182. unset($form['fpa_fieldset']);
  183. $form['user_permissions'] = array(
  184. '#type' => 'fieldset',
  185. '#title' => t('User permissions'),
  186. '#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.'),
  187. '#group' => 'additional_settings',
  188. '#tree' => TRUE,
  189. 'create_roles' => array(
  190. '#type' => 'checkboxes',
  191. '#title' => t('Roles that can CREATE content of this type'),
  192. '#options' => $roles,
  193. '#default_value' => array_keys($create_roles),
  194. ),
  195. 'edit_roles' => array(
  196. '#type' => 'checkboxes',
  197. '#title' => t('Roles that can EDIT any content of this type'),
  198. '#options' => $roles,
  199. '#default_value' => array_keys($edit_roles),
  200. ),
  201. 'edit_own_roles' => array(
  202. '#type' => 'checkboxes',
  203. '#title' => t('Roles that can EDIT own content of this type'),
  204. '#options' => $roles,
  205. '#default_value' => array_keys($edit_own_roles),
  206. ),
  207. 'delete_roles' => array(
  208. '#type' => 'checkboxes',
  209. '#title' => t('Roles that can DELETE any content of this type'),
  210. '#options' => $roles,
  211. '#default_value' => array_keys($delete_roles),
  212. ),
  213. 'delete_own_roles' => array(
  214. '#type' => 'checkboxes',
  215. '#title' => t('Roles that can DELETE own content of this type'),
  216. '#options' => $roles,
  217. '#default_value' => array_keys($delete_own_roles),
  218. ),
  219. );
  220. }
  221. $form['extras'] = array(
  222. '#type' => 'fieldset',
  223. '#title' => t('Extra settings'),
  224. '#group' => 'additional_settings',
  225. 'content_type_extras_title_hide' => array(
  226. '#type' => 'checkbox',
  227. '#title' => t('Hide node titles from displaying'),
  228. '#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.'),
  229. '#default_value' => content_type_extras_get_setting('content_type_extras_title_hide', $type),
  230. '#weight' => 0,
  231. ),
  232. 'content_type_extras_top_buttons' => array(
  233. '#type' => 'checkboxes',
  234. '#title' => t('Show form buttons at top of'),
  235. '#description' => t('Select the areas to duplicate form submission buttons on the top of the page.'),
  236. '#options' => array(
  237. 'manage_fields' => t('Manage fields form'),
  238. 'node_edit' => t('Node edit form'),
  239. ),
  240. '#default_value' => content_type_extras_get_setting('content_type_extras_top_buttons', $type),
  241. ),
  242. );
  243. if (!empty($form['#node_type']->is_new)) {
  244. // I decided to make this option only available when creating content types because:
  245. // 1. Once the content type is created, the admin can manage the body field just like
  246. // any other field under "Manage fields"
  247. // 2. We would have to provide some kind of data checking on existing content types
  248. // to see if there was already data entered in the body field for that content type
  249. // and, if so, how the admin wanted to handle that...that just doesn't make sense!
  250. $form['extras']['content_type_extras_remove_body'] = array(
  251. '#type' => 'checkbox',
  252. '#title' => t('Remove body field from this content type'),
  253. '#default_value' => content_type_extras_get_setting('content_type_extras_remove_body', $type),
  254. '#weight' => 10,
  255. );
  256. }
  257. if (module_exists('pathauto')) {
  258. $form['extras']['pathauto_node'] = array(
  259. '#type' => 'textfield',
  260. '#title' => t('Path alias'),
  261. '#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.'),
  262. '#default_value' => content_type_extras_get_setting('pathauto_node', $type . '_pattern'),
  263. '#weight' => 20,
  264. );
  265. if (!content_type_extras_get_default('content_type_extras_disable_token_display')) {
  266. $form['extras']['token_help'] = array(
  267. '#title' => t('Replacement patterns'),
  268. '#type' => 'fieldset',
  269. '#collapsible' => TRUE,
  270. '#collapsed' => TRUE,
  271. 'help' => array(
  272. '#theme' => 'token_tree',
  273. '#token_types' => array('node'),
  274. ),
  275. '#weight' => 30,
  276. );
  277. }
  278. }
  279. // Custom form submission handler
  280. // See why this is done in the notes for the redirect function in
  281. // content_type_extras.module
  282. $form['#submit'][] = 'content_type_extras_node_type_form_submit_redirect';
  283. }
  284. /**
  285. * Form submission for $form_id node_type_form
  286. */
  287. function content_type_extras_node_type_form_submit(&$form, &$form_state) {
  288. $values = $form_state['values'];
  289. $user_permissions = $values['user_permissions'];
  290. $selected_perms = content_type_extras_get_selected_roles($user_permissions);
  291. foreach ($user_permissions as $action => $group) {
  292. list($type, $trash) = explode('_', $action);
  293. $set_perms = array();
  294. if ($type == 'create') {
  295. $set_perms = array(
  296. 'create ' . $values['type'] . ' content',
  297. );
  298. }
  299. elseif ($type == 'edit') {
  300. if($trash == 'own') {
  301. $set_perms = array(
  302. $type . ' own ' . $values['type'] . ' content',
  303. );
  304. } else {
  305. $set_perms = array(
  306. $type . ' any ' . $values['type'] . ' content',
  307. );
  308. }
  309. }
  310. elseif ($type == 'delete') {
  311. if($trash == 'own') {
  312. $set_perms = array(
  313. $type . ' own ' . $values['type'] . ' content',
  314. );
  315. } else {
  316. $set_perms = array(
  317. $type . ' any ' . $values['type'] . ' content',
  318. );
  319. }
  320. }
  321. foreach ($group as $rid => $setting) {
  322. if ($setting) {
  323. user_role_grant_permissions($rid, $set_perms);
  324. }
  325. else {
  326. user_role_revoke_permissions($rid, $set_perms);
  327. }
  328. }
  329. }
  330. if (!empty($values['content_type_extras_remove_body'])) {
  331. // I'm not sure of a better way to not have a body field, other than to
  332. // delete it after it has been created by Core.
  333. $instance = field_read_instance('node', 'body', $values['type']);
  334. field_delete_instance($instance);
  335. }
  336. // Remove variable that is automatically created, it's not needed
  337. variable_del('content_type_extras_remove_body_' . $values['type']);
  338. // We have to rename the pathauto variable. Drupal by default creates one, but
  339. // it's not named properly for pathauto to recognize it.
  340. variable_set('pathauto_node_' . $values['type'] . '_pattern', variable_get('pathauto_node_' . $values['type']));
  341. variable_del('pathauto_node_' . $values['type']);
  342. }
  343. function content_type_extras_get_selected_roles($permissions, $perm_type = NULL) {
  344. $selected_perms = array();
  345. foreach ($permissions as $type => $group) {
  346. foreach ($group as $rid => $value) {
  347. $selected_perms[$type] = array();
  348. if ($value != 0) {
  349. $selected_perms[$type][$rid] = $rid;
  350. }
  351. }
  352. }
  353. if (!empty($perm_type)) {
  354. return $selected_perms[$perm_type];
  355. }
  356. return $selected_perms;
  357. }