##, on the properties array to reset. static $weight = 1; $weight = isset($properties['weight']) ? $properties['weight'] : $weight + 1; $widget_types = install_get_widget_types(); if (!array_key_exists($field_widget_type, $widget_types)) { drupal_set_message("'$field_widget_type' is not an available field/widget."); return; } // First step of creating the field - create the basic field. // Corresponds to first page of the Add field UI. $default_field_add = array( 'type_name' => $content_type, 'field_name' => $field_name, 'field_widget_type' => $field_widget_type, 'label' => $label, 'op' => 'Create field', 'submit' => 'Create field', 'form_id' => '_content_admin_field_add_new', ); // Create our new field. _content_admin_field_add_new_submit('_content_admin_field_add_new', $default_field_add); // Second step, update the field with our custom properties. // Corresponds to the second page (general config page) of content type creation. // We define defaults for a number of different field/widget types (eg. "referenceable_types" // is applicable to nodereference. These are ignored in cck when not applicable. $default_field_edit = array( 'type_name' => $content_type, 'field_name' => $field_name, 'label' => $label, 'weight' => $weight, 'description' => '', 'default_value_php' => '', 'group' => 0, 'required' => 0, 'multiple' => FALSE, 'text_processing' => 0, 'max_length' => '', 'allowed_values' => '', 'allowed_values_php' => '', 'rows' => 1, 'op' => 'Save field settings', 'submit' => 'Save field settings', 'form_id' => '_content_admin_field', 'referenceable_types' => array(), // Used in nodereference fields and userreference ); foreach($default_field_edit AS $key => $value) { if (isset($properties[$key])) { $default_field_edit[$key] = $properties[$key]; } } if (isset($properties['default_value'])) { $default_field_edit['default_value'][0]['value'] = $properties['default_value']; } // Derive the widget and type from the input type. $widget_parts = explode('-', $field_widget_type); $default_field_edit['field_type'] = $widget_parts[0]; $default_field_edit['widget_type'] = $widget_parts[1]; $default_field_edit['module'] = $widget_parts[0] .', optionwidgets'; _content_admin_field_submit('_content_admin_field_add_new', $default_field_edit); } /** * Returns an array of available fieldtype-widgettype arguments to * the $field_widget_type parameter of install_create_field() */ function install_get_widget_types() { $field_types = _content_field_types(); $widget_types = _content_widget_types(); $field_type_options = array(); foreach ($field_types as $field_name => $field_type) { foreach ($widget_types as $widget_name => $widget_type) { if (in_array($field_name, $widget_type['field types'])) { $field_type_options[$field_name .'-'. $widget_name] = $field_type['label'] .': '. $widget_type['label']; } } } return $field_type_options; } /** * Add an existing field to another content type. * * @param $content_type * Required. Existing content type (text id). * @param $field_name * Required. Existing field name (text id). */ function install_add_existing_field($content_type, $field_name) { // Defaults for passing to submit function. $form_values = array( 'field_name' => $field_name, 'type_name' => $content_type, 'op' => 'Add field', 'submit' => 'Add field', ); _content_admin_field_add_existing_submit('_content_admin_field_add_existing', $form_values); } /** * Create a new field group for an existing content type. * * @param $content_type * Required. Existing content type (text id). * @param $group_name * An alpha-numeric/underscore group name. * @param $label * Required. A nice text name for your group. * @param $settings * Optional. An array of settings, see the default array defined in the * function for an example. * @param $weight * Optional, a standard weight integer. */ function install_create_field_group($content_type, $group_name, $label, $settings = array(), $weight = 0) { if (!is_array($settings)) { // Default settings array. $settings = array( 'form' => array('style' => 'fieldset', 'description' => ''), 'display' => array('description' => '', 'teaser' => NULL, 'label' => NULL), ); } db_query("INSERT INTO {node_group} (type_name, group_name, label, settings, weight) VALUES ('%s', '%s', '%s', '%s', %d)", $content_type, $group_name, $label, serialize($settings), $weight); } /** * Assign fields to a field group. * * @param $content_type * Required. Existing content type (text id). * @param $group_name * Required. Existing group name (text id). * @param $fields * Required. An array of field names. */ function install_fields_in_group($content_type, $group_name, $fields) { if (!is_array($fields)) { $fields = array($fields); } foreach ($fields AS $field_name) { db_query("INSERT INTO {node_group_fields} (type_name, group_name, field_name) VALUES ('%s', '%s', '%s')", $content_type, $group_name, $field_name); } }