[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/modules/node/ -> content_types.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Content type editing UI.
   6   */
   7  
   8  /**
   9   * Displays the content type admin overview page.
  10   */
  11  function node_overview_types() {
  12    $types = node_get_types();
  13    $names = node_get_types('names');
  14    $header = array(t('Name'), t('Type'), t('Description'), array('data' => t('Operations'), 'colspan' => '2'));
  15    $rows = array();
  16  
  17    foreach ($names as $key => $name) {
  18      $type = $types[$key];
  19      if (node_hook($type, 'form')) {
  20        $type_url_str = str_replace('_', '-', $type->type);
  21        $row = array(
  22          l($name, 'admin/content/node-type/'. $type_url_str),
  23          check_plain($type->type),
  24          filter_xss_admin($type->description),
  25        );
  26        // Set the edit column.
  27        $row[] = array('data' => l(t('edit'), 'admin/content/node-type/'. $type_url_str));
  28  
  29        // Set the delete column.
  30        if ($type->custom) {
  31          $row[] = array('data' => l(t('delete'), 'admin/content/node-type/'. $type_url_str .'/delete'));
  32        }
  33        else {
  34          $row[] = array('data' => '');
  35        }
  36        $rows[] = $row;
  37      }
  38    }
  39  
  40    if (empty($rows)) {
  41      $rows[] = array(array('data' => t('No content types available.'), 'colspan' => '5', 'class' => 'message'));
  42    }
  43  
  44    return theme('table', $header, $rows);
  45  }
  46  
  47  /**
  48   * Generates the node type editing form.
  49   */
  50  function node_type_form(&$form_state, $type = NULL) {
  51    if (!isset($type->type)) {
  52      $type = new stdClass();
  53      $type->type = $type->name = $type->module = $type->description = $type->help = '';
  54      $type->min_word_count = 0;
  55      $type->has_title = TRUE;
  56      $type->has_body = TRUE;
  57      $type->title_label = t('Title');
  58      $type->body_label = t('Body');
  59      $type->custom = TRUE;
  60      $type->modified = FALSE;
  61      $type->locked = FALSE;
  62    }
  63  
  64    $form['#node_type'] = $type; // Make the type object available to implementations of hook_form_alter.
  65  
  66    $form['identity'] = array(
  67      '#type' => 'fieldset',
  68      '#title' => t('Identification'),
  69    );
  70    $form['identity']['name'] = array(
  71      '#title' => t('Name'),
  72      '#type' => 'textfield',
  73      '#default_value' => $type->name,
  74      '#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 begin with a capital letter and contain only letters, numbers, and <strong>spaces</strong>. This name must be unique.'),
  75      '#required' => TRUE,
  76    );
  77  
  78    if (!$type->locked) {
  79      $form['identity']['type'] = array(
  80        '#title' => t('Type'),
  81        '#type' => 'textfield',
  82        '#default_value' => $type->type,
  83        '#maxlength' => 32,
  84        '#required' => TRUE,
  85        '#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 must contain only lowercase letters, numbers, and underscores. Underscores will be converted into hyphens when constructing the URL of the <em>create content</em> page. This name must be unique.'),
  86      );
  87    }
  88    else {
  89      $form['identity']['type'] = array(
  90        '#type' => 'value',
  91        '#value' => $type->type,
  92      );
  93      $form['identity']['type_display'] = array(
  94        '#title' => t('Type'),
  95        '#type' => 'item',
  96        '#value' => theme('placeholder', $type->type),
  97        '#description' => t('The machine-readable name of this content type. This field cannot be modified for system-defined content types.'),
  98      );
  99    }
 100  
 101    $form['identity']['description'] = array(
 102      '#title' => t('Description'),
 103      '#type' => 'textarea',
 104      '#default_value' => $type->description,
 105      '#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.'),
 106      );
 107  
 108    $form['submission'] = array(
 109      '#type' => 'fieldset',
 110      '#title' => t('Submission form settings'),
 111      '#collapsible' => TRUE,
 112      '#collapsed' => TRUE,
 113    );
 114    $form['submission']['title_label'] = array(
 115      '#title' => t('Title field label'),
 116      '#type' => 'textfield',
 117      '#default_value' => $type->title_label,
 118      '#required' => TRUE,
 119    );
 120    if (!$type->has_title) {
 121      // Avoid overwriting a content type that intentionally does not have a
 122      // title field.
 123      $form['submission']['title_label']['#attributes'] = array('disabled' => 'disabled');
 124      $form['submission']['title_label']['#description'] = t('This content type does not have a title field.');
 125      $form['submission']['title_label']['#required'] = FALSE;
 126    }
 127    $form['submission']['body_label'] = array(
 128      '#title' => t('Body field label'),
 129      '#type' => 'textfield',
 130      '#default_value' => isset($type->body_label) ? $type->body_label : '',
 131      '#description' => t('To omit the body field for this content type, remove any text and leave this field blank.'),
 132    );
 133    $form['submission']['min_word_count'] = array(
 134      '#type' => 'select',
 135      '#title' => t('Minimum number of words'),
 136      '#default_value' => $type->min_word_count,
 137      '#options' => drupal_map_assoc(array(0, 1, 10, 25, 50, 75, 100, 125, 150, 175, 200)),
 138      '#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.')
 139    );
 140    $form['submission']['help']  = array(
 141      '#type' => 'textarea',
 142      '#title' => t('Explanation or submission guidelines'),
 143      '#default_value' => $type->help,
 144      '#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.')
 145    );
 146    $form['workflow'] = array(
 147      '#type' => 'fieldset',
 148      '#title' => t('Workflow settings'),
 149      '#collapsible' => TRUE,
 150      '#collapsed' => TRUE,
 151    );
 152    $form['workflow']['node_options'] = array('#type' => 'checkboxes',
 153      '#title' => t('Default options'),
 154      '#default_value' => variable_get('node_options_'. $type->type, array('status', 'promote')),
 155      '#options' => array(
 156        'status' => t('Published'),
 157        'promote' => t('Promoted to front page'),
 158        'sticky' => t('Sticky at top of lists'),
 159        'revision' => t('Create new revision'),
 160      ),
 161      '#description' => t('Users with the <em>administer nodes</em> permission will be able to override these options.'),
 162    );
 163  
 164    $form['old_type'] = array(
 165      '#type' => 'value',
 166      '#value' => $type->type,
 167    );
 168    $form['orig_type'] = array(
 169      '#type' => 'value',
 170      '#value' => isset($type->orig_type) ? $type->orig_type : '',
 171    );
 172    $form['module'] = array(
 173      '#type' => 'value',
 174      '#value' => $type->module,
 175    );
 176    $form['custom'] = array(
 177      '#type' => 'value',
 178      '#value' => $type->custom,
 179    );
 180    $form['modified'] = array(
 181      '#type' => 'value',
 182      '#value' => $type->modified,
 183    );
 184    $form['locked'] = array(
 185      '#type' => 'value',
 186      '#value' => $type->locked,
 187    );
 188  
 189    $form['submit'] = array(
 190      '#type' => 'submit',
 191      '#value' => t('Save content type'),
 192      '#weight' => 40,
 193    );
 194  
 195    if ($type->custom) {
 196      if (!empty($type->type)) {
 197        $form['delete'] = array(
 198          '#type' => 'submit',
 199          '#value' => t('Delete content type'),
 200          '#weight' => 45,
 201        );
 202      }
 203    }
 204    else {
 205      $form['reset'] = array(
 206        '#type' => 'submit',
 207        '#value' => t('Reset to defaults'),
 208        '#weight' => 50,
 209      );
 210    }
 211  
 212    return $form;
 213  }
 214  
 215  /**
 216   * Validates the content type submission form generated by node_type_form().
 217   */
 218  function node_type_form_validate($form, &$form_state) {
 219    $type = new stdClass();
 220    $type->type = trim($form_state['values']['type']);
 221    $type->name = trim($form_state['values']['name']);
 222  
 223    // Work out what the type was before the user submitted this form
 224    $old_type = trim($form_state['values']['old_type']);
 225  
 226    $types = node_get_types('names');
 227  
 228    if (!$form_state['values']['locked']) {
 229      if (isset($types[$type->type]) && $type->type != $old_type) {
 230        form_set_error('type', t('The machine-readable name %type is already taken.', array('%type' => $type->type)));
 231      }
 232      if (!preg_match('!^[a-z0-9_]+$!', $type->type)) {
 233        form_set_error('type', t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
 234      }
 235      // 'theme' conflicts with theme_node_form().
 236      // '0' is invalid, since elsewhere we check it using empty().
 237      if (in_array($type->type, array('0', 'theme'))) {
 238        form_set_error('type', t("Invalid machine-readable name. Please enter a name other than %invalid.", array('%invalid' => $type->type)));
 239      }
 240    }
 241  
 242    $names = array_flip($types);
 243  
 244    if (isset($names[$type->name]) && $names[$type->name] != $old_type) {
 245      form_set_error('name', t('The human-readable name %name is already taken.', array('%name' => $type->name)));
 246    }
 247  }
 248  
 249  /**
 250   * Implementation of hook_form_submit().
 251   */
 252  function node_type_form_submit($form, &$form_state) {
 253    $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
 254  
 255    $type = new stdClass();
 256  
 257    $type->type = trim($form_state['values']['type']);
 258    $type->name = trim($form_state['values']['name']);
 259    $type->orig_type = trim($form_state['values']['orig_type']);
 260    $type->old_type = isset($form_state['values']['old_type']) ? $form_state['values']['old_type'] : $type->type;
 261  
 262    $type->description = $form_state['values']['description'];
 263    $type->help = $form_state['values']['help'];
 264    $type->min_word_count = $form_state['values']['min_word_count'];
 265    $type->title_label = $form_state['values']['title_label'];
 266    $type->body_label = $form_state['values']['body_label'];
 267  
 268    // title_label is required in core; has_title will always be true, unless a
 269    // module alters the title field.
 270    $type->has_title = ($type->title_label != '');
 271    $type->has_body = ($type->body_label != '');
 272  
 273    $type->module = !empty($form_state['values']['module']) ? $form_state['values']['module'] : 'node';
 274    $type->custom = $form_state['values']['custom'];
 275    $type->modified = TRUE;
 276    $type->locked = $form_state['values']['locked'];
 277  
 278    if ($op == t('Reset to defaults')) {
 279      node_type_reset($type);
 280    }
 281    elseif ($op == t('Delete content type')) {
 282      $form_state['redirect'] = 'admin/content/node-type/'. str_replace('_', '-', $type->old_type) .'/delete';
 283      return;
 284    }
 285  
 286    $status = node_type_save($type);
 287  
 288    $variables = $form_state['values'];
 289  
 290    // Remove everything that's been saved already - whatever's left is assumed
 291    // to be a persistent variable.
 292    foreach ($variables as $key => $value) {
 293      if (isset($type->$key)) {
 294        unset($variables[$key]);
 295      }
 296    }
 297  
 298    unset($variables['form_token'], $variables['op'], $variables['submit'], $variables['delete'], $variables['reset'], $variables['form_id']);
 299  
 300    // Save or reset persistent variable values.
 301    foreach ($variables as $key => $value) {
 302      $variable_new = $key .'_'. $type->type;
 303      $variable_old = $key .'_'. $type->old_type;
 304  
 305      if ($op == t('Reset to defaults')) {
 306        variable_del($variable_old);
 307      }
 308      else {
 309        if (is_array($value)) {
 310          $value = array_keys(array_filter($value));
 311        }
 312        variable_set($variable_new, $value);
 313  
 314        if ($variable_new != $variable_old) {
 315          variable_del($variable_old);
 316        }
 317      }
 318    }
 319  
 320    node_types_rebuild();
 321    menu_rebuild();
 322    $t_args = array('%name' => $type->name);
 323  
 324    if ($op == t('Reset to defaults')) {
 325      drupal_set_message(t('The content type %name has been reset to its default values.', $t_args));
 326      return;
 327    }
 328  
 329    if ($status == SAVED_UPDATED) {
 330      drupal_set_message(t('The content type %name has been updated.', $t_args));
 331    }
 332    elseif ($status == SAVED_NEW) {
 333      drupal_set_message(t('The content type %name has been added.', $t_args));
 334      watchdog('node', 'Added content type %name.', $t_args, WATCHDOG_NOTICE, l(t('view'), 'admin/content/types'));
 335    }
 336  
 337    $form_state['redirect'] = 'admin/content/types';
 338    return;
 339  }
 340  
 341  /**
 342   * Implementation of hook_node_type().
 343   */
 344  function node_node_type($op, $info) {
 345    if ($op != 'delete' && !empty($info->old_type) && $info->old_type != $info->type) {
 346      $update_count = node_type_update_nodes($info->old_type, $info->type);
 347  
 348      if ($update_count) {
 349        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)));
 350      }
 351    }
 352  }
 353  
 354  /**
 355   * Resets all of the relevant fields of a module-defined node type to their
 356   * default values.
 357   *
 358   * @param &$type
 359   *   The node type to reset. The node type is passed back by reference with its
 360   *   resetted values. If there is no module-defined info for this node type,
 361   *   then nothing happens.
 362   */
 363  function node_type_reset(&$type) {
 364    $info_array = module_invoke_all('node_info');
 365    if (isset($info_array[$type->orig_type])) {
 366      $info_array[$type->orig_type]['type'] = $type->orig_type; 
 367      $info = _node_type_set_defaults($info_array[$type->orig_type]);
 368  
 369      foreach ($info as $field => $value) {
 370        $type->$field = $value;
 371      }
 372    }
 373  }
 374  
 375  /**
 376   * Menu callback; delete a single content type.
 377   */
 378  function node_type_delete_confirm(&$form_state, $type) {
 379    $form['type'] = array('#type' => 'value', '#value' => $type->type);
 380    $form['name'] = array('#type' => 'value', '#value' => $type->name);
 381  
 382    $message = t('Are you sure you want to delete the content type %type?', array('%type' => $type->name));
 383    $caption = '';
 384  
 385    $num_nodes = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = '%s'", $type->type));
 386    if ($num_nodes) {
 387      $caption .= '<p>'. format_plural($num_nodes, '<strong>Warning:</strong> there is currently 1 %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' => $type->name)) .'</p>';
 388    }
 389  
 390    $caption .= '<p>'. t('This action cannot be undone.') .'</p>';
 391  
 392    return confirm_form($form, $message, 'admin/content/types', $caption, t('Delete'));
 393  }
 394  
 395  /**
 396   * Process content type delete confirm submissions.
 397   */
 398  function node_type_delete_confirm_submit($form, &$form_state) {
 399    node_type_delete($form_state['values']['type']);
 400  
 401    $t_args = array('%name' => $form_state['values']['name']);
 402    drupal_set_message(t('The content type %name has been deleted.', $t_args));
 403    watchdog('menu', 'Deleted content type %name.', $t_args, WATCHDOG_NOTICE);
 404  
 405    node_types_rebuild();
 406    menu_rebuild();
 407  
 408    $form_state['redirect'] = 'admin/content/types';
 409    return;
 410  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7