You are here

function content_types_install in Content Construction Kit (CCK) 6

Same name and namespace in other branches
  1. 6.3 content.install \content_types_install()
  2. 6.2 content.install \content_types_install()

'Safe' version of content_types() to use in updates and installs.

Can't safely use content_fields() or content_types() in an update to get a fields array, especially without knowing what field modules are enabled, or the current state of the database and cache, so create a fields array from database info that is limited to fields from modules that are currently enabled.

4 calls to content_types_install()
content_associate_fields in ./content.module
Allows a module to update the database for fields and columns it controls.
content_update_6002 in ./content.install
Get rid of automatic per content tables for content types that have no fields. Switching to adding those tables only when needed.
nodereference_update_6000 in modules/nodereference/nodereference.install
All fields must allow NULL values to indicate empty fields.
text_update_6001 in modules/text/text.install
Set all columns to accept NULL values and set empty string values in the database to NULL.

File

./content.install, line 18

Code

function content_types_install() {
  $module_field_types = $module_widgets = array();
  foreach (module_list() as $module) {
    if ($field_type = module_invoke($module, 'field_info')) {
      $module_field_types[$module] = $field_type;
    }
    if ($widget_type = module_invoke($module, 'widget_info')) {
      $module_widgets[$module] = $widget_type;
    }
  }
  $fields = array();
  $db_result = db_query("SELECT * FROM {" . content_instance_tablename() . "} nfi " . " LEFT JOIN {" . content_field_tablename() . "} nf ON nf.field_name = nfi.field_name");
  while ($field = db_fetch_array($db_result)) {

    // There may be module data available for currently disabled modules,
    // or missing module data for currently enabled modules, so start over
    // to get only field info for enabled modules.
    unset($field['module']);
    unset($field['widget_module']);

    // 'columns' is a reserved word in MySQL4, so our column is named 'db_columns'.
    $field['columns'] = $field['db_columns'];
    unset($field['db_columns']);
    foreach ($module_field_types as $module => $types) {
      foreach ($types as $type_name => $type) {
        if ($field['type'] == $type_name) {
          $field['module'] = $module;
        }
      }
    }
    foreach ($module_widgets as $module => $types) {
      foreach ($types as $type_name => $type) {
        if ($field['widget_type'] == $type_name) {
          $field['widget_module'] = $module;
        }
      }
    }
    if (!empty($field['module']) && !empty($field['widget_module'])) {
      $field['widget_settings'] = unserialize($field['widget_settings']);
      $field['display_settings'] = unserialize($field['display_settings']);
      $field['columns'] = module_invoke($field['module'], 'field_settings', 'database columns', $field);
      $fields[$field['type_name']][$field['field_name']] = $field;
    }
  }
  return $fields;
}