There appears to be some wierdness with file usage, or once you've uploaded a file or image to a multifield, you can't re-save the same node again because it won't let you 'use' that image due to validation failing in file_managed_file_validate().

Comments

dave reid’s picture

Category: bug » task
dave reid’s picture

Assigned: Unassigned » dave reid
Issue summary: View changes

Taking this one at for the next release.

dave reid’s picture

juampynr’s picture

Looks like we need to add a call to file_usage_add($file, 'multifield', 'node', $node->nid) when a file is saved. I am debugging the module to see where is the best place to add this.

juampynr’s picture

Assigned: dave reid » Unassigned
Category: Task » Bug report
Priority: Major » Critical
Status: Active » Needs review
StatusFileSize
new3.21 KB

The following patch adds file usage statistics for multifield files.

I have updated the issue metadata because this bug makes it impossible to re-save entities containing files.

juampynr’s picture

StatusFileSize
new2.9 KB

Here is an improved version of the patch thanks to feedback given by @q0rban.

badrange’s picture

Status: Needs review » Needs work

I used this patch together with a patch from #2327317: Integrity constraint violation when saving multifield with a file subfield and I still get the same error. Without those patches I get a SQL error when trying to edit a page with a multifield. With the patches I can add an image and save the page with no sql errors, but not edit the page after uploading images.

File usage page shows that the files are used 0 times.

dave reid’s picture

I want to work on this a bit more, there are some issues with the patch.

+++ b/multifield.field.inc
@@ -359,6 +360,55 @@ function _multifield_field_attach_update($entity_type, $entity) {
+            $original_field = $original_entity->{$field['field_name']}[$entity->language][$delta][$file_field_name];

This line here assumes that this structure exists, when it in fact may not (for example, if we're adding a record, this will cause a notice since the new larger $delta does not exist on the original object).

Overall, this doesn't seem to cover the logic in hook_field_insert() and hook_field_delete() as well. I'm working on some tests around file usage in #2327317: Integrity constraint violation when saving multifield with a file subfield that can easily be extended to test support for usage tracking on the parent entity.

But I think the biggest problem is that we've seen this pop up with multiple field types. For example, with taxonomy terms: #2390233: Taxonomy terms in multifields on nodes don't update taxonomy_index table. I'm wondering if maybe there could be a more general solution that somehow takes a stub version of the parent entity, puts the subfield values onto it, and runs hook_field_insert|update|delete where needed, which should account for file usage tracking and taxonomy index tracking for the parent entity.