This behaviour occurs when a file field is attached to a node, and also with a managed_file element as part of a custom form (with the default file_managed_file_value value callback).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quicksketch’s picture

Could you describe what you mean by "UUID not added"? I think what you mean is that the UUID is not accessible via the return values of a manage_file element. All you're given is the FID. However I think if you need the UUID, you can still obtain it by loading the entity for the file and then checking $file->uuid. I definitely see where you're coming from that I would expect file_managed to give you UUIDs instead of FIDs, but given the FID you should be able to get the UUID even though it's not provided directly.

nirbhasa’s picture

I meant that the uuid value is NULL in the file_managed table after the file has been uploaded.

quicksketch’s picture

Oh. Dang well that's a big problem. :)

I'm looking into #1932652: Add image uploading to WYSIWYGs through editor.module right now (where this problem was discovered). I'll get into providing some actually useful code shortly.

quicksketch’s picture

Well, this problem is beyond my skill level. I have no idea why the UUID isn't getting created. file_save_upload() creates the original $file variable with entity_create() just like everywhere else we have new entities, but for some reason the UUID isn't getting generated (apparently). Since you can not currently dpm() or debug() any entity variables, I can't tell what's going on in here. Such is D8. :P

Berdir’s picture

FileStorageController:baseFieldDefinitions() defines uuid as a string_field. That must be uuid_field, as that references the UuidItem class, which implements applyDefault(), which is the place where the default UUID is created.

The issue that changed this went in more or less in parallel with file ng conversion, that's why we missed it. Looks like we need test coverage there, shouldn't be too complicated.

@quicksketch: try debug($entity->getPropertyValues()) (weird name, I know). Also, PHP 5.4 is much better at that, it throws out some warnings, but doesn't go into an endless loop anymore.

quicksketch’s picture

Thanks @Berdir, you're spot-on. It's a one-line change to fix this problem. Tests should be trivial to write too.

Berdir’s picture

As mentioned to @larowlan yesterday, we really need to update the UUID change notice, still mentiones hook_entity_info(), and we should document the differences between config entities and content/NG entities: https://drupal.org/node/1721572

quicksketch’s picture

Status: Active » Needs review
FileSize
1.81 KB

Here's a patch which fixes the problem and provides new tests, but for some reason the new test isn't passing on my local. We'll see if testbot thinks differently.

Status: Needs review » Needs work

The last submitted patch, drupal8_missing_file_uuid-2046809.patch, failed testing.

quicksketch’s picture

We must have a bigger problem here. This trivial bit of code fails:

    $file = $this->createFile('druplicon.txt', NULL, 'public');
    $by_uuid_file = entity_load_by_uuid('file', $file->uuid());

Seems like entity_load_by_uuid() doesn't work within the same request for some reason. The UUID is definitely now getting created, but we can't load an entity by it. Very strange.

larowlan’s picture

Status: Needs work » Needs review
FileSize
1.93 KB
758 bytes

$this->createFile writes the record to file_managed direct, so it doesn't go through the normal hooks and the uuid isn't autocreated.
Added a $file->save() to trigger the full stack/write.
And then a file_test_reset(); to reset the load hooks.
Works for me locally™

tim.plunkett’s picture

Two insignificant nitpicks, but when you repost can you provide a test-only patch so we can see it fail? Thanks!

+++ b/core/modules/file/lib/Drupal/file/Tests/LoadTest.phpundefined
@@ -88,4 +88,22 @@ function testMultiple() {
+   * Load a single file and ensure that the correct values are returned.

Loads

+++ b/core/modules/file/lib/Drupal/file/Tests/LoadTest.phpundefined
@@ -88,4 +88,22 @@ function testMultiple() {
+    }
+  }

Can you add a blank line after the end of the method please?

larowlan’s picture

larowlan’s picture

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

If #14 is red+green, RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 337b370 and pushed to 8.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.