content_type_extras.node_type_form.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. // Get all available user roles
  117. $roles = user_roles();
  118. $admin_role = variable_get('user_admin_role', 0);
  119. if ($admin_role != 0) {
  120. $roles[$admin_role] .= t(' <em>(administrator role)</em>');
  121. }
  122. $create_roles = array();
  123. $edit_roles = array();
  124. $delete_roles = array();
  125. // If we are on an existing content type form
  126. if (!empty($form['name']['#default_value'])) {
  127. $create_roles = user_roles(FALSE, 'create ' . $form['#node_type']->type . ' content');
  128. $edit_roles = user_roles(FALSE, 'edit any ' . $form['#node_type']->type . ' content');
  129. $delete_roles = user_roles(FALSE, 'delete any ' . $form['#node_type']->type . ' content');
  130. }
  131. // We are creating a new content type
  132. else {
  133. $user_permissions = content_type_extras_get_setting('user_permissions', '');
  134. $selected_perms = content_type_extras_get_selected_roles($user_permissions);
  135. $create_roles = $selected_perms['create_roles'];
  136. $edit_roles = $selected_perms['edit_roles'];
  137. $delete_roles = $selected_perms['delete_roles'];
  138. }
  139. $permission_select = content_type_extras_get_default('content_type_extras_user_permissions_select');
  140. if ($permission_select == 'cte') {
  141. // We need to remove FPA's implementation
  142. unset($form['fpa_fieldset']);
  143. $form['user_permissions'] = array(
  144. '#type' => 'fieldset',
  145. '#title' => t('User permissions'),
  146. '#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.'),
  147. '#group' => 'additional_settings',
  148. '#tree' => TRUE,
  149. 'create_roles' => array(
  150. '#type' => 'checkboxes',
  151. '#title' => t('Roles that can CREATE content of this type'),
  152. '#options' => $roles,
  153. '#default_value' => array_keys($create_roles),
  154. ),
  155. 'edit_roles' => array(
  156. '#type' => 'checkboxes',
  157. '#title' => t('Roles that can EDIT any content of this type'),
  158. '#options' => $roles,
  159. '#default_value' => array_keys($edit_roles),
  160. ),
  161. 'delete_roles' => array(
  162. '#type' => 'checkboxes',
  163. '#title' => t('Roles that can DELETE any content of this type'),
  164. '#options' => $roles,
  165. '#default_value' => array_keys($delete_roles),
  166. ),
  167. );
  168. }
  169. $form['extras'] = array(
  170. '#type' => 'fieldset',
  171. '#title' => t('Extra settings'),
  172. '#group' => 'additional_settings',
  173. 'content_type_extras_title_hide' => array(
  174. '#type' => 'checkbox',
  175. '#title' => t('Hide node titles from displaying'),
  176. '#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.'),
  177. '#default_value' => content_type_extras_get_setting('content_type_extras_title_hide', $type),
  178. '#weight' => 0,
  179. ),
  180. );
  181. if (!empty($form['#node_type']->is_new)) {
  182. // I decided to make this option only available when creating content types because:
  183. // 1. Once the content type is created, the admin can manage the body field just like
  184. // any other field under "Manage fields"
  185. // 2. We would have to provide some kind of data checking on existing content types
  186. // to see if there was already data entered in the body field for that content type
  187. // and, if so, how the admin wanted to handle that...that just doesn't make sense!
  188. $form['extras']['content_type_extras_remove_body'] = array(
  189. '#type' => 'checkbox',
  190. '#title' => t('Remove body field from this content type'),
  191. '#default_value' => content_type_extras_get_setting('content_type_extras_remove_body', $type),
  192. '#weight' => 10,
  193. );
  194. }
  195. if (module_exists('pathauto')) {
  196. $form['extras']['pathauto_node'] = array(
  197. '#type' => 'textfield',
  198. '#title' => t('Path alias'),
  199. '#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.'),
  200. '#default_value' => content_type_extras_get_setting('pathauto_node', $type . '_pattern'),
  201. '#weight' => 20,
  202. );
  203. $form['extras']['token_help'] = array(
  204. '#title' => t('Replacement patterns'),
  205. '#type' => 'fieldset',
  206. '#collapsible' => TRUE,
  207. '#collapsed' => TRUE,
  208. 'help' => array(
  209. '#theme' => 'token_tree',
  210. '#token_types' => array('node'),
  211. ),
  212. '#weight' => 30,
  213. );
  214. }
  215. // Custom form submission handler
  216. // See why this is done in the notes for the redirect function in
  217. // content_type_extras.module
  218. $form['#submit'][] = 'content_type_extras_node_type_form_submit_redirect';
  219. }
  220. /**
  221. * Form submission for $form_id node_type_form
  222. */
  223. function content_type_extras_node_type_form_submit(&$form, &$form_state) {
  224. $values = $form_state['values'];
  225. $user_permissions = $values['user_permissions'];
  226. $selected_perms = content_type_extras_get_selected_roles($user_permissions);
  227. foreach ($user_permissions as $action => $group) {
  228. list($type, $trash) = explode('_', $action);
  229. $set_perms = array();
  230. if ($type == 'create') {
  231. $set_perms = array(
  232. 'create ' . $values['type'] . ' content',
  233. );
  234. }
  235. else {
  236. $set_perms = array(
  237. $type . ' own ' . $values['type'] . ' content',
  238. $type . ' any ' . $values['type'] . ' content',
  239. );
  240. }
  241. foreach ($group as $rid => $setting) {
  242. if ($setting) {
  243. user_role_grant_permissions($rid, $set_perms);
  244. }
  245. else {
  246. user_role_revoke_permissions($rid, $set_perms);
  247. }
  248. }
  249. }
  250. if (!empty($values['content_type_extras_remove_body'])) {
  251. // I'm not sure of a better way to not have a body field, other than to
  252. // delete it after it has been created by Core.
  253. $instance = field_read_instance('node', 'body', $values['type']);
  254. field_delete_instance($instance);
  255. }
  256. // Remove variable that is automatically created, it's not needed
  257. variable_del('content_type_extras_remove_body_' . $values['type']);
  258. // We have to rename the pathauto variable. Drupal by default creates one, but
  259. // it's not named properly for pathauto to recognize it.
  260. variable_set('pathauto_node_' . $values['type'] . '_pattern', variable_get('pathauto_node_' . $values['type']));
  261. variable_del('pathauto_node_' . $values['type']);
  262. }
  263. function content_type_extras_get_selected_roles($permissions, $perm_type = NULL) {
  264. $selected_perms = array();
  265. foreach ($permissions as $type => $group) {
  266. foreach ($group as $rid => $value) {
  267. $selected_perms[$type] = array();
  268. if ($value != 0) {
  269. $selected_perms[$type][$rid] = $rid;
  270. }
  271. }
  272. }
  273. if (!empty($perm_type)) {
  274. return $selected_perms[$perm_type];
  275. }
  276. return $selected_perms;
  277. }