[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/cck/includes/ -> content.crud.inc (source)

   1  <?php
   2  // $Id: content.crud.inc,v 1.76.2.17 2009/07/14 22:17:05 yched Exp $
   3  
   4  /**
   5   * @file
   6   * Create/Read/Update/Delete functions for CCK-defined object types.
   7   *
   8   * The content module field API will allow $field arguments to
   9   * be input either in the field => widget nested array that is used
  10   * by the content module, or in flattened $form_values arrays, by
  11   * converting flattened arrays to the nested format.
  12   *
  13   * A hook_content_fieldapi() is available for each field instance action,
  14   * and each hook receives the nested field => widget array as an argument.
  15   *
  16   * The hook_content_fieldapi() $ops include:
  17   *
  18   *   - create instance
  19   *   - read instance
  20   *   - update instance
  21   *   - delete instance
  22   *
  23   * Another function, content_module_delete($module) will clean up
  24   * after a module that has been deleted by removing all data and
  25   * settings information that was created by that module.
  26   */
  27  
  28  /**
  29   * Create an array of default values for a field type.
  30   */
  31  function content_field_default_values($field_type) {
  32    $field_types = _content_field_types();
  33    $module = $field_types[$field_type]['module'];
  34  
  35    $field = array(
  36      'module' => $module,
  37      'type' => $field_type,
  38      'active' => 0,
  39    );
  40  
  41    if (module_exists($module)) {
  42      $field['active'] = 1;
  43    }
  44  
  45    $field['columns'] = (array) module_invoke($module, 'field_settings', 'database columns', $field);
  46    // Ensure columns always default to NULL values.
  47    foreach ($field['columns'] as $column_name => $column) {
  48      $field['columns'][$column_name]['not null'] = FALSE;
  49    }
  50  
  51    $field['required'] = 0;
  52    $field['multiple'] = 0;
  53    $field['db_storage'] = CONTENT_DB_STORAGE_PER_CONTENT_TYPE;
  54  
  55    // Make sure field settings all have an index in the array.
  56    $setting_names = (array) module_invoke($module, 'field_settings', 'save', $field);
  57    drupal_alter('field_settings', $setting_names, 'save', $field);
  58    foreach ($setting_names as $setting) {
  59      $field[$setting] = NULL;
  60    }
  61    return $field;
  62  }
  63  
  64  /**
  65   * Create an array of default values for a field instance.
  66   */
  67  function content_instance_default_values($field_name, $type_name, $widget_type) {
  68    $widget_types = _content_widget_types();
  69    $module = $widget_types[$widget_type]['module'];
  70  
  71    $widget = array(
  72      'field_name' => $field_name,
  73      'type_name' => $type_name,
  74      'weight' => 0,
  75      'label' => $field_name,
  76      'description' => '',
  77      'widget_type' => $widget_type,
  78      'widget_module' => $module,
  79      'display_settings' => array(),
  80      'widget_settings' => array(),
  81    );
  82  
  83    if (module_exists($module)) {
  84      $widget['widget_active'] = 1;
  85    }
  86  
  87    $settings_names = array_merge(array('label'), array_keys(content_build_modes()));
  88    $widget['display_settings'] = array();
  89    foreach ($settings_names as $name) {
  90      $widget['display_settings'][$name]['format'] = ($name == 'label') ? 'above' : 'default';
  91      $widget['display_settings'][$name]['exclude'] = 0;
  92    }
  93  
  94    // Make sure widget settings all have an index in the array.
  95    $settings_names = (array) module_invoke($module, 'widget_settings', 'save', $widget);
  96    drupal_alter('widget_settings', $settings_names, 'save', $widget);
  97    $widget['widget_settings'] = array();
  98    foreach ($settings_names as $name) {
  99      $widget['widget_settings'][$name] = NULL;
 100    }
 101    return $widget;
 102  }
 103  
 104  /**
 105   * Expand field info to create field => widget info.
 106   */
 107  function content_field_instance_expand($field) {
 108    if (isset($field['widget'])) {
 109      return $field;
 110    }
 111    $field['widget'] = !empty($field['widget_settings']) ? $field['widget_settings'] : array();
 112    $field['widget']['label'] = !empty($field['label']) ? $field['label'] : $field['field_name'];
 113    $field['widget']['weight'] = !empty($field['weight']) ? $field['weight'] : 0;
 114    $field['widget']['description'] = !empty($field['description']) ? $field['description'] : '';
 115  
 116    if (!empty($field['widget_type'])) {
 117      $field['widget']['type'] = $field['widget_type'];
 118      $widget_types = _content_widget_types();
 119      $field['widget']['module'] = isset($widget_types[$field['widget_type']]['module']) ? $widget_types[$field['widget_type']]['module'] : $field['widget_module'];
 120    }
 121    elseif (!empty($field['widget_module'])) {
 122      $field['widget']['module'] = $field['widget_module'];
 123    }
 124  
 125    unset($field['widget_type']);
 126    unset($field['weight']);
 127    unset($field['label']);
 128    unset($field['description']);
 129    unset($field['widget_module']);
 130    unset($field['widget_settings']);
 131  
 132    // If content.module is handling the default value,
 133    // initialize $widget_settings with default values,
 134    if (isset($field['default_value']) && isset($field['default_value_php']) &&
 135        content_callback('widget', 'default value', $field) == CONTENT_CALLBACK_DEFAULT) {
 136      $field['widget']['default_value'] = !empty($field['default_value']) ? $field['default_value']  : NULL;
 137      $field['widget']['default_value_php'] = !empty($field['default_value_php']) ? $field['default_value_php'] : NULL;
 138      unset($field['default_value']);
 139      unset($field['default_value_php']);
 140    }
 141    return $field;
 142  }
 143  
 144  /**
 145   * Collapse field info from field => widget to flattened form values.
 146   */
 147  function content_field_instance_collapse($field) {
 148    if (!isset($field['widget'])) {
 149      return $field;
 150    }
 151    $field['widget_settings'] = !empty($field['widget']) ? $field['widget'] : array();
 152    $field['widget_type'] = !empty($field['widget']['type']) ? $field['widget']['type'] : '';
 153    $field['weight'] = !empty($field['widget']['weight']) ? $field['widget']['weight'] : 0;
 154    $field['label'] = !empty($field['widget']['label']) ? $field['widget']['label'] : $field['field_name'];
 155    $field['description'] = !empty($field['widget']['description']) ? $field['widget']['description'] : '';
 156    $field['type_name'] = !empty($field['type_name']) ? $field['type_name'] : '';
 157  
 158    if (!empty($field['widget']['module'])) {
 159      $widget_module = $field['widget']['module'];
 160    }
 161    elseif (!empty($field['widget']['type'])) {
 162      $widget_types = _content_widget_types();
 163      $widget_module = $widget_types[$field['widget']['type']]['module'];
 164    }
 165    else {
 166      $widget_module = '';
 167    }
 168    $field['widget_module'] = $widget_module;
 169    unset($field['widget_settings']['type']);
 170    unset($field['widget_settings']['weight']);
 171    unset($field['widget_settings']['label']);
 172    unset($field['widget_settings']['description']);
 173    unset($field['widget_settings']['module']);
 174    unset($field['widget']);
 175    return $field;
 176  }
 177  
 178  /**
 179   * Create a new field instance.
 180   *
 181   * @param $field
 182   *   An array of properties to create the field with, input either in
 183   *   the field => widget format used by the content module or as an
 184   *   array of form values.
 185   *
 186   *   Required values:
 187   *   - field_name, the name of the field to be created
 188   *   - type_name, the content type of the instance to be created
 189   *
 190   *   If there is no prior instance to create this from, we also need:
 191   *   - type, the type of field to create
 192   *   - widget_type, the type of widget to use
 193   * @param $rebuild
 194   *   TRUE to clear content type caches and rebuild menu (default).
 195   *   FALSE allows the caller to process several fields at a time quickly, but then
 196   *   the caller is reponsible to clear content type caches and rebuild menu as soon
 197   *   as all fields have been processed. For example:
 198   *   @code
 199   *     // Create several fields at a time.
 200   *     foreach ($fields as $field) {
 201   *       content_field_instance_create($field, FALSE);
 202   *     }
 203   *     // Clear caches and rebuild menu.
 204   *     content_clear_type_cache(TRUE);
 205   *     menu_rebuild();
 206   *   @endcode
 207   * @see content_clear_type_cache()
 208   * @see menu_rebuild()
 209   */
 210  function content_field_instance_create($field, $rebuild = TRUE) {
 211    include_once('./'. drupal_get_path('module', 'content') .'/includes/content.admin.inc');
 212  
 213    $form_values = $field;
 214    $field = content_field_instance_expand($field);
 215  
 216    // If there are prior instances, fill out missing values from the prior values,
 217    // otherwise get missing values from default values.
 218    $prior_instances = content_field_instance_read(array('field_name' => $field['field_name']));
 219    if (!empty($prior_instances) && is_array($prior_instances)) {
 220      $prev_field = content_field_instance_expand($prior_instances[0]);
 221  
 222      // Weight, label, and description may have been forced into the $field
 223      // by content_field_instance_expand(). If there is a previous instance to
 224      // get these values from and there was no value supplied originally, use
 225      // the previous value.
 226      $field['widget']['weight'] = isset($form_values['weight']) ? $form_values['weight'] : $prev_field['widget']['weight'];
 227      $field['widget']['label']  = isset($form_values['label']) ? $form_values['label'] : $prev_field['widget']['label'];
 228      $field['widget']['description'] = isset($form_values['description']) ? $form_values['description'] : $prev_field['widget']['description'];
 229    }
 230    else {
 231      $prev_field = array('widget' => array());
 232    }
 233  
 234    // If we have a field type, we can build default values for this field type.
 235    $default_values = array('widget' => array());
 236    if (isset($field['type'])) {
 237      $default_values = content_field_default_values($field['type']);
 238      $default_instance_values = content_instance_default_values($field['field_name'], $field['type_name'], $field['widget']['type']);
 239      $default_values = content_field_instance_expand(array_merge($default_values, $default_instance_values));
 240    }
 241  
 242    // Merge default values, previous values, and current values to create
 243    // a complete field array.
 244    $widget = array_merge($default_values['widget'], $prev_field['widget'], $field['widget']);
 245    $field = array_merge($default_values, $prev_field, $field);
 246    $field['widget'] = $widget;
 247  
 248    // Make sure we know what module to invoke for field info.
 249    if (empty($field['module']) && !empty($field['type'])) {
 250      $field_types = _content_field_types();
 251      $field['module'] = $field_types[$field['type']]['module'];
 252    }
 253  
 254    // The storage type may need to be updated.
 255    $field['db_storage'] = content_storage_type($field);
 256  
 257    // Get a fresh copy of the column information whenever a field is created.
 258    $field['columns'] = (array) module_invoke($field['module'], 'field_settings', 'database columns', $field);
 259  
 260    if (empty($prev_field['widget']) || $prior_instances < 1) {
 261      // If this is the first instance, create the field.
 262      $field['db_storage'] = $field['multiple'] > 0 ? CONTENT_DB_STORAGE_PER_FIELD : CONTENT_DB_STORAGE_PER_CONTENT_TYPE;
 263      _content_field_write($field, 'create');
 264    }
 265    elseif (!empty($prev_field['widget']) && $prev_field['db_storage'] == CONTENT_DB_STORAGE_PER_CONTENT_TYPE && count($prior_instances) > 0) {
 266      // If the database storage has changed, update the field and previous instances.
 267      $field['db_storage'] = CONTENT_DB_STORAGE_PER_FIELD;
 268  
 269      foreach ($prior_instances as $instance) {
 270        $new_instance = $instance;
 271        $new_instance['db_storage'] = CONTENT_DB_STORAGE_PER_FIELD;
 272  
 273        // Invoke hook_content_fieldapi().
 274        module_invoke_all('content_fieldapi', 'update instance', $new_instance);
 275  
 276        content_alter_schema($instance, $new_instance);
 277      }
 278    }
 279  
 280    // Invoke hook_content_fieldapi().
 281    module_invoke_all('content_fieldapi', 'create instance', $field);
 282  
 283    // Update the field and the instance with the latest values.
 284    _content_field_write($field, 'update');
 285    _content_field_instance_write($field, 'create');
 286  
 287    content_alter_schema(array(), $field);
 288  
 289    if ($rebuild) {
 290      content_clear_type_cache(TRUE);
 291      menu_rebuild();
 292    }
 293  
 294    return $field;
 295  }
 296  
 297  /**
 298   * Update an existing field instance.
 299   *
 300   * @param $field
 301   *   An array of properties to update the field with, input either in
 302   *   the field => widget format used by the content module or as an
 303   *   array of form values.
 304   * @param $rebuild
 305   *   TRUE to clear content type caches and rebuild menu (default).
 306   *   FALSE allows the caller to process several fields at a time quickly, but then
 307   *   the caller is reponsible to clear content type caches and rebuild menu as soon
 308   *   as all fields have been processed. For example:
 309   *   @code
 310   *     // Update several fields at a time.
 311   *     foreach ($fields as $field) {
 312   *       content_field_instance_update($field, FALSE);
 313   *     }
 314   *     // Clear caches and rebuild menu.
 315   *     content_clear_type_cache(TRUE);
 316   *     menu_rebuild();
 317   *   @endcode
 318   * @see content_clear_type_cache()
 319   * @see menu_rebuild()
 320   */
 321  function content_field_instance_update($field, $rebuild = TRUE) {
 322    include_once('./'. drupal_get_path('module', 'content') .'/includes/content.admin.inc');
 323  
 324    // Ensure the field description is in the 'expanded' form.
 325    $field = content_field_instance_expand($field);
 326  
 327    // Get the previous value from the table.
 328    $previous = content_field_instance_read(array('field_name' => $field['field_name'], 'type_name' => $field['type_name']));
 329    $prev_field = array_pop($previous);
 330  
 331    // Create a complete field array by merging the previous and current values,
 332    // letting the current values overwrite the previous ones.
 333    $widget = array_merge($prev_field['widget'], $field['widget']);
 334    $field = array_merge($prev_field, $field);
 335    $field['widget'] = $widget;
 336  
 337    // Make sure we know what module to invoke for field info.
 338    if (empty($field['module']) && !empty($field['type'])) {
 339      $field_types = _content_field_types();
 340      $field['module'] = $field_types[$field['type']]['module'];
 341    }
 342  
 343    // The storage type may need to be updated.
 344    $field['db_storage'] = content_storage_type($field);
 345  
 346    // Changes in field values may affect columns, or column
 347    // information may have changed, get a fresh copy.
 348    $field['columns'] = (array) module_invoke($field['module'], 'field_settings', 'database columns', $field);
 349  
 350    // If the database storage has changed, update the field and previous instances.
 351    $prior_instances = content_field_instance_read(array('field_name' => $field['field_name']));
 352  
 353    if ($prev_field['db_storage'] == CONTENT_DB_STORAGE_PER_CONTENT_TYPE && count($prior_instances) > 1) {
 354      // Update the field's data storage.
 355      $field['db_storage'] = CONTENT_DB_STORAGE_PER_FIELD;
 356  
 357      // Update the schema for prior instances to adapt to the change in db storage.
 358      foreach ($prior_instances as $instance) {
 359        if ($instance['type_name'] != $field['type_name']) {
 360          $new_instance = $instance;
 361          $new_instance['db_storage'] = CONTENT_DB_STORAGE_PER_FIELD;
 362  
 363          // Invoke hook_content_fieldapi().
 364          module_invoke_all('content_fieldapi', 'update instance', $new_instance);
 365  
 366          content_alter_schema($instance, $new_instance);
 367        }
 368      }
 369    }
 370  
 371    // Invoke hook_content_fieldapi().
 372    module_invoke_all('content_fieldapi', 'update instance', $field);
 373  
 374    // Update the field and the instance with the latest values.
 375    _content_field_write($field, 'update');
 376    _content_field_instance_write($field, 'update');
 377  
 378    content_alter_schema($prev_field, $field);
 379  
 380    if ($rebuild) {
 381      content_clear_type_cache(TRUE);
 382  
 383      // The label is in the menu tree, so we need a menu rebuild
 384      // if the label changes.
 385      if ($prev_field['widget']['label'] != $field['widget']['label']) {
 386        menu_rebuild();
 387      }
 388    }
 389  
 390    return $field;
 391  }
 392  
 393  /**
 394   * Write a field record.
 395   *
 396   * @param $field
 397   *   The field array to process.
 398   */
 399  function _content_field_write($field, $op = 'update') {
 400    // Rearrange the data to create the global_settings array.
 401    $field['global_settings'] = array();
 402    $setting_names = (array) module_invoke($field['module'], 'field_settings', 'save', $field);
 403    drupal_alter('field_settings', $setting_names, 'save', $field);
 404  
 405    foreach ($setting_names as $setting) {
 406      // Unlike _content_field_instance_write() and 'widget_settings', 'global_settings'
 407      // is never preexisting, so we take no particular precautions here.
 408      $field['global_settings'][$setting] = isset($field[$setting]) ? $field[$setting] : '';
 409      unset($field[$setting]);
 410    }
 411    // 'columns' is a reserved word in MySQL4, so our column is named 'db_columns'.
 412    $field['db_columns'] = $field['columns'];
 413  
 414    switch ($op) {
 415      case 'create':
 416        drupal_write_record(content_field_tablename(), $field);
 417        break;
 418      case 'update':
 419        drupal_write_record(content_field_tablename(), $field, 'field_name');
 420        break;
 421    }
 422    unset($field['db_columns']);
 423    return $field;
 424  }
 425  
 426  /**
 427   * Write a field instance record.
 428   *
 429   * @param $field
 430   *   The field array to process.
 431   */
 432  function _content_field_instance_write($field, $op = 'update') {
 433    // Collapse the field => widget format, so that the values to be saved by
 434    // drupal_write_record are on top-level.
 435    $field = content_field_instance_collapse($field);
 436  
 437    // Rearrange the data to create the widget_settings array.
 438    $setting_names = (array) module_invoke($field['widget_module'], 'widget_settings', 'save', $field);
 439    drupal_alter('widget_settings', $setting_names, 'save', $field);
 440    foreach ($setting_names as $setting) {
 441      // In some cases (when the updated $field was originally read from
 442      // the db, as opposed to gathered from the values of a form), the values
 443      // are already in the right place, we take care to not wipe them.
 444      if (isset($field[$setting])) {
 445        $field['widget_settings'][$setting] = $field[$setting];
 446        unset($field[$setting]);
 447      }
 448    }
 449  
 450    switch ($op) {
 451      case 'create':
 452        drupal_write_record(content_instance_tablename(), $field);
 453        break;
 454      case 'update':
 455        drupal_write_record(content_instance_tablename(), $field, array('field_name', 'type_name'));
 456        break;
 457    }
 458    return $field;
 459  }
 460  
 461  /**
 462   * Load a field instance.
 463   *
 464   * @param $param
 465   *   An array of properties to use in selecting a field instance. Valid keys:
 466   *   - 'type_name' - The name of the content type in which the instance exists.
 467   *   - 'field_name' - The name of the field whose instance is to be loaded.
 468   *   if NULL, all instances will be returned.
 469   * @param $include_inactive
 470   *   TRUE will return field instances that are 'inactive', because their field
 471   *   module or widget module is currently disabled.
 472   * @return
 473   *   The field arrays.
 474   */
 475  function content_field_instance_read($param = NULL, $include_inactive = FALSE) {
 476    $cond = array();
 477    $args = array();
 478    if (is_array($param)) {
 479      // Turn the conditions into a query.
 480      foreach ($param as $key => $value) {
 481        $cond[] = 'nfi.'. db_escape_string($key) ." = '%s'";
 482        $args[] = $value;
 483      }
 484    }
 485    if (!$include_inactive) {
 486      $cond[] = 'nf.active = 1';
 487      $cond[] = 'nfi.widget_active = 1';
 488    }
 489    $where = $cond ? ' WHERE '. implode(' AND ', $cond) : '';
 490  
 491    $db_result = db_query("SELECT * FROM {". content_instance_tablename() ."} nfi ".
 492      " JOIN {". content_field_tablename() ."} nf ON nfi.field_name = nf.field_name ".
 493      "$where ORDER BY nfi.weight ASC, nfi.label ASC", $args);
 494  
 495    $fields = array();
 496    while ($instance = db_fetch_array($db_result)) {
 497      // Unserialize arrays.
 498      foreach (array('widget_settings', 'display_settings', 'global_settings', 'db_columns') as $key) {
 499        $instance[$key] = (!empty($instance[$key])) ? (array) unserialize($instance[$key]) : array();
 500      }
 501      // 'columns' is a reserved word in MySQL4, so our column is named 'db_columns'.
 502      $instance['columns'] = $instance['db_columns'];
 503      unset($instance['db_columns']);
 504  
 505      // Unfold 'global_settings'.
 506      foreach ($instance['global_settings'] as $key => $value) {
 507        $instance[$key] = $value;
 508      }
 509      unset($instance['global_settings']);
 510  
 511      // Put the field in the $field => 'widget' structure that is used
 512      // all around content.module.
 513      $field = content_field_instance_expand($instance);
 514  
 515      // Invoke hook_content_fieldapi().
 516      module_invoke_all('content_fieldapi', 'read instance', $field);
 517      $fields[] = $field;
 518    }
 519    return $fields;
 520  }
 521  
 522  /**
 523   * Delete an existing field instance.
 524   *
 525   * @param $field_name
 526   *   The field name to delete.
 527   * @param $type_name
 528   *   The content type where the field instance is going to be deleted.
 529   * @param $rebuild
 530   *   TRUE to clear content type caches and rebuild menu (default).
 531   *   FALSE allows the caller to process several fields at a time quickly, but then
 532   *   the caller is reponsible to clear content type caches and rebuild menu as soon
 533   *   as all fields have been processed. For example:
 534   *   @code
 535   *     // Delete several fields at a time.
 536   *     foreach ($fields as $field) {
 537   *       content_field_instance_delete($field['field_name'], $type_name, FALSE);
 538   *     }
 539   *     // Clear caches and rebuild menu.
 540   *     content_clear_type_cache(TRUE);
 541   *     menu_rebuild();
 542   *   @endcode
 543   * @see content_clear_type_cache()
 544   * @see menu_rebuild()
 545   */
 546  function content_field_instance_delete($field_name, $type_name, $rebuild = TRUE) {
 547    include_once('./'. drupal_get_path('module', 'content') .'/includes/content.admin.inc');
 548  
 549    // Get the previous field value.
 550    $field = array_pop(content_field_instance_read(array('field_name' => $field_name, 'type_name' => $type_name)));
 551  
 552    // Invoke hook_content_fieldapi().
 553    module_invoke_all('content_fieldapi', 'delete instance', $field);
 554  
 555    db_query("DELETE FROM {". content_instance_tablename() .
 556      "} WHERE field_name = '%s' AND type_name = '%s'", $field['field_name'], $field['type_name']);
 557  
 558    // If no instances remain, delete the field entirely.
 559    $instances = content_field_instance_read(array('field_name' => $field_name));
 560    if (sizeof($instances) < 1) {
 561      db_query("DELETE FROM {". content_field_tablename() ."} WHERE field_name = '%s'", $field['field_name']);
 562      content_alter_schema($field, array());
 563    }
 564    // If only one instance remains, we may need to change the database
 565    // representation for this field.
 566    elseif (sizeof($instances) == 1 && !($field['multiple'])) {
 567      // Multiple-valued fields are always stored per-field-type.
 568      $instance = $instances[0];
 569      $new_instance = $instance;
 570      $new_instance['db_storage'] = CONTENT_DB_STORAGE_PER_CONTENT_TYPE;
 571      _content_field_write($new_instance, 'update');
 572  
 573      content_alter_schema($instance, $new_instance);
 574    }
 575  
 576    // If the deleted instance was the last field for the content type,
 577    // we drop the per-type table. We also consider possibly inactive fields.
 578    if (!content_field_instance_read(array('type_name' => $field['type_name']), TRUE)) {
 579      $base_tablename = _content_tablename($field['type_name'], CONTENT_DB_STORAGE_PER_CONTENT_TYPE);
 580      if (db_table_exists($base_tablename)) {
 581        db_drop_table($ret, $base_tablename);
 582      }
 583    }
 584  
 585    if ($rebuild) {
 586      content_clear_type_cache(TRUE);
 587      menu_rebuild();
 588    }
 589  
 590    return $field;
 591  }
 592  
 593  /**
 594   * Delete all data related to a module.
 595   *
 596   * @param string $module
 597   */
 598  function content_module_delete($module) {
 599    // Delete the field data.
 600    // If content module has been uninstalled first, all tables
 601    // have already been dropped, and running that code will raise errors.
 602    if (db_table_exists(content_instance_tablename())) {
 603      $results = db_query("SELECT field_name, type_name FROM {". content_instance_tablename() ."} WHERE widget_module = '%s'", $module);
 604      while ($field = db_fetch_array($results)) {
 605        content_field_instance_delete($field['field_name'], $field['type_name'], FALSE);
 606      }
 607      // Force the caches and static arrays to update to the new info.
 608      content_clear_type_cache(TRUE);
 609      menu_rebuild();
 610    }
 611  }
 612  
 613  /**
 614   * Make changes needed when a content type is created.
 615   *
 616   * @param $info
 617   *   value supplied by hook_node_type()
 618   *
 619   * node_get_types() is still missing the new type at this point due to
 620   * a static caching bug. We ask it to rebuild its cache so that
 621   * content_clear_type_cache() can do its job properly.
 622   */
 623  function content_type_create($info) {
 624    node_get_types(NULL, NULL, TRUE);
 625    content_clear_type_cache(TRUE);
 626  }
 627  
 628  /**
 629   * Make changes needed when an existing content type is updated.
 630   *
 631   * @param $info
 632   *   value supplied by hook_node_type()
 633   */
 634  function content_type_update($info) {
 635    if (!empty($info->old_type) && $info->old_type != $info->type) {
 636      // Rename the content type in all fields that use changed content type.
 637      db_query("UPDATE {". content_instance_tablename() ."} SET type_name='%s' WHERE type_name='%s'", array($info->type, $info->old_type));
 638  
 639      // Rename the content fields table to match new content type name.
 640      $old_type = content_types($info->old_type);
 641      $old_name = _content_tablename($old_type['type'], CONTENT_DB_STORAGE_PER_CONTENT_TYPE);
 642      $new_name = _content_tablename($info->type, CONTENT_DB_STORAGE_PER_CONTENT_TYPE);
 643      if (db_table_exists($old_name)) {
 644        $ret = array();
 645        db_rename_table($ret, $old_name, $new_name);
 646        watchdog('content', 'Content fields table %old_name has been renamed to %new_name and field instances have been updated.', array(
 647          '%old_name' => $old_name, '%new_name' => $new_name));
 648      }
 649  
 650      // Rename the variable storing weights for non-CCK fields.
 651      if ($extra = variable_get('content_extra_weights_'. $info->old_type, array())) {
 652        variable_set('content_extra_weights_'. $info->type, $extra);
 653        variable_del('content_extra_weights_'. $info->old_type);
 654      }
 655    }
 656  
 657    // Reset all content type info.
 658    // Menu needs to be rebuilt as well, but node types need to be rebuilt first.
 659    // node_type_form_submit() takes care of this.
 660    content_clear_type_cache(TRUE);
 661  }
 662  
 663  /**
 664   * Make changes needed when a content type is deleted.
 665   *
 666   * @param $info
 667   *   value supplied by hook_node_type()
 668   *
 669   * TODO should we skip doing this entirely since core leaves the
 670   * nodes in the database as orphans and wait until the nodes are
 671   * deleted to respond?
 672   *
 673   */
 674  function content_type_delete($info) {
 675    // Don't delete data for content-types defined by disabled modules.
 676    if (!empty($info->disabled)) {
 677      return;
 678    }
 679  
 680    // TODO : What about inactive fields ?
 681    // Currently, content_field_instance_delete doesn't work on those...
 682    $fields = content_field_instance_read(array('type_name' => $info->type));
 683    foreach ($fields as $field) {
 684      content_field_instance_delete($field['field_name'], $info->type, FALSE);
 685    }
 686    $table = _content_tablename($info->type, CONTENT_DB_STORAGE_PER_CONTENT_TYPE);
 687    if (db_table_exists($table)) {
 688      $ret = array();
 689      db_drop_table($ret, $table);
 690      watchdog('content', 'The content fields table %name has been deleted.', array('%name' => $table));
 691    }
 692    // Menu needs to be rebuilt as well, but node types need to be rebuilt first.
 693    // node_type_form_submit() takes care of this.
 694    content_clear_type_cache(TRUE);
 695  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7