You are here

function _content_admin_field_add_new_submit in Content Construction Kit (CCK) 5

Same name and namespace in other branches
  1. 6 includes/content.admin.inc \_content_admin_field_add_new_submit()

Create a new field for a content type.

File

./content_admin.inc, line 629
Administrative interface for content type creation.

Code

function _content_admin_field_add_new_submit($form_id, $form_values) {

  // Find a valid, computer-friendly field name.
  $fields = content_fields();
  $type = content_types($form_values['type_name']);

  // Accept field name from programmed submissions if valid and it doesn't already exist.
  if ($form_values['field_name']) {
    $field_name = $form_values['field_name'];
  }
  else {
    $field_name = trim($form_values['label']);
    $field_name = drupal_strtolower($field_name);
    $field_name = str_replace(array(
      ' ',
      '-',
    ), '_', $field_name);
    $field_name = preg_replace('/[^a-z0-9_]/', '', $field_name);
    $field_name = 'field_' . $field_name;
    $field_name = substr($field_name, 0, 31);
    if (isset($fields[$field_name])) {
      $counter = 0;
      do {
        $new_name = substr($field_name, 0, 29) . '_' . $counter++;
      } while (isset($fields[$new_name]));
      $field_name = $new_name;
    }
  }
  $field_widget_type = explode('-', $form_values['field_widget_type']);
  db_query("INSERT INTO {node_field} (field_name, type, global_settings, required, multiple, db_storage) VALUES ('%s', '%s', '%s', %d, %d, %d)", $field_name, $field_widget_type[0], serialize(array()), 0, 0, CONTENT_DB_STORAGE_PER_CONTENT_TYPE);
  db_query("INSERT INTO {node_field_instance} (field_name, type_name, weight, label, widget_type, widget_settings, display_settings, description) VALUES ('%s', '%s', %d, '%s', '%s', '%s', '%s', '%s')", $field_name, $form_values['type_name'], 0, $form_values['label'], $field_widget_type[1], serialize(array()), serialize(array()), '');
  content_clear_type_cache();

  // Create new database columns as necessary.
  $field_types = _content_field_types();
  $field_type = $field_types[$field_widget_type[0]];
  $field = content_fields($field_name);
  $columns = module_invoke($field_type['module'], 'field_settings', 'database columns', $field);
  if (is_array($columns) && count($columns)) {
    content_alter_db_field(array(), array(), $field, $columns);
  }
  drupal_set_message(t('Created field %label.', array(
    '%label' => $form_values['label'],
  )));
  return 'admin/content/types/' . $type['url_str'] . '/fields/' . $field_name;
}