5 * Content type editing user interface.
9 * Displays the content type admin overview page.
11 function node_overview_types() {
12 $types = node_type_get_types();
13 $names = node_type_get_names();
14 $field_ui = module_exists('field_ui') && user_access('administer fields');
15 $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => $field_ui ? '4' : '2'));
18 foreach ($names as $key => $name) {
20 if (node_hook($type->type, 'form')) {
21 $type_url_str = str_replace('_', '-', $type->type);
22 $row = array(theme('node_admin_overview', array('name' => $name, 'type' => $type)));
23 // Set the edit column.
24 $row[] = array('data' => l(t('edit'), 'admin/structure/types/manage/' . $type_url_str));
28 $row[] = array('data' => l(t('manage fields'), 'admin/structure/types/manage/' . $type_url_str . '/fields'));
31 $row[] = array('data' => l(t('manage display'), 'admin/structure/types/manage/' . $type_url_str . '/display'));
34 // Set the delete column.
36 $row[] = array('data' => l(t('delete'), 'admin/structure/types/manage/' . $type_url_str . '/delete'));
39 $row[] = array('data' => '');
46 $build['node_table'] = array(
50 '#empty' => t('No content types available. <a href="@link">Add content type</a>.', array('@link' => url('admin/structure/types/add'))),
57 * Returns HTML for a node type description for the content type admin overview page.
60 * An associative array containing:
61 * - name: The human-readable name of the content type.
62 * - type: An object containing the 'type' (machine name) and 'description' of
67 function theme_node_admin_overview($variables) {
68 $name = $variables['name'];
69 $type = $variables['type'];
71 $output = check_plain($name);
72 $output .= ' <small>' . t('(Machine name: @type)', array('@type' => $type->type)) . '</small>';
73 $output .= '<div class="description">' . filter_xss_admin($type->description) . '</div>';
78 * Form constructor for the node type editing form.
81 * (optional) An object representing the node type, when editing an existing
84 * @see node_type_form_validate()
85 * @see node_type_form_submit()
88 function node_type_form($form, &$form_state, $type = NULL) {
89 if (!isset($type->type)) {
90 // This is a new type. Node module managed types are custom and unlocked.
91 $type = node_type_set_defaults(array('custom' => 1, 'locked' => 0));
94 // Make the type object available to implementations of hook_form_alter.
95 $form['#node_type'] = $type;
97 $form['name'] = array(
98 '#title' => t('Name'),
99 '#type' => 'textfield',
100 '#default_value' => $type->name,
101 '#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.'),
106 $form['type'] = array(
107 '#type' => 'machine_name',
108 '#default_value' => $type->type,
110 '#disabled' => $type->locked,
111 '#machine_name' => array(
112 'exists' => 'node_type_load',
114 '#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(
115 '%node-add' => t('Add new content'),
119 $form['description'] = array(
120 '#title' => t('Description'),
121 '#type' => 'textarea',
122 '#default_value' => $type->description,
123 '#description' => t('Describe this content type. The text will be displayed on the <em>Add new content</em> page.'),
126 $form['additional_settings'] = array(
127 '#type' => 'vertical_tabs',
128 '#attached' => array(
129 'js' => array(drupal_get_path('module', 'node') . '/content_types.js'),
133 $form['submission'] = array(
134 '#type' => 'fieldset',
135 '#title' => t('Submission form settings'),
136 '#collapsible' => TRUE,
137 '#group' => 'additional_settings',
139 $form['submission']['title_label'] = array(
140 '#title' => t('Title field label'),
141 '#type' => 'textfield',
142 '#default_value' => $type->title_label,
145 if (!$type->has_title) {
146 // Avoid overwriting a content type that intentionally does not have a
148 $form['submission']['title_label']['#attributes'] = array('disabled' => 'disabled');
149 $form['submission']['title_label']['#description'] = t('This content type does not have a title field.');
150 $form['submission']['title_label']['#required'] = FALSE;
152 $form['submission']['node_preview'] = array(
154 '#title' => t('Preview before submitting'),
155 '#default_value' => variable_get('node_preview_' . $type->type, DRUPAL_OPTIONAL),
157 DRUPAL_DISABLED => t('Disabled'),
158 DRUPAL_OPTIONAL => t('Optional'),
159 DRUPAL_REQUIRED => t('Required'),
162 $form['submission']['help'] = array(
163 '#type' => 'textarea',
164 '#title' => t('Explanation or submission guidelines'),
165 '#default_value' => $type->help,
166 '#description' => t('This text will be displayed at the top of the page when creating or editing content of this type.'),
168 $form['workflow'] = array(
169 '#type' => 'fieldset',
170 '#title' => t('Publishing options'),
171 '#collapsible' => TRUE,
172 '#collapsed' => TRUE,
173 '#group' => 'additional_settings',
175 $form['workflow']['node_options'] = array('#type' => 'checkboxes',
176 '#title' => t('Default options'),
177 '#default_value' => variable_get('node_options_' . $type->type, array('status', 'promote')),
179 'status' => t('Published'),
180 'promote' => t('Promoted to front page'),
181 'sticky' => t('Sticky at top of lists'),
182 'revision' => t('Create new revision'),
184 '#description' => t('Users with the <em>Administer content</em> permission will be able to override these options.'),
186 $form['display'] = array(
187 '#type' => 'fieldset',
188 '#title' => t('Display settings'),
189 '#collapsible' => TRUE,
190 '#collapsed' => TRUE,
191 '#group' => 'additional_settings',
193 $form['display']['node_submitted'] = array(
194 '#type' => 'checkbox',
195 '#title' => t('Display author and date information.'),
196 '#default_value' => variable_get('node_submitted_' . $type->type, TRUE),
197 '#description' => t('Author username and publish date will be displayed.'),
199 $form['old_type'] = array(
201 '#value' => $type->type,
203 $form['orig_type'] = array(
205 '#value' => isset($type->orig_type) ? $type->orig_type : '',
207 $form['base'] = array(
209 '#value' => $type->base,
211 $form['custom'] = array(
213 '#value' => $type->custom,
215 $form['modified'] = array(
217 '#value' => $type->modified,
219 $form['locked'] = array(
221 '#value' => $type->locked,
224 $form['actions'] = array('#type' => 'actions');
225 $form['actions']['submit'] = array(
227 '#value' => t('Save content type'),
232 if (!empty($type->type)) {
233 $form['actions']['delete'] = array(
235 '#value' => t('Delete content type'),
245 * Helper function for teaser length choices.
247 function _node_characters($length) {
248 return ($length == 0) ? t('Unlimited') : format_plural($length, '1 character', '@count characters');
252 * Form validation handler for node_type_form().
254 * @see node_type_form_submit()
256 function node_type_form_validate($form, &$form_state) {
257 $type = new stdClass();
258 $type->type = $form_state['values']['type'];
259 $type->name = trim($form_state['values']['name']);
261 // Work out what the type was before the user submitted this form
262 $old_type = $form_state['values']['old_type'];
264 $types = node_type_get_names();
266 if (!$form_state['values']['locked']) {
267 // 'theme' conflicts with theme_node_form().
268 // '0' is invalid, since elsewhere we check it using empty().
269 if (in_array($type->type, array('0', 'theme'))) {
270 form_set_error('type', t("Invalid machine-readable name. Enter a name other than %invalid.", array('%invalid' => $type->type)));
274 $names = array_flip($types);
276 if (isset($names[$type->name]) && $names[$type->name] != $old_type) {
277 form_set_error('name', t('The human-readable name %name is already taken.', array('%name' => $type->name)));
282 * Form submission handler for node_type_form().
284 * @see node_type_form_validate()
286 function node_type_form_submit($form, &$form_state) {
287 $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
289 $type = node_type_set_defaults();
291 $type->type = $form_state['values']['type'];
292 $type->name = trim($form_state['values']['name']);
293 $type->orig_type = trim($form_state['values']['orig_type']);
294 $type->old_type = isset($form_state['values']['old_type']) ? $form_state['values']['old_type'] : $type->type;
296 $type->description = $form_state['values']['description'];
297 $type->help = $form_state['values']['help'];
298 $type->title_label = $form_state['values']['title_label'];
299 // title_label is required in core; has_title will always be true, unless a
300 // module alters the title field.
301 $type->has_title = ($type->title_label != '');
303 $type->base = !empty($form_state['values']['base']) ? $form_state['values']['base'] : 'node_content';
304 $type->custom = $form_state['values']['custom'];
305 $type->modified = TRUE;
306 $type->locked = $form_state['values']['locked'];
307 if (isset($form['#node_type']->module)) {
308 $type->module = $form['#node_type']->module;
311 if ($op == t('Delete content type')) {
312 $form_state['redirect'] = 'admin/structure/types/manage/' . str_replace('_', '-', $type->old_type) . '/delete';
316 $variables = $form_state['values'];
318 // Remove everything that's been saved already - whatever's left is assumed
319 // to be a persistent variable.
320 foreach ($variables as $key => $value) {
321 if (isset($type->$key)) {
322 unset($variables[$key]);
326 unset($variables['form_token'], $variables['op'], $variables['submit'], $variables['delete'], $variables['reset'], $variables['form_id'], $variables['form_build_id']);
328 // Save or reset persistent variable values.
329 foreach ($variables as $key => $value) {
330 $variable_new = $key . '_' . $type->type;
331 $variable_old = $key . '_' . $type->old_type;
333 if (is_array($value)) {
334 $value = array_keys(array_filter($value));
336 variable_set($variable_new, $value);
338 if ($variable_new != $variable_old) {
339 variable_del($variable_old);
343 // Saving the content type after saving the variables allows modules to act
344 // on those variables via hook_node_type_insert().
345 $status = node_type_save($type);
347 node_types_rebuild();
349 $t_args = array('%name' => $type->name);
351 if ($status == SAVED_UPDATED) {
352 drupal_set_message(t('The content type %name has been updated.', $t_args));
354 elseif ($status == SAVED_NEW) {
355 node_add_body_field($type);
356 drupal_set_message(t('The content type %name has been added.', $t_args));
357 watchdog('node', 'Added content type %name.', $t_args, WATCHDOG_NOTICE, l(t('view'), 'admin/structure/types'));
360 $form_state['redirect'] = 'admin/structure/types';
365 * Implements hook_node_type_insert().
367 function node_node_type_insert($info) {
368 if (!empty($info->old_type) && $info->old_type != $info->type) {
369 $update_count = node_type_update_nodes($info->old_type, $info->type);
372 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)));
378 * Implements hook_node_type_update().
380 function node_node_type_update($info) {
381 if (!empty($info->old_type) && $info->old_type != $info->type) {
382 $update_count = node_type_update_nodes($info->old_type, $info->type);
385 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)));
391 * Resets relevant fields of a module-defined node type to their default values.
394 * The node type to reset. The node type is passed back by reference with its
395 * resetted values. If there is no module-defined info for this node type,
396 * then nothing happens.
398 function node_type_reset($type) {
399 $info_array = module_invoke_all('node_info');
400 if (isset($info_array[$type->orig_type])) {
401 $info_array[$type->orig_type]['type'] = $type->orig_type;
402 $info = node_type_set_defaults($info_array[$type->orig_type]);
404 foreach ($info as $field => $value) {
405 $type->$field = $value;
411 * Menu callback; delete a single content type.
415 function node_type_delete_confirm($form, &$form_state, $type) {
416 $form['type'] = array('#type' => 'value', '#value' => $type->type);
417 $form['name'] = array('#type' => 'value', '#value' => $type->name);
419 $message = t('Are you sure you want to delete the content type %type?', array('%type' => $type->name));
422 $num_nodes = db_query("SELECT COUNT(*) FROM {node} WHERE type = :type", array(':type' => $type->type))->fetchField();
424 $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>';
427 $caption .= '<p>' . t('This action cannot be undone.') . '</p>';
429 return confirm_form($form, $message, 'admin/structure/types', $caption, t('Delete'));
433 * Process content type delete confirm submissions.
435 * @see node_type_delete_confirm()
437 function node_type_delete_confirm_submit($form, &$form_state) {
438 node_type_delete($form_state['values']['type']);
440 variable_del('node_preview_' . $form_state['values']['type']);
441 $t_args = array('%name' => $form_state['values']['name']);
442 drupal_set_message(t('The content type %name has been deleted.', $t_args));
443 watchdog('node', 'Deleted content type %name.', $t_args, WATCHDOG_NOTICE);
445 node_types_rebuild();
448 $form_state['redirect'] = 'admin/structure/types';