content_types.inc

  1. 7.x drupal/modules/node/content_types.inc
  2. 5.x drupal/modules/node/content_types.inc
  3. 6.x drupal/modules/node/content_types.inc

Content type editing user interface.

Functions

Namesort descending Description
node_node_type_insert Implements hook_node_type_insert().
node_node_type_update Implements hook_node_type_update().
node_overview_types Displays the content type admin overview page.
node_type_delete_confirm Menu callback; delete a single content type.
node_type_delete_confirm_submit Process content type delete confirm submissions.
node_type_form Form constructor for the node type editing form.
node_type_form_submit Form submission handler for node_type_form().
node_type_form_validate Form validation handler for node_type_form().
node_type_reset Resets relevant fields of a module-defined node type to their default values.
theme_node_admin_overview Returns HTML for a node type description for the content type admin overview page.
_node_characters Helper function for teaser length choices.

File

drupal/modules/node/content_types.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Content type editing user interface.
  5. */
  6. /**
  7. * Displays the content type admin overview page.
  8. */
  9. function node_overview_types() {
  10. $types = node_type_get_types();
  11. $names = node_type_get_names();
  12. $field_ui = module_exists('field_ui') && user_access('administer fields');
  13. $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => $field_ui ? '4' : '2'));
  14. $rows = array();
  15. foreach ($names as $key => $name) {
  16. $type = $types[$key];
  17. if (node_hook($type->type, 'form')) {
  18. $type_url_str = str_replace('_', '-', $type->type);
  19. $row = array(theme('node_admin_overview', array('name' => $name, 'type' => $type)));
  20. // Set the edit column.
  21. $row[] = array('data' => l(t('edit'), 'admin/structure/types/manage/' . $type_url_str));
  22. if ($field_ui) {
  23. // Manage fields.
  24. $row[] = array('data' => l(t('manage fields'), 'admin/structure/types/manage/' . $type_url_str . '/fields'));
  25. // Display fields.
  26. $row[] = array('data' => l(t('manage display'), 'admin/structure/types/manage/' . $type_url_str . '/display'));
  27. }
  28. // Set the delete column.
  29. if ($type->custom) {
  30. $row[] = array('data' => l(t('delete'), 'admin/structure/types/manage/' . $type_url_str . '/delete'));
  31. }
  32. else {
  33. $row[] = array('data' => '');
  34. }
  35. $rows[] = $row;
  36. }
  37. }
  38. $build['node_table'] = array(
  39. '#theme' => 'table',
  40. '#header' => $header,
  41. '#rows' => $rows,
  42. '#empty' => t('No content types available. <a href="@link">Add content type</a>.', array('@link' => url('admin/structure/types/add'))),
  43. );
  44. return $build;
  45. }
  46. /**
  47. * Returns HTML for a node type description for the content type admin overview page.
  48. *
  49. * @param $variables
  50. * An associative array containing:
  51. * - name: The human-readable name of the content type.
  52. * - type: An object containing the 'type' (machine name) and 'description' of
  53. * the content type.
  54. *
  55. * @ingroup themeable
  56. */
  57. function theme_node_admin_overview($variables) {
  58. $name = $variables['name'];
  59. $type = $variables['type'];
  60. $output = check_plain($name);
  61. $output .= ' <small>' . t('(Machine name: @type)', array('@type' => $type->type)) . '</small>';
  62. $output .= '<div class="description">' . filter_xss_admin($type->description) . '</div>';
  63. return $output;
  64. }
  65. /**
  66. * Form constructor for the node type editing form.
  67. *
  68. * @param $type
  69. * (optional) An object representing the node type, when editing an existing
  70. * node type.
  71. *
  72. * @see node_type_form_validate()
  73. * @see node_type_form_submit()
  74. * @ingroup forms
  75. */
  76. function node_type_form($form, &$form_state, $type = NULL) {
  77. if (!isset($type->type)) {
  78. // This is a new type. Node module managed types are custom and unlocked.
  79. $type = node_type_set_defaults(array('custom' => 1, 'locked' => 0));
  80. }
  81. // Make the type object available to implementations of hook_form_alter.
  82. $form['#node_type'] = $type;
  83. $form['name'] = array(
  84. '#title' => t('Name'),
  85. '#type' => 'textfield',
  86. '#default_value' => $type->name,
  87. '#description' => t('The human-readable name of this content type. This text will be displayed as part of the list on the <em>Add new content</em> page. It is recommended that this name begin with a capital letter and contain only letters, numbers, and spaces. This name must be unique.'),
  88. '#required' => TRUE,
  89. '#size' => 30,
  90. );
  91. $form['type'] = array(
  92. '#type' => 'machine_name',
  93. '#default_value' => $type->type,
  94. '#maxlength' => 32,
  95. '#disabled' => $type->locked,
  96. '#machine_name' => array(
  97. 'exists' => 'node_type_load',
  98. ),
  99. '#description' => t('A unique machine-readable name for this content type. It must only contain lowercase letters, numbers, and underscores. This name will be used for constructing the URL of the %node-add page, in which underscores will be converted into hyphens.', array(
  100. '%node-add' => t('Add new content'),
  101. )),
  102. );
  103. $form['description'] = array(
  104. '#title' => t('Description'),
  105. '#type' => 'textarea',
  106. '#default_value' => $type->description,
  107. '#description' => t('Describe this content type. The text will be displayed on the <em>Add new content</em> page.'),
  108. );
  109. $form['additional_settings'] = array(
  110. '#type' => 'vertical_tabs',
  111. '#attached' => array(
  112. 'js' => array(drupal_get_path('module', 'node') . '/content_types.js'),
  113. ),
  114. );
  115. $form['submission'] = array(
  116. '#type' => 'fieldset',
  117. '#title' => t('Submission form settings'),
  118. '#collapsible' => TRUE,
  119. '#group' => 'additional_settings',
  120. );
  121. $form['submission']['title_label'] = array(
  122. '#title' => t('Title field label'),
  123. '#type' => 'textfield',
  124. '#default_value' => $type->title_label,
  125. '#required' => TRUE,
  126. );
  127. if (!$type->has_title) {
  128. // Avoid overwriting a content type that intentionally does not have a
  129. // title field.
  130. $form['submission']['title_label']['#attributes'] = array('disabled' => 'disabled');
  131. $form['submission']['title_label']['#description'] = t('This content type does not have a title field.');
  132. $form['submission']['title_label']['#required'] = FALSE;
  133. }
  134. $form['submission']['node_preview'] = array(
  135. '#type' => 'radios',
  136. '#title' => t('Preview before submitting'),
  137. '#default_value' => variable_get('node_preview_' . $type->type, DRUPAL_OPTIONAL),
  138. '#options' => array(
  139. DRUPAL_DISABLED => t('Disabled'),
  140. DRUPAL_OPTIONAL => t('Optional'),
  141. DRUPAL_REQUIRED => t('Required'),
  142. ),
  143. );
  144. $form['submission']['help'] = array(
  145. '#type' => 'textarea',
  146. '#title' => t('Explanation or submission guidelines'),
  147. '#default_value' => $type->help,
  148. '#description' => t('This text will be displayed at the top of the page when creating or editing content of this type.'),
  149. );
  150. $form['workflow'] = array(
  151. '#type' => 'fieldset',
  152. '#title' => t('Publishing options'),
  153. '#collapsible' => TRUE,
  154. '#collapsed' => TRUE,
  155. '#group' => 'additional_settings',
  156. );
  157. $form['workflow']['node_options'] = array('#type' => 'checkboxes',
  158. '#title' => t('Default options'),
  159. '#default_value' => variable_get('node_options_' . $type->type, array('status', 'promote')),
  160. '#options' => array(
  161. 'status' => t('Published'),
  162. 'promote' => t('Promoted to front page'),
  163. 'sticky' => t('Sticky at top of lists'),
  164. 'revision' => t('Create new revision'),
  165. ),
  166. '#description' => t('Users with the <em>Administer content</em> permission will be able to override these options.'),
  167. );
  168. $form['display'] = array(
  169. '#type' => 'fieldset',
  170. '#title' => t('Display settings'),
  171. '#collapsible' => TRUE,
  172. '#collapsed' => TRUE,
  173. '#group' => 'additional_settings',
  174. );
  175. $form['display']['node_submitted'] = array(
  176. '#type' => 'checkbox',
  177. '#title' => t('Display author and date information.'),
  178. '#default_value' => variable_get('node_submitted_' . $type->type, TRUE),
  179. '#description' => t('Author username and publish date will be displayed.'),
  180. );
  181. $form['old_type'] = array(
  182. '#type' => 'value',
  183. '#value' => $type->type,
  184. );
  185. $form['orig_type'] = array(
  186. '#type' => 'value',
  187. '#value' => isset($type->orig_type) ? $type->orig_type : '',
  188. );
  189. $form['base'] = array(
  190. '#type' => 'value',
  191. '#value' => $type->base,
  192. );
  193. $form['custom'] = array(
  194. '#type' => 'value',
  195. '#value' => $type->custom,
  196. );
  197. $form['modified'] = array(
  198. '#type' => 'value',
  199. '#value' => $type->modified,
  200. );
  201. $form['locked'] = array(
  202. '#type' => 'value',
  203. '#value' => $type->locked,
  204. );
  205. $form['actions'] = array('#type' => 'actions');
  206. $form['actions']['submit'] = array(
  207. '#type' => 'submit',
  208. '#value' => t('Save content type'),
  209. '#weight' => 40,
  210. );
  211. if ($type->custom) {
  212. if (!empty($type->type)) {
  213. $form['actions']['delete'] = array(
  214. '#type' => 'submit',
  215. '#value' => t('Delete content type'),
  216. '#weight' => 45,
  217. );
  218. }
  219. }
  220. return $form;
  221. }
  222. /**
  223. * Helper function for teaser length choices.
  224. */
  225. function _node_characters($length) {
  226. return ($length == 0) ? t('Unlimited') : format_plural($length, '1 character', '@count characters');
  227. }
  228. /**
  229. * Form validation handler for node_type_form().
  230. *
  231. * @see node_type_form_submit()
  232. */
  233. function node_type_form_validate($form, &$form_state) {
  234. $type = new stdClass();
  235. $type->type = $form_state['values']['type'];
  236. $type->name = trim($form_state['values']['name']);
  237. // Work out what the type was before the user submitted this form
  238. $old_type = $form_state['values']['old_type'];
  239. $types = node_type_get_names();
  240. if (!$form_state['values']['locked']) {
  241. // 'theme' conflicts with theme_node_form().
  242. // '0' is invalid, since elsewhere we check it using empty().
  243. if (in_array($type->type, array('0', 'theme'))) {
  244. form_set_error('type', t("Invalid machine-readable name. Enter a name other than %invalid.", array('%invalid' => $type->type)));
  245. }
  246. }
  247. $names = array_flip($types);
  248. if (isset($names[$type->name]) && $names[$type->name] != $old_type) {
  249. form_set_error('name', t('The human-readable name %name is already taken.', array('%name' => $type->name)));
  250. }
  251. }
  252. /**
  253. * Form submission handler for node_type_form().
  254. *
  255. * @see node_type_form_validate()
  256. */
  257. function node_type_form_submit($form, &$form_state) {
  258. $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  259. $type = node_type_set_defaults();
  260. $type->type = $form_state['values']['type'];
  261. $type->name = trim($form_state['values']['name']);
  262. $type->orig_type = trim($form_state['values']['orig_type']);
  263. $type->old_type = isset($form_state['values']['old_type']) ? $form_state['values']['old_type'] : $type->type;
  264. $type->description = $form_state['values']['description'];
  265. $type->help = $form_state['values']['help'];
  266. $type->title_label = $form_state['values']['title_label'];
  267. // title_label is required in core; has_title will always be true, unless a
  268. // module alters the title field.
  269. $type->has_title = ($type->title_label != '');
  270. $type->base = !empty($form_state['values']['base']) ? $form_state['values']['base'] : 'node_content';
  271. $type->custom = $form_state['values']['custom'];
  272. $type->modified = TRUE;
  273. $type->locked = $form_state['values']['locked'];
  274. if (isset($form['#node_type']->module)) {
  275. $type->module = $form['#node_type']->module;
  276. }
  277. if ($op == t('Delete content type')) {
  278. $form_state['redirect'] = 'admin/structure/types/manage/' . str_replace('_', '-', $type->old_type) . '/delete';
  279. return;
  280. }
  281. $variables = $form_state['values'];
  282. // Remove everything that's been saved already - whatever's left is assumed
  283. // to be a persistent variable.
  284. foreach ($variables as $key => $value) {
  285. if (isset($type->$key)) {
  286. unset($variables[$key]);
  287. }
  288. }
  289. unset($variables['form_token'], $variables['op'], $variables['submit'], $variables['delete'], $variables['reset'], $variables['form_id'], $variables['form_build_id']);
  290. // Save or reset persistent variable values.
  291. foreach ($variables as $key => $value) {
  292. $variable_new = $key . '_' . $type->type;
  293. $variable_old = $key . '_' . $type->old_type;
  294. if (is_array($value)) {
  295. $value = array_keys(array_filter($value));
  296. }
  297. variable_set($variable_new, $value);
  298. if ($variable_new != $variable_old) {
  299. variable_del($variable_old);
  300. }
  301. }
  302. // Saving the content type after saving the variables allows modules to act
  303. // on those variables via hook_node_type_insert().
  304. $status = node_type_save($type);
  305. node_types_rebuild();
  306. menu_rebuild();
  307. $t_args = array('%name' => $type->name);
  308. if ($status == SAVED_UPDATED) {
  309. drupal_set_message(t('The content type %name has been updated.', $t_args));
  310. }
  311. elseif ($status == SAVED_NEW) {
  312. node_add_body_field($type);
  313. drupal_set_message(t('The content type %name has been added.', $t_args));
  314. watchdog('node', 'Added content type %name.', $t_args, WATCHDOG_NOTICE, l(t('view'), 'admin/structure/types'));
  315. }
  316. $form_state['redirect'] = 'admin/structure/types';
  317. return;
  318. }
  319. /**
  320. * Implements hook_node_type_insert().
  321. */
  322. function node_node_type_insert($info) {
  323. if (!empty($info->old_type) && $info->old_type != $info->type) {
  324. $update_count = node_type_update_nodes($info->old_type, $info->type);
  325. if ($update_count) {
  326. drupal_set_message(format_plural($update_count, 'Changed the content type of 1 post from %old-type to %type.', 'Changed the content type of @count posts from %old-type to %type.', array('%old-type' => $info->old_type, '%type' => $info->type)));
  327. }
  328. }
  329. }
  330. /**
  331. * Implements hook_node_type_update().
  332. */
  333. function node_node_type_update($info) {
  334. if (!empty($info->old_type) && $info->old_type != $info->type) {
  335. $update_count = node_type_update_nodes($info->old_type, $info->type);
  336. if ($update_count) {
  337. drupal_set_message(format_plural($update_count, 'Changed the content type of 1 post from %old-type to %type.', 'Changed the content type of @count posts from %old-type to %type.', array('%old-type' => $info->old_type, '%type' => $info->type)));
  338. }
  339. }
  340. }
  341. /**
  342. * Resets relevant fields of a module-defined node type to their default values.
  343. *
  344. * @param $type
  345. * The node type to reset. The node type is passed back by reference with its
  346. * resetted values. If there is no module-defined info for this node type,
  347. * then nothing happens.
  348. */
  349. function node_type_reset($type) {
  350. $info_array = module_invoke_all('node_info');
  351. if (isset($info_array[$type->orig_type])) {
  352. $info_array[$type->orig_type]['type'] = $type->orig_type;
  353. $info = node_type_set_defaults($info_array[$type->orig_type]);
  354. foreach ($info as $field => $value) {
  355. $type->$field = $value;
  356. }
  357. }
  358. }
  359. /**
  360. * Menu callback; delete a single content type.
  361. *
  362. * @ingroup forms
  363. */
  364. function node_type_delete_confirm($form, &$form_state, $type) {
  365. $form['type'] = array('#type' => 'value', '#value' => $type->type);
  366. $form['name'] = array('#type' => 'value', '#value' => $type->name);
  367. $message = t('Are you sure you want to delete the content type %type?', array('%type' => $type->name));
  368. $caption = '';
  369. $num_nodes = db_query("SELECT COUNT(*) FROM {node} WHERE type = :type", array(':type' => $type->type))->fetchField();
  370. if ($num_nodes) {
  371. $caption .= '<p>' . format_plural($num_nodes, '%type is used by 1 piece of content on your site. If you remove this content type, you will not be able to edit the %type content and it may not display correctly.', '%type is used by @count pieces of content on your site. If you remove %type, you will not be able to edit the %type content and it may not display correctly.', array('%type' => $type->name)) . '</p>';
  372. }
  373. $caption .= '<p>' . t('This action cannot be undone.') . '</p>';
  374. return confirm_form($form, $message, 'admin/structure/types', $caption, t('Delete'));
  375. }
  376. /**
  377. * Process content type delete confirm submissions.
  378. *
  379. * @see node_type_delete_confirm()
  380. */
  381. function node_type_delete_confirm_submit($form, &$form_state) {
  382. node_type_delete($form_state['values']['type']);
  383. variable_del('node_preview_' . $form_state['values']['type']);
  384. $t_args = array('%name' => $form_state['values']['name']);
  385. drupal_set_message(t('The content type %name has been deleted.', $t_args));
  386. watchdog('node', 'Deleted content type %name.', $t_args, WATCHDOG_NOTICE);
  387. node_types_rebuild();
  388. menu_rebuild();
  389. $form_state['redirect'] = 'admin/structure/types';
  390. return;
  391. }