Commit ab6a2015 authored by quicksketch's avatar quicksketch

#417774: Add Public FileField API Functions and fix field_file_references() to...

#417774: Add Public FileField API Functions and fix field_file_references() to not be field specific.
parent fb9dd5b8
......@@ -370,33 +370,14 @@ function field_file_strip_path($path) {
return $path;
}
/**
* Return all the references to a file by a single field.
* Return a count of the references to a file by all modules.
*/
function field_file_references($file, $field) {
$db_info = content_database_info($field);
$references = db_result(db_query(
'SELECT count('. $db_info['columns']['fid']['column'] .')
FROM {'. $db_info['table'] .'}
WHERE '. $db_info['columns']['fid']['column'] .' = %d', $file->fid
));
// If a field_name is present in the file object, the file is being deleted
// from this field.
if (isset($file->field_name) && $field['field_name'] == $file->field_name) {
// If deleting the entire node, count how many references to decrement.
if (isset($file->delete_nid)) {
$node_references = db_result(db_query(
'SELECT count('. $db_info['columns']['fid']['column'] .')
FROM {'. $db_info['table'] .'}
WHERE '. $db_info['columns']['fid']['column'] .' = %d AND nid = %d', $file->fid, $file->delete_nid
));
$references = $references - $node_references;
}
else {
$references = $references - 1;
}
function field_file_references($file) {
$references = (array) module_invoke_all('file_references', $file);
$reference_count = 0;
foreach ($references as $module => $count) {
$reference_count += $count;
}
return $references;
return $reference_count;
}
......@@ -530,7 +530,7 @@ function filefield_file_references($file) {
if ($field['type'] != 'filefield') {
continue;
}
$references += field_file_references($file, $field);
$references += filefield_get_file_reference_count($file, $field);
}
return array('filefield' => $references);
}
......@@ -745,3 +745,44 @@ function filefield_validate_associate_field(&$file, $field) {
$file->field = $field;
return array();
}
/*******************************************************************************
* Public API functions for FileField.
******************************************************************************/
/**
* Count the number of times the file is referenced within a field.
*
* @param $file
* A file object.
* @param $field
* The CCK field array.
* @return
* An integer value.
*/
function filefield_get_file_reference_count($file, $field) {
$db_info = content_database_info($field);
$references = db_result(db_query(
'SELECT count('. $db_info['columns']['fid']['column'] .')
FROM {'. $db_info['table'] .'}
WHERE '. $db_info['columns']['fid']['column'] .' = %d', $file->fid
));
// If a field_name is present in the file object, the file is being deleted
// from this field.
if (isset($file->field_name) && $field['field_name'] == $file->field_name) {
// If deleting the entire node, count how many references to decrement.
if (isset($file->delete_nid)) {
$node_references = db_result(db_query(
'SELECT count('. $db_info['columns']['fid']['column'] .')
FROM {'. $db_info['table'] .'}
WHERE '. $db_info['columns']['fid']['column'] .' = %d AND nid = %d', $file->fid, $file->delete_nid
));
$references = $references - $node_references;
}
else {
$references = $references - 1;
}
}
return $references;
}
......@@ -338,9 +338,13 @@ function filefield_widget_validate(&$element, &$form_state) {
$field = content_fields($element['#field_name'], $element['#type_name']);
if ($file = field_file_load($element['fid']['#value'])) {
$file = (object) $file;
// TODO: Currently fields can only reference files by the same field.
if ($file->status == FILE_STATUS_PERMANENT && field_file_references($file, $field) == 0) {
form_error($element, t('Referencing to the file used the %field field is not allowed.', array('%field' => $element['#title'])));
if ($file->status == FILE_STATUS_PERMANENT) {
// TODO: We could use field_file_references() here to reference any file
// but hook_file_delete() needs to be implemented first.
$references = module_invoke('filefield', 'file_references', $file);
if ($references['filefield'] == 0) {
form_error($element, t('Referencing to the file used the %field field is not allowed.', array('%field' => $element['#title'])));
}
}
}
else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment