The @return doc for file_get_file_references() says it returns an integer, but in fact it returns an array of references to a managed file, optionally narrowing that down to a specific field.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

Issue tags: +Novice

Someone must have copied and pasted that @return from another function -- good catch carlos8f! This is probably a good project for a novice patch contributor to fix.

jhodgdon’s picture

It might also be useful to add a bit to the documentation: maybe explaining what a "file reference" is?

Anonymous’s picture

Assigned: Unassigned »

Ill do this one.

Anonymous’s picture

Status: Active » Needs review
FileSize
870 bytes

Here is the patch, tried to keep the description brief.

carlos8f’s picture

Status: Needs review » Needs work

In my opinion, brief documentation is not enough documentation. This is yet another array structure for developers to memorize, so let's be as detailed as possible. That could prevent critical security bugs like #992674: Private file download returns access denied, which resulted from confusion over whether the keys of one level of the array are entity IDs or revision IDs. If passing FIELD_LOAD_CURRENT, they are entity IDs; if FIELD_LOAD_REVISION is passed, they are revision IDs. Here's the spec as I understand it:

$references = file_get_file_references($file, NULL, FIELD_LOAD_CURRENT);
$references = array(
  $field_name => array(
    $entity_type => array(
      $entity_id (nid, 123) => array(
        'nid' => 123,
        'vid' => 124,
        'type' => 'blog',
      ),
    ),
  ),
);
$references = file_get_file_references($file, NULL, FIELD_LOAD_REVISION);
$references = array(
  $field_name => array(
    $entity_type => array(
      $revision_id (vid, 124) => array(
        'nid' => 123,
        'vid' => 124,
        'type' => 'blog',
      ),
    ),
  ),
);
$references = file_get_file_references($file, 'my_field');
$references = array(
  $entity_type => array(
    $revision_id (vid, 124) => array(
      'nid' => 123,
      'vid' => 124,
      'type' => 'blog',
    ),
  ),
);

(Please check my work though, I am doing this from memory)

rfay’s picture

Subscribing.

I'd actually like to improve the docs in this area a bit more. There is not enough explanation of when to use file_usage_add() and file_usage_delete(). This will probably come up a fair bit in D7 as it's a new WTF. In the image_example #992778: Image example: Can't change style after image is loaded. recently came up.

Could we take a look and try to grok the actual file reference thing and get it handled in the handbook and in core code?

jhodgdon’s picture

Status: Needs work » Needs review
Issue tags: -Novice

Sounds like this is not so much of a novice issue any more. :)

carlos8f’s picture

Status: Needs review » Needs work

Needs work :)

neoglez’s picture

Well, i see the situation is still there, also in the dev version, it's funny (actually sad -no ofense) to see that this issue was practically resolved yet it was not aplied to D7.
"It might also be useful to add a bit to the documentation: maybe explaining what a "file reference" is?"
..please, please...;-)

jhodgdon’s picture

neoglez: There is not a patch that has been accepted yet. We don't apply nearly/practically OK patches to Drupal, only patches that are completely OK. If you'd like to help by making a fully acceptable patch, that would be lovely!

rfay’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7

Has to go into D8 first.

pillarsdotnet’s picture

kathyh’s picture

Status: Needs work » Needs review
FileSize
902 bytes

Updated return value to be array rather than integer and updated comments.

jhodgdon’s picture

Status: Needs review » Needs work

Hi Kathy, good start!

Your new patch does not quite follow our documentation standards:
http://drupal.org/node/1354
There needs a blank line between the summary and the new explanation paragraph.

A couple of other things I would fix:
- Since there are several parameters to this function, " If passing FIELD_LOAD_CURRENT," might be clearer as "If $age is FIELD_LOAD_CURRENT..." -- similar with the other value?

Hmmm... There are some issues with the first and second lines...
- I don't think that "for example" needs to be in the 2nd sentence of the explanation.
- I am not sure entities "declare" file references... if they do, this needs some explanation?
- I guess the whole explanation is a bit confusing to me. These things being called "references" are not references in the programming sense, they are entities or revisions that have a field with the file attached, right?. Maybe we should either explain that? How about just changing the first line to say

Returns a list of entities or revisions that reference a file.

Then the whole explanation could be left out, I think?

kathyh’s picture

Status: Needs work » Needs review
FileSize
1.52 KB

Updated to add blank lines and param definition updates per comment #14

kathyh’s picture

Assigned: » kathyh
FileSize
1.51 KB

Removed misc whitespaces that shouldn't have been there.

jhodgdon’s picture

Status: Needs review » Needs work

Looks good, thanks! The docs seem very clear to me, and are formatted correctly.

One grammar nitpick: The word "which"... it either needs to have a comma before it, or it should be "that" instead. I think it should be "that" in the two places in this patch.

