content_types.inc 14.3 KB
Newer Older
drumm's avatar
drumm committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
// $Id$

/**
 * @file
 * Content type editing UI.
 */

/**
 * Displays the content type admin overview page.
 */
function node_overview_types() {
  $types = node_get_types();
  $names = node_get_types('names');
  $header = array(t('Name'), t('Type'), t('Description'), array('data' => t('Operations'), 'colspan' => '2'));
  $rows = array();

  foreach ($names as $key => $name) {
    $type = $types[$key];
20
    if (function_exists($type->module .'_form')) {
drumm's avatar
drumm committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
      $name = check_plain($name);
      $type_url_str = str_replace('_', '-', $type->type);
      // Populate the operations field.
      $operations = array();

      // Set the edit column.
      $operations[] = array('data' => l(t('edit'), 'admin/content/types/'. $type_url_str));

      // Set the delete column.
      if ($type->custom) {
        $operations[] = array('data' => l(t('delete'), 'admin/content/types/'. $type_url_str .'/delete'));
      }
      else {
        $operations[] = array('data' => '');
      }

      $row = array(array('data' => l($name, 'admin/content/types/'. $type_url_str), 'class' => $class), array('data' => check_plain($type->type), 'class' => $class), array('data' => check_plain($type->description), 'class' => $class));
      foreach ($operations as $operation) {
        $operation['class'] = $class;
        $row[] = $operation;
      }
      $rows[] = $row;
    }
  }

  if (empty($rows)) {
    $rows[] = array(array('data' => t('No content types available.'), 'colspan' => '5', 'class' => 'message'));
  }

  return theme('table', $header, $rows);
}

/**
 * Generates the node type editing form.
 */
