Working on #322287: Add file.module to provide UI for managing files it because apparent that it'd be really useful to see the actual context where modules are using a file. Knowing that imagefield is using a file is helpful but it's much more helpful to know that it's being used on node/1025.
Sometimes having path is overkill, e.g. during deletion you really just want a "yes, someone is using this file" / "no, it's unused" answer, so it would seem beneficial to have two modes: boolean and menu paths.
This would complicate the calling of the hook so it seems that it would be helpful to add a new file_usage() function that would be responsible for calling hook_file_references(). The upside is that we could fix one annoyance in hook_file_references(). Currently the module implementing hook_file_references() is responsible for keying it's references with the module name. If there's a function for checking usage then it can replicate some of the module_invoke_all code and key the results by module leaving the modules to just return their usage as either a number or array of links.
I'm thinking of doing something along the lines of this--totally untested--bit of code:
/**
* Determine if a file is in use.
*
* @param $file
* A file object.
* @param $mode
* The type of value to return. Allowed values are: 'boolean', 'path'.
* @return
* Mixed depending on the value of $mode.
* - 'boolean' returns a simple yes/no answer.
* - 'path' returns an array of paths to instances where the file is used.
*/
function file_usage($file, $mode = 'path') {
$file = (object)$file;
$return = array();
foreach (module_implements('file_references') as $name) {
$function = $name . '_' . $hook;
$result = $function($file, $mode);
if (isset($result)) {
$return[$name] = $result;
}
}
return $return;
}
Code that just wants the yes/no like file_delete() would call file_usage($file, 'boolean');
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | file_329311.patch | 21.94 KB | drewish |
| #3 | file_329311.patch | 17.11 KB | drewish |
| #1 | file_329311.patch | 2.81 KB | drewish |
Comments
Comment #1
drewish commentedthe more i mess with it the less i'm convinced that there should be two modes. file_delete() is the only case that would just need a yes/no answer but it turns around and passes the results back to the caller if the file is in use... delete's happen infrequently so if we spend a little longer figuring out where the files are used that's not a huge burden.
the attached returns an array of arrays like:
this gives you something to display for a list of links to usages.
Comment #2
drewish commentedafter talking with earl i think that a better approach is forcing modules to store their usage in a table. that way views can query it and we can easily delete multiple files without having to continually call the hook.
Comment #3
drewish commentedi should probably just close this issue and open a new one but since it's here i'll post this work in progress to.
Comment #4
drewish commentednow with unit tests.
Comment #5
drewish commentedclosing this in favor of #353458: hook_file_references() was not designed for a highly flexible field storage