You are here

function content_max_delta in Content Construction Kit (CCK) 6.3

Same name and namespace in other branches
  1. 6.2 content.module \content_max_delta()

Find max delta value actually in use for a field.

Helper function to do things like tell when we should prevent a change in multiple value settings that would result in data loss, or know if content actually exists for a field.

Parameters

$field_name: The field name to examine.

$type_name: If provided, search only for existing data in that type, otherwise search for all instances of field data in all types.

Return value

NULL if field is not in use, or the maximum delta value in use.

TODO Go back to the field settings validation and use this function to prevent (or confirm) changes in multiple values that would destroy data.

Fields with only NULL data will show up as being in use. Do we want to eliminate them from the results?

3 calls to content_max_delta()
content_multigroup_allowed_in in modules/content_multigroup/content_multigroup.admin.inc
Helper function for deciding if a field is allowed into a Multigroup.
content_multigroup_allowed_out in modules/content_multigroup/content_multigroup.admin.inc
Helper function for deciding if a field is allowed out of a Multigroup.
content_multigroup_group_edit_form_validate in modules/content_multigroup/content_multigroup.admin.inc
Validate the Fieldgroup edit form.

File

./content.module, line 2686
Allows administrators to associate custom fields to content types.

Code

function content_max_delta($field_name, $type_name = NULL) {
  $fields = content_fields();
  $field = $fields[$field_name];

  // Non-multiple value fields don't use the delta column,
  // but could exist in multiple databases. If any value
  // exists in any examined table, the max delta will be zero.
  if (empty($field['multiple'])) {
    $content_types = content_types();
    foreach ($content_types as $content_type) {
      if (empty($type_name) || $content_type['type'] == $type_name) {
        foreach ($content_type['fields'] as $field) {
          $db_info = content_database_info($field);
          if (db_result(db_query("SELECT COUNT(*) FROM {" . $db_info['table'] . "}")) >= 1) {
            return 0;
          }
        }
      }
    }
  }
  else {
    $db_info = content_database_info($field);
    if (!empty($type_name)) {
      $delta = db_result(db_query("SELECT MAX(delta) FROM {" . $db_info['table'] . "} f LEFT JOIN {node} n ON f.vid = n.vid WHERE n.type = '%s'", $type_name));
    }
    else {
      $delta = db_result(db_query("SELECT MAX(delta) FROM {" . $db_info['table'] . "}"));
    }
    if ($delta >= 0) {
      return $delta;
    }
  }

  // If we got this far, there is no data for this field.
  return NULL;
}