function node_type_form($type = NULL) {
  if (!isset($type->type)) {
    $type = new stdClass();
    $type->type = $type->name = $type->module = $type->description = $type->help = '';
    $type->min_word_count = 0;
    $type->has_title = TRUE;
    $type->has_body = TRUE;
    $type->title_label = t('Title');
    $type->body_label = t('Body');
    $type->custom = TRUE;
    $type->modified = FALSE;
    $type->locked = FALSE;
  }

70
  $form['#node_type'] = $type; // Make the type object available to implementations of hook_form_alter.
71

drumm's avatar
drumm committed
72 73 74 75 76 77 78 79
  $form['identity'] = array(
    '#type' => 'fieldset',
    '#title' => t('Identification'),
  );
  $form['identity']['name'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#default_value' => $type->name,
80
    '#description' => t('The human-readable name of this content type. This text will be displayed as part of the list on the <em>create content</em> page. It is recommended that this name begins with a capital letter and consists only of letters, numbers, and <strong>spaces</strong>. This name must be unique to this content type.'),
drumm's avatar
drumm committed
81 82 83 84 85 86 87 88 89 90
    '#required' => TRUE,
  );

  if (!$type->locked) {
    $form['identity']['type'] = array(
      '#title' => t('Type'),
      '#type' => 'textfield',
      '#default_value' => $type->type,
      '#maxlength' => 32,
      '#required' => TRUE,
91
      '#description' => t('The machine-readable name of this content type. This text will be used for constructing the URL of the <em>create content</em> page for this content type. This name may consist of only of lowercase letters, numbers, and underscores. Dashes are not allowed. Underscores will be converted into dashes when constructing the URL of the <em>create content</em> page. This name must be unique to this content type.'),
drumm's avatar
drumm committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    );
  }
  else {
    $form['identity']['type'] = array(
      '#type' => 'value',
      '#value' => $type->type,
    );
    $form['identity']['type_display'] = array(
      '#title' => t('Type'),
      '#type' => 'item',
      '#value' => theme('placeholder', $type->type),
      '#description' => t('The machine-readable name of this content type. This field cannot be modified for system-defined content types.'),
    );
  }

107 108 109 110 111 112 113
  $form['identity']['description'] = array(
    '#title' => t('Description'),
    '#type' => 'textarea',
    '#default_value' => $type->description,
    '#description' => t('A brief description of this content type. This text will be displayed as part of the list on the <em>create content</em> page.'),
    );

drumm's avatar
drumm committed
114 115 116 117 118 119
  $form['submission'] = array(
    '#type' => 'fieldset',
    '#title' =>t('Submission form'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
120 121 122 123 124 125 126 127 128 129 130 131
  $form['submission']['title_label'] = array(
    '#title' => t('Title field label'),
    '#type' => 'textfield',
    '#default_value' => $type->title_label,
    '#required' => TRUE,
  );
  if (!$type->has_title) {
    // Avoid overwriting a content type that intentionally does not have a
    // title field.
    $form['submission']['title_label']['#attributes'] = array('disabled' => 'disabled');
    $form['submission']['title_label']['#description'] = t('This content type does not have a title field.');
    $form['submission']['title_label']['#required'] = FALSE;
drumm's avatar
drumm committed
132
  }
133 134 135 136 137 138
  $form['submission']['body_label'] = array(
    '#title' => t('Body field label'),
    '#type' => 'textfield',
    '#default_value' => $type->body_label,
    '#description' => t('To omit the body field for this content type, remove any text and leave this field blank.'),
  );
drumm's avatar
drumm committed
139 140 141 142 143 144 145
  $form['submission']['min_word_count'] = array(
    '#type' => 'select',
    '#title' => t('Minimum number of words'),
    '#default_value' => $type->min_word_count,
    '#options' => drupal_map_assoc(array(0, 10, 25, 50, 75, 100, 125, 150, 175, 200)),
    '#description' => t('The minimum number of words for the body field to be considered valid for this content type. This can be useful to rule out submissions that do not meet the site\'s standards, such as short test posts.')
  );
146 147 148 149 150 151
  $form['submission']['help']  = array(
    '#type' => 'textarea',
    '#title' => t('Explanation or submission guidelines'),
    '#default_value' =>  $type->help,
    '#description' => t('This text will be displayed at the top of the submission form for this content type. It is useful for helping or instructing your users.')
  );
drumm's avatar
drumm committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  $form['workflow'] = array(
    '#type' => 'fieldset',
    '#title' =>t('Workflow'),
    '#collapsible' => TRUE,
  );
  $form['workflow']['node_options'] = array('#type' => 'checkboxes',
    '#title' => t('Default options'),
    '#default_value' => variable_get('node_options_'. $type->type, array('status', 'promote')),
    '#options' => array(
      'status' => t('Published'),
      'promote' => t('Promoted to front page'),
      'sticky' => t('Sticky at top of lists'),
      'revision' => t('Create new revision'),
    ),
    '#description' => t('Users with the <em>administer nodes</em> permission will be able to override these options.'),
  );

  $form['old_type'] = array(
    '#type' => 'value',
    '#value' => $type->type,
  );
  $form['orig_type'] = array(
    '#type' => 'value',
    '#value' => $type->orig_type,
  );
  $form['module'] = array(
    '#type' => 'value',
    '#value' => $type->module,
  );
  $form['custom'] = array(
    '#type' => 'value',
    '#value' => $type->custom,
  );
  $form['modified'] = array(
    '#type' => 'value',
    '#value' => $type->modified,
  );
  $form['locked'] = array(
    '#type' => 'value',
    '#value' => $type->locked,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save content type'),
197
    '#weight' => 40,
drumm's avatar
drumm committed
198 199 200 201 202 203 204
  );

  if ($type->custom) {
    if (!empty($type->type)) {
      $form['delete'] = array(
        '#type' => 'submit',
        '#value' => t('Delete content type'),
205
        '#weight' => 45,
drumm's avatar
drumm committed
206 207 208 209 210 211 212
      );
    }
  }
  else {
    $form['reset'] = array(
      '#type' => 'submit',
      '#value' => t('Reset to defaults'),
213
      '#weight' => 50,
drumm's avatar
drumm committed
214 215 216
    );
  }

217
  return $form;
drumm's avatar
drumm committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
}

/**
 * Implementation of hook_form_validate().
 */
function node_type_form_validate($form_id, $form_values) {
  $type = new stdClass();
  $type->type = trim($form_values['type']);
  $type->name = trim($form_values['name']);

  // Work out what the type was before the user submitted this form
  $old_type = trim($form_values['old_type']);
  if (empty($old_type)) {
    $old_type = $type->type;
  }

  $types = node_get_types('names');

  if (!$form_values['locked']) {
    if (isset($types[$type->type]) && $type->type != $old_type) {
238
      form_set_error('type', t('The machine-readable name %type is already taken.', array('%type' => $type->type)));
drumm's avatar
drumm committed
239
    }
240
    if (!preg_match('!^[a-z0-9_]+$!', $type->type)) {
241
      form_set_error('type', t('The machine-readable name can only consist of lowercase letters, underscores, and numbers.'));
drumm's avatar
drumm committed
242
    }
243 244 245 246
    // The type cannot be just the character '0', since elsewhere we check it using empty().
    if ($type->type === '0') {
      form_set_error('type', t("Invalid type. Please enter a type name other than '0' (the character zero)."));
    }
drumm's avatar
drumm committed
247 248 249 250 251
  }

  $names = array_flip($types);

  if (isset($names[$type->name]) && $names[$type->name] != $old_type) {
252
    form_set_error('name', t('The human-readable name %name is already taken.', array('%name' => $names[$type->name])));
drumm's avatar
drumm committed
253 254 255 256 257 258 259
  }
}

/**
 * Implementation of hook_form_submit().
 */
function node_type_form_submit($form_id, $form_values) {
260
  $op = isset($form_values['op']) ? $form_values['op'] : '';
drumm's avatar
drumm committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274

  $type = new stdClass();

  $type->type = trim($form_values['type']);
  $type->name = trim($form_values['name']);
  $type->orig_type = trim($form_values['orig_type']);
  $type->old_type = isset($form_values['old_type']) ? $form_values['old_type'] : $type->type;

  $type->description = $form_values['description'];
  $type->help = $form_values['help'];
  $type->min_word_count = $form_values['min_word_count'];
  $type->title_label = $form_values['title_label'];
  $type->body_label = $form_values['body_label'];

275 276 277 278 279
  // title_label is required in core; has_title will always be true, unless a
  // module alters the title field.
  $type->has_title = ($type->title_label != '');
  $type->has_body = ($type->body_label != '');

drumm's avatar
drumm committed
280 281 282 283 284 285 286 287 288
  $type->module = !empty($form_values['module']) ? $form_values['module'] : 'node';
  $type->custom = $form_values['custom'];
  $type->modified = TRUE;
  $type->locked = $form_values['locked'];

  if ($op == t('Reset to defaults')) {
    node_type_reset($type);
  }
  elseif ($op == t('Delete content type')) {
289
    return 'admin/content/types/'. str_replace('_', '-', $type->old_type) .'/delete';
drumm's avatar
drumm committed
290 291 292 293
  }

  $status = node_type_save($type);

294 295
  $variables = $form_values;

drumm's avatar
drumm committed
296 297
  // Remove everything that's been saved already - whatever's left is assumed
  // to be a persistent variable.
298
  foreach ($variables as $key => $value) {
drumm's avatar
drumm committed
299
    if (isset($type->$key)) {
300
      unset($variables[$key]);
drumm's avatar
drumm committed
301 302 303
    }
  }

304
  unset($variables['form_token'], $variables['op'], $variables['submit'], $variables['delete'], $variables['reset'], $variables['form_id']);
drumm's avatar
drumm committed
305 306

  // Save or reset persistent variable values.
307
  foreach ($variables as $key => $value) {
drumm's avatar
drumm committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    $key .= '_'. $type->type;
    if ($op == t('Reset to defaults')) {
      variable_del($key);
    }
    else {
      if (is_array($value)) {
        $value = array_keys(array_filter($value));
      }
      variable_set($key, $value);

      if ($type->old_type != $type->type) {
        $key = str_replace($type->type, $type->old_type, $key);
        variable_del($key);
      }
    }
  }

  node_types_rebuild();
326
  // menu_rebuild clears the cache, too
drumm's avatar
drumm committed
327
  menu_rebuild();
328
  $t_args = array('%name' => $type->name);
drumm's avatar
drumm committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

  if ($op == t('Reset to defaults')) {
    drupal_set_message(t('The content type %name has been reset to its default values.', $t_args));
    return;
  }

  if ($status == SAVED_UPDATED) {
    drupal_set_message(t('The content type %name has been updated.', $t_args));
  }
  elseif ($status == SAVED_NEW) {
    drupal_set_message(t('The content type %name has been added.', $t_args));
    watchdog('node', t('Added content type %name.', $t_args), WATCHDOG_NOTICE, l(t('view'), 'admin/content/types'));
  }

  return 'admin/content/types';
}

346 347 348 349
/**
 * Implementation of hook_node_type().
 */
function node_node_type($op, $info) {
350
  if ($op != 'delete' && !empty($info->old_type) && $info->old_type != $info->type) {
351 352 353 354 355 356 357 358 359 360
    $update_count = node_type_update_nodes($info->old_type, $info->type);

    if ($update_count) {
      $substr_pre = 'Changed the content type of ';
      $substr_post = strtr(' from %old-type to %type.', array('%old-type' => theme('placeholder', $info->old_type), '%type' => theme('placeholder', $info->type)));
      drupal_set_message(format_plural($update_count, $substr_pre .'@count post'. $substr_post, $substr_pre .'@count posts'. $substr_post));
    }
  }
}

drumm's avatar
drumm committed
361 362 363 364 365 366 367 368 369 370
/**
 * Resets all of the relevant fields of a module-defined node type to their
 * default values.
 *
 * @param &$type
 *   The node type to reset. The node type is passed back by reference with its
 *   resetted values. If there is no module-defined info for this node type,
 *   then nothing happens.
 */
function node_type_reset(&$type) {
371
  $info_array = module_invoke_all('node_info');
drumm's avatar
drumm committed
372 373 374 375 376 377 378 379 380 381 382 383 384
  if (isset($info_array[$type->orig_type])) {
    $info = _node_type_set_defaults($info_array[$type->orig_type]);
    $info['type'] = $type->orig_type;

    foreach ($info as $field => $value) {
      $type->$field = $value;
    }
  }
}

/**
 * Menu callback; delete a single content type.
 */
385
function node_type_delete_confirm($type) {
drumm's avatar
drumm committed
386 387 388
  $form['type'] = array('#type' => 'value', '#value' => $type->type);
  $form['name'] = array('#type' => 'value', '#value' => $type->name);

389
  $message = t('Are you sure you want to delete the content type %type?', array('%type' => $type->name));
drumm's avatar
drumm committed
390 391 392 393
  $caption = '';

  $num_nodes = db_num_rows(db_query("SELECT * FROM {node} WHERE type = '%s'", $type->type));
  if ($num_nodes) {
394
    $caption .= '<p>'. strtr(format_plural($num_nodes, '<strong>Warning:</strong> there is currently @count %type post on your site. It may not be able to be displayed or edited correctly, once you have removed this content type.', '<strong>Warning:</strong> there are currently @count %type posts on your site. They may not be able to be displayed or edited correctly, once you have removed this content type.'), array('%type' => theme('placeholder', $type->name))) .'</p>';
drumm's avatar
drumm committed
395 396 397 398
  }

  $caption .= '<p>'. t('This action cannot be undone.') .'</p>';

399
  return confirm_form($form, $message, 'admin/content/types', $caption, t('Delete'));
drumm's avatar
drumm committed
400 401 402
}

/**
403
 * Process content type delete confirm submissions.
drumm's avatar
drumm committed
404
 */
405
function node_type_delete_confirm_submit($form_id, $form_values) {
406
  node_type_delete($form_values['type']);
drumm's avatar
drumm committed
407

408
  $t_args = array('%name' => $form_values['name']);
drumm's avatar
drumm committed
409 410 411 412 413 414 415 416
  drupal_set_message(t('The content type %name has been deleted.', $t_args));
  watchdog('menu', t('Deleted content type %name.', $t_args), WATCHDOG_NOTICE);

  node_types_rebuild();
  menu_rebuild();

  return 'admin/content/types';
}