kathyh’s picture

Status: Needs work » Needs review
FileSize
1.53 KB

Changed 'which' to 'that' (per #17) and re-rolled to use D8 core dir (11/1/11)

jhodgdon’s picture

Status: Needs review » Needs work

Sorry, must have missed this the last time. The line wrapping here isn't quite right -- some of the 2nd line should be moved up to the first line, so that it wraps as close to 80 characters as possible:

+ * Returns a list of entities or revisions that reference a file.
+ * A file should not be deleted if still referenced.
+ *

I'm also wondering if this would be clearer with a data type:

  * @param $field_type
- *   (optional) The name of a field type. If given, limits the reference check
- *   to fields of the given type.
+ *   (optional) A field type that limits the reference check to fields of the
+ *   given type.  Default is type 'file'.

@param string $field_type
The reason is that "types" in Drupal are sometimes strings, sometimes objects, sometimes arrays, and you took out the "name of a field type" that was in the original (not sure why)... actually I like the wording in the original better, myself.

kathyh’s picture

Status: Needs work » Needs review
FileSize
1.44 KB

Update per comments in #19

jhodgdon’s picture

Status: Needs review » Needs work

Nitpick: After a . at the end of the sentence, we need to violate everything we learned in 8th grade typing class and put only 1 space rather than two. :)

Other than that, looks great, thanks!

kathyh’s picture

Status: Needs work » Needs review
FileSize
1.44 KB

Updates per #21

carlos8f’s picture

Status: Needs review » Needs work
  * @return
- *   An integer value.
+ *   An array of references. If $age is FIELD_LOAD_CURRENT, they are entity IDs.
+ *   If $age is FIELD_LOAD_REVISION, they are revision IDs.
  */

"they" doesn't seem to reference anything. Since this is a complex multidimensional array structure, it would be nice if the docs could make us aware of that structure. The keys in question here are 2-3 levels deep so as-is the developer will have to do a bunch of debugging just to figure out what this function returns.

kathyh’s picture

Status: Needs work » Needs review
FileSize
2.56 KB

Updates per #23

jhodgdon’s picture

Status: Needs review » Needs work

Wow, this patch changed a lot since the last time I reviewed it (#20). I am not really in favor of the current patch, with all the $ and () inside @code. We normally explain return structure by using words/lists.

Also, I'm not convinced that it is correct -- looking at the code for this function, it looks like it probably returns an array of objects, not an array of arrays as suggested in #5 and put into this patch. Feel free to tell me I'm wrong... but normally doesn't a query execute() return an array of objects?

jhodgdon’s picture

OK I'm probably wrong on this. Here's the one function that calls file_get_file_references:
http://api.drupal.org/api/drupal/core--modules--file--file.module/functi...

Anyway, we do normally use wording like this to describe returned array structure:

An associative array of xyzs, in which the keys are foo IDs, and the values are associative arrays representing bars. The bar arrays have the following elements:
- abc: The blah.
etc.

kathyh’s picture

Assigned: kathyh » Unassigned
Bevan’s picture

Priority: Normal » Major
Status: Needs work » Needs review

I started working on this and came up with simply "Nested array of entities keyed by field name, entity type and entity ID.". That is imprecise since it does not consider all the variations.

Having this as wrong as it is, is silly. We should at least change it from "An integer" to "An array" without delay. Bumping priority a to get some attention.

jhodgdon’s picture

Priority: Major » Normal

Sorry, you can't just bump a documentation issue up to major to "get attention". Incorrect docs are just not "major". See http://drupal.org/node/45111

To get this fixed, the most appropriate response would be to make a new patch. :)

jhodgdon’s picture

Status: Needs review » Needs work

Also, the patch above has already been reviewed and marked "needs work". We need a new patch.

Bevan’s picture

Status: Needs work » Needs review

You are right. I was just feeling frustrated that something so obviously wrong and easy to fix has become derailed by such a big discussion. Would a patch that simply changes "Integer" to "Array" be accepted as an interim solution while we work out better documentation for this function?

jhodgdon’s picture

Status: Needs review » Needs work

Not really. We need to just figure out good documentation for the function and get that into a patch. We don't really tend to accept "interim fix" patches in Drupal Core.

Also, please leave the status at "needs work" until there is another patch to review. Those of us following the issue will still respond to comments, but "needs review" means "there is a patch to review". (There is a link to a page explaining issue status under the Status field, if you are confused.)

Bevan’s picture

That is frustrating. But thank you anyway. :)

jhodgdon’s picture

Version: 8.0.x-dev » 7.x-dev
Issue summary: View changes
Issue tags: -Needs backport to D7

Looking at old issues... this was fixed in 8 but is still a problem in 7.

fietserwin’s picture

Status: Needs work » Closed (duplicate)