Due the lack of the possibility to define hook_field in other modules then the cck field itself, it is great that file field is already exporting its own hooks so we can react on saving / inserting the fields.
The attached patch is not changing the current behavior but adds a really importat thing: The node where the operation is currently run on. This is needed if you dont only modify the file object but maybe save data related to the node etc.
I think this is kind of trivial but very effective. This way i can integrate private files for filefield for all widgets (imagefield, swfupload .. ) at the same time, without creating a new custom + not needed widget.
Thanks
Comments
Comment #1
eugenmayer commentedI realized that even that wont fit all needs. We also need to pass $field to the hook. Otherwise you simply dont know what settings the field has - you dont even know which field it is. I think this is also a very critical argument.
Added this to the patch - still wont affect any default behavior
Comment #2
eugenmayer commentedFixing title
Comment #3
dawehnerThis is a non critical feature request. Critical is for me by definition, something like a WSOD. Sorry to be that strict ;)
Comment #4
eugenmayer commentedFixing category, fixing code style
Comment #5
eugenmayer commentedconcurent saving :)
Comment #6
dawehnerIf someone called filefield_file_save in his custom module this results to an error. This patch corrects this.
Comment #7
eugenmayer commentedGood catch Daniel, forgot about that this is API and not a private method. but i think field should be rather array() then?
Comment #8
eugenmayer commentedThis should finalize it
Comment #9
dawehnerThis makes sense.
Comment #10
quicksketchThese API hooks should not have $field and $node appended to them. They imitate hook_file_update() and other hooks (like hook_file_delete()) available in Drupal 7.
Comment #11
eugenmayer commentedWell for D7 those hooks are not needed at all, but what about Drupal 6? In Drupal 7 you can also define hook_field in modules which are not defining that field, but in Drupal 6 thats not possible.
Comment #12
quicksketchThe Drupal 7 File API hooks *are* the FileField hooks, just moved into core. It was nearly a direct port when the hooks were first proposed (they're slightly different now, but still pretty close). The APIs provided by FileField are intended to be generic and not related to CCK at all, for example a file could be uploaded through IMCE and call the same hooks. In which case, there wouldn't be a $field variable at all.
Comment #13
eugenmayer commentedNo its not the same.
You _cannot_ define hook_field in D6 from outer modules. This is confirmed.
And you _cannot_ define hook_file_update from outer modules. well this is clear anyway (as the fieldtype is unique).
So there is absolutely no other way in D6 catching that hook.
Comment #14
quicksketchhook_field() has nothing to do with hook_file_*(). They are separate APIs. The File hooks shouldn't be assuming use of nodes or fields.
Comment #15
eugenmayer commentedWell but the file and node are in a relation and you want simply lose that information? It is also passed as "entity" in hook_file_field so its surely an important information.
Lets say you want to do something with the file uploaded AND assigend to a node (by a filefield), but you also need the relation to its parent. there is no way doing so without:
- hacking filefield
- using nodeapi and filtering the whole node for fields .. what is really bad.
I see your point, as in D7 fields are not longer node-specific, but can be attached to any entity. But this makes this information even more important, otherwise you now would have to implement hook_nodeapi, then one for comment, user .. and so forth. So even with the perspective to drupal 7 ( eventhough its 6.3.3 and not 7.x. ) i think thats a useful addition. I think with the D7 scope the implementation of the hook would more look like
This way i can implement private files support for every subwidgets of filefield. Yes i could create a widget "filefield_private", but that would mean imagefield_private would need to be created, and swfupload_private and and and. Cant you see the usecase?
_drupalwiki_privatefile_to_private needs the relation to the parent, as it needs to now "who is the parent i check access for later".
Comment #16
miro_dietikerI absolutely see the need of the full context here.
If there's any reason to pass the $node then $field is also required as those information are needed for the same reason.
Comment #17
quicksketchFileField is tied to node and the field system this is true. But the File API that FileField includes is completely separate from both nodes and fields. It's essentially a precursor to the File API that we have in Drupal 7. There is absolutely no need (in Drupal 6 or Drupal 7) for files to be tied to anything (other than the user that uploaded it, since that's stored in the files table). A module that is a general purpose file manager like IMCE could manage files completely separate of nodes or fields. This change is adding additional information in the wrong places.
Comment #18
eugenmayer commentedWell i just dont see how your statement is taking related to this API change. what you state in #17 is, that there are cases, where files are on their own - not tight to anything. Well in that case, entity is null and those systems which rely on a parent to work will skip this file, e.g. private-files ( as permissions cant be applied ).
So i see your point that there are cases where its not needed but i think you can undestand that there are even more cases ( general ) where it makes a lot of use.
And as providing that API does not harm your case, but not providing it makes the case i described horrible i would please you to let this feature go in.
Thanks
Comment #19
quicksketchMy question for this change is since it's not in Drupal 7, what makes this same change unnecessary there? It seems that whatever system is in place in Drupal 7's field system should be applied to CCK in Drupal 6, making this tying of the File API to nodes and fields unnecessary.
Comment #20
eugenmayer commentednot sure i get your question. as hook_field is gone in D7 i think there is the same issue for both now, you cant hook into update to get entity + file from outer modules.
So you will need this for both.
Comment #21
quicksketchThis comment led me to believe that this change was something that was only needed in Drupal 6:
My feeling is still that adding $node and $field should not be mingled into hook_file_update() or other file-based hooks like delete or insert.
If you'd like to latch onto data when a file is saved through FileField, I'd suggest you implement hook_elements() and add additional #process or #filefield_value_callback properties. These should allow you to capture when files have been inserted. Then you can use hook_file_update() to check when a file's status has been set to permanent or is deleted to manage the records in your own tables.
Comment #22
eugenmayer commentedAs explained, this would then need a implementation for every widget type.
And now its not mingeling, hook_file_update is only able to be implemented once - and will be implemented by the field author in his module. so you never can use it. And most file-based hooks _have_ no context, as they only handle "the file" no matter where it is attached.
Filefield is exactly the connection between node and file and i have no idea why you make it that hard implementing those to arguments which are evidently.
You search and search for alternatives which are not even close to be that handy. If you simply dont want to take this feature in - dont do it. Last comment on my side.
Comment #23
quicksketchI still believe the separation between files and nodes is very intentional. I'm open to improving the FileField APIs, but only in ways that accommodate for Drupal 7's functionality. We can't put features into FileField that have no equivalent in Drupal 7, since D7 is frozen and will leave users stranded without an upgrade path. If additional functionality is needed, it will need to be accommodated for in an add-on module, despite the inconveniences of our APIs.
I should note that in Drupal 7, most fields have been broken out into separate types again (similar to Drupal 5), so ImageField is no longer a type of "file" for example. This means that support will need to be implemented per-field in addition to per-widget in Drupal 7, since the "everything is a file field" approach has been removed.
Comment #24
eugenmayer commentedWell then i probably make a Drupal 6 fork of filefield to cover fields of D6 as you seem to be focused on D7, which is (for me), not ready to be used in complex productions in 1 our 1,5 years.
I really dont get your D7 focus. D7`s fileapi like differs 200% from d6..you have much better posibilities there. But you go against a feature in D6 which has, well are we even allowed to call it a fileapi? I think its next to the comment module and the taxonomy module one of the darker sides in Drupal. So you just let the D6 in the big black whole because D7 handles things "differenly". And we speak about 2 arguments, not a "code change".
Comment #25
quicksketchAs a person that wrote large portions of both the D6 and D7 file handling, I would say that they're actually much closer than you expect hook-wise. Sure D7 has stream-wrappers, but other than that the hooks that are available are extremely similar, since FileField was the basis for FileAPI in Drupal 7. As such, I can't change the APIs in FileField at this point, as it's too late for the same changes in D7. Not that it makes much difference regarding this change, since it wouldn't be accepted anyway for the reasons that I've stated above (the crossing of two separate systems).