I'm having some problems getting my head around the CCK api. The module I'm working on should create a few CCK fields and add them to a content type. The documentation I've found at http://drupal.org/node/101742 is geared towards creating your own field types and widgets... but I only want to create fields of an existing type (that is, a field name and some default values) and have the validation etc. handled by CCK instead of defining it myself - similarly to adding these fields with the interface in Drupal.

Is this doable, and if so, could somebody point me in the right direction?

Thanks.

Comments

stdbrouw’s picture

No idea, anybody?

bestknight’s picture

Could this help you? http://drupal.org/node/293663

stdbrouw’s picture

Not really unfortunately. I need to add new CCK fields, not the contents of those fields.

KingMoore’s picture

try this:


/**
 * Create a new field for an existing content type.
 *
 * @param $content_type       Required. Existing content type (text id).
 * @param $field_name         Required. A field name using only alpha-numeric and underscores.
 * @param $field_widget_type  Required. A special name that uniquely identifies both the field type
 *                            and widget type. Each option item on the first page of the 'Add Field' form.
 * @param $properties         Optional. An array of additional properties for the field.
 *
 * NOTE: Use install add_existing_field() to add an existing field.
 *
 * TIP: You can call print_r(install_get_widget_types()); to display a list of available
 *      $field_widget_type options.
 */
function install_create_field($content_type, $field_name, $field_widget_type, $label, $properties = array()) {

  include_once(drupal_get_path('module', 'content') .'/content_admin.inc');

  // Manage weight, so that fields are ordered as they are created.
  // Pass 'weight' => ##, 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',
  );
  foreach($properties as $p => $prop) {
    // Or just array merge...
    if (!isset($default_field_edit[$p])) {
      $default_field_edit[$p] = $prop;
    }
  }  
  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;
}

like this:

  //set field properties
  $properties = array (
    'allowed_values' => "0|No\n1|Yes",
    'required' => '1',
  );

  //create field
  install_create_field('basic', 'field_basic_problematic', 'number_integer-options_select', 'Problematic', $properties);

note that I can get a list of all 'widget_types' like this:

dpr(install_get_widget_types());
stdbrouw’s picture

Cool, thanks! I'd been searching through the CCK code but couldn't find what I needed. Apparently you're better at it than I am :-)

mecano’s picture

will this somehow make it through cck main code?

reaper2006’s picture

Hi,

I'm with one problem. I need to present some result of query result like a field in all drupal nodes.
I pass to explain.
I execute some queries, and i need to present in node, as link to another contents. But my problem is in, how to do this....

I already change the form of add and edit, to add somes fields as i need.. But I can't add some like this in final node form.

Anyone can help me??

Thanks