Skip to Content

CCK file fields

Change in plan. I don't think I want to use CCK fields for this after all. I will just try to add e_page form fields into each page that users specify. I have some good example code from bookmadesimple that lists out the node types. After that, I'm thinking it's just a matter of hook_form_alter.

Here are some leads for turning our e_page content type into a CCK content type so that the fields can be splashed around more readily.

I'm actually thinking that putting the fields into cck may not be necessary after all. Getting them into other content types may just be a matter of deploying hook_form_alter. That seems to be what Book Made Simple does. Need to look also at 5Star, maybe.

  • http://civicactions.com/blog/2009/jul/24/using_chaos_tools_module_create_exportables
  • look for import function in the cck module. Is there an API?
  • module: migration
  • module: ctools
  • module: book made simple. how does it get it's form into designated content types edit forms?
  • module:features. this builds the module for you, so maybe we just use it, but there are down sides. It would be easier for site admins to paste our cck type export into the import field than to go through the whole feature installation process. If possible, I'd rather copy the method features uses to do the import than try to cludge a features generated module together with our own.

Code from cck module file content_copy.module

This is the function I think we need to pass the expert data to. The data needs to be in the &$form_state object, and must be named 'macro'.

function content_copy_import_form_submit($form, &$form_state) {

$form_values = $form_state['values'];

// Get the content type we are importing into. $type_name = $form_values['type_name']; $type_label = node_get_types('name', $type_name);

$content = NULL; // Convert the import formatted text back into a $content array. // Return if errors generated or not an array. // Use '@' to suppress errors about undefined constants in the macro. @eval($form_values['macro']);

// Preliminary error trapping, must have valid arrays to work with. if (!isset($content) || !isset($content['type']) || !is_array($content) || !is_array($content['type'])) { form_set_error('macro', t('The import data is not valid import text.')); return; }

module_load_include('inc', 'content', 'includes/content.crud');

// Get all type and field info for this database. $content_info = _content_type_info();

$imported_type = $content['type']; $imported_type_name = $imported_type['type']; $imported_type_label = $imported_type['name'];

// It is allowed to import a type with no fields, // so the fields array could be empty and must be cast as an array. $imported_fields = isset($content['fields']) ? $content['fields'] : array();

// Perform more pre-import error trapping. // If there are potential problems, exit without doing the import. $not_enabled = array();

// The groups array could be empty and still valid, make sure to cast it as an array. // If there are groups in the import, make sure the fieldgroup module is enabled. $imported_groups = array(); if (isset($content['groups']) && module_exists('fieldgroup')) { $imported_groups = (array) $content['groups']; } elseif (isset($content['groups']) && is_array($content['groups'])) { $not_enabled[] = 'fieldgroup'; }

// Make sure that all the field and widget modules in the import are enabled in this database. foreach ($imported_fields as $import) { $field = content_field_instance_collapse($import); if (empty($field['module']) || empty($field['widget_module'])) { $not_enabled[] = $field['field_name']; } else { if (!module_exists($field['module'])) { $not_enabled[] = $field['module']; } if (!module_exists($field['widget_module'])) { $not_enabled[] = $field['widget_module']; } } }

// If any required module is not enabled, set an error message and exit. if ($not_enabled) { form_set_error('macro', t('The following modules must be enabled for this import to work: %modules.', array( '%modules' => implode(', ', array_unique($not_enabled)) ))); }

// Make sure the imported content type doesn't already exist in the database. if ($form_values['type_name'] == '') { if (in_array($imported_type_name, array_keys($content_info['content types']))) { form_set_error('macro', t('The content type %type already exists in this database.', array( '%type' => $imported_type_name ))); } }

if (form_get_errors()) { drupal_set_message(t('Exiting. No import performed.'), 'error'); return; }

// Create the content type, if requested. if ($form_values['type_name'] == '') {

$type = (object) $imported_type;
$values = $imported_type;
// Prevent a warning in node/content_types.inc
$type->has_title = TRUE;
$type_form_state = array('values' => $values);

// There's no API for creating node types, we still have to use drupal_execute().
module_load_include('inc', 'node', 'includes/content_types');
drupal_execute('node_type_form', $type_form_state, $type);

// Reset type and database values once new type has been added.
$type_name  = $imported_type_name;
$type_label = node_get_types('name', $type_name);
content_clear_type_cache();
$content_info = _content_type_info();

if (form_get_errors() || !isset($content_info['content types']) || !is_array($content_info['content types'][$type_name])) {
   drupal_set_message(t('An error has occurred adding the content type %type.<br/>Please check the errors displayed for more details.', array(
        '%type' => $imported_type_name
        )));
   return;
}

}

// Create the groups for this type, if they don't already exist. if (module_exists('fieldgroup') && $imported_groups) { foreach ($imported_groups as $group) { $group_name = $group['group_name']; fieldgroup_save_group($type_name, $group); } // Reset the static variable in fieldgroup_groups() with new data. fieldgroup_groups('', FALSE, TRUE); }

// Iterate through the field forms in the import and execute each. $rebuild = FALSE; foreach ($imported_fields as $field) {

// Make sure the field doesn't already exist in the type.
// If so, do nothing, fields can't be duplicated within a content type.
$field_name   = $field['field_name'];

// Might need to overwrite the content type name if a new type was created.
$field['type_name'] = $type_name;

if (!empty($field['field_name']) && isset($content_info['content types'][$type_name]['fields'][$field_name])) {
  drupal_set_message(t('The imported field %field_label (%field_name) was not added to %type because that field already exists in %type.', array(
    '%field_label' => $field['label'], '%field_name' => $field_name, '%type' => $type_label)));
}
else {
  $field = content_field_instance_create($field, FALSE);
  $rebuild = TRUE;
  drupal_set_message(t('The field %field_label (%field_name) was added to the content type %type.', array(
    '%field_label' => $field['widget']['label'], '%field_name' => $field_name, '%type' => $type_label)));
}

// Fieldgroup module erases all group related data when a module that
// provides a content type is disabled, but CCK does not remove the fields.
// In this case, we should ensure group data related to fields is properly
// restored. Hence, we need to update field group data for newly imported
// field, but also for fields that already exist.
if (module_exists('fieldgroup') && isset($imported_groups)) {
  fieldgroup_update_fields($field);
}

}

// Clear caches and rebuild menu only if any field has been created. if ($rebuild) { content_clear_type_cache(TRUE); menu_rebuild(); }

// Import weights of non-CCK fields. if (isset($content['extra'])) { variable_set('content_extra_weights_'. $type_name, $content['extra']); } }

Your rating: None Average: 1 (3 votes)