function content_field in Content Construction Kit (CCK) 5
Same name and namespace in other branches
- 6.3 content.module \content_field()
- 6 content.module \content_field()
- 6.2 content.module \content_field()
Implementation of hook_field(). Handles common field housekeeping.
This implementation is special, as content.module does not define any field types. Instead, this function gets called after the type-specific hook, and takes care of the database interface for field types that do not choose to do their own storage.
1 call to content_field()
- _content_field_invoke_default in ./
content.module - Invoke content.module's version of a field hook.
File
- ./
content.module, line 383 - Allows administrators to associate custom fields to content types.
Code
function content_field($op, &$node, $field, &$node_field, $teaser, $page) {
$db_info = content_database_info($field);
switch ($op) {
case 'load':
$column_names = array();
foreach ($db_info['columns'] as $column => $attributes) {
$column_names[] = $attributes['column'] . ' AS ' . $column;
}
$query = 'SELECT ' . implode(', ', $column_names) . ' FROM {' . $db_info['table'] . '} WHERE vid = %d';
if ($field['multiple']) {
$result = db_query($query . ' ORDER BY delta', $node->vid);
}
else {
$result = db_query_range($query, $node->vid, 0, 1);
}
$additions = array();
while ($value = db_fetch_array($result)) {
$additions[$field['field_name']][] = $value;
}
return $additions;
case 'insert':
foreach ($node_field as $delta => $item) {
$data = array();
$column_names = array();
$column_placeholders = array();
$column_assignments = array();
foreach ($db_info['columns'] as $column => $attributes) {
$column_names[] = $attributes['column'];
if ($item[$column] == '' && !$attributes['not null'] && !$field['required']) {
$column_placeholders[] = '%s';
$column_assignments[] = $attributes['column'] . ' = %s';
$item[$column] = 'NULL';
}
else {
switch ($attributes['type']) {
case 'int':
case 'mediumint':
case 'tinyint':
case 'bigint':
$column_placeholders[] = '%d';
$column_assignments[] = $attributes['column'] . ' = %d';
break;
case 'float':
$column_placeholders[] = '%f';
$column_assignments[] = $attributes['column'] . ' = %f';
break;
default:
$column_placeholders[] = "'%s'";
$column_assignments[] = $attributes['column'] . " = '%s'";
}
}
$data[] = $item[$column];
}
$data[] = $node->vid;
$data[] = $node->nid;
if ($field['multiple']) {
$data[] = $delta;
}
if ($field['multiple']) {
db_query('INSERT INTO {' . $db_info['table'] . '} (' . implode(', ', $column_names) . ', vid, nid, delta) VALUES (' . implode(', ', $column_placeholders) . ', %d, %d, %d)', $data);
}
else {
if (db_result(db_query('SELECT COUNT(*) FROM {' . $db_info['table'] . '} WHERE vid = %d AND nid = %d', $node->vid, $node->nid))) {
db_query('UPDATE {' . $db_info['table'] . '} SET ' . implode(', ', $column_assignments) . ' WHERE vid = %d AND nid = %d', $data);
}
else {
db_query('INSERT INTO {' . $db_info['table'] . '} (' . implode(', ', $column_names) . ', vid, nid) VALUES (' . implode(', ', $column_placeholders) . ', %d, %d)', $data);
}
}
}
return;
case 'update':
if ($field['multiple']) {
// Delete and insert, rather than update, in case a field was added.
db_query('DELETE FROM {' . $db_info['table'] . '} WHERE vid = %d', $node->vid);
}
foreach ($node_field as $delta => $item) {
$data = array();
$column_names = array();
$column_placeholders = array();
$column_assignments = array();
foreach ($db_info['columns'] as $column => $attributes) {
$column_names[] = $attributes['column'];
if ($item[$column] == '' && !$attributes['not null'] && !$field['required']) {
$column_placeholders[] = '%s';
$column_assignments[] = $attributes['column'] . ' = %s';
$item[$column] = 'NULL';
}
else {
switch ($attributes['type']) {
case 'int':
case 'mediumint':
case 'tinyint':
case 'bigint':
$column_placeholders[] = '%d';
$column_assignments[] = $attributes['column'] . ' = %d';
break;
case 'float':
$column_placeholders[] = '%f';
$column_assignments[] = $attributes['column'] . ' = %f';
break;
default:
$column_placeholders[] = "'%s'";
$column_assignments[] = $attributes['column'] . " = '%s'";
}
}
$data[] = $item[$column];
}
$data[] = $node->vid;
$data[] = $node->nid;
if ($field['multiple']) {
$data[] = $delta;
}
if ($field['multiple']) {
db_query('INSERT INTO {' . $db_info['table'] . '} (' . implode(', ', $column_names) . ', vid, nid, delta) VALUES (' . implode(', ', $column_placeholders) . ', %d, %d, %d)', $data);
}
else {
if (db_result(db_query('SELECT COUNT(*) FROM {' . $db_info['table'] . '} WHERE vid = %d AND nid = %d', $node->vid, $node->nid))) {
db_query('UPDATE {' . $db_info['table'] . '} SET ' . implode(', ', $column_assignments) . ' WHERE vid = %d AND nid = %d', $data);
}
else {
db_query('INSERT INTO {' . $db_info['table'] . '} (' . implode(', ', $column_names) . ', vid, nid) VALUES (' . implode(', ', $column_placeholders) . ', %d, %d)', $data);
}
}
}
return;
case 'delete':
// Delete using nid rather than vid to purge all revisions.
db_query('DELETE FROM {' . $db_info['table'] . '} WHERE nid = %d', $node->nid);
return;
case 'delete revision':
db_query('DELETE FROM {' . $db_info['table'] . '} WHERE vid = %d', $node->vid);
return;
}
}