If I add fields to do with the image file (such as filename), the view shows multiple copies of each node, one for each actual derivative image file.

What's needed in the SQL is image_files.filename = '_original'
Going by the Views API docs here: http://drupal.org/node/99564 we need two things: an addition clause, and also to make it an inner join.

This is what works:

    'join' => array(
      'type' => 'inner',
      'left' => array(
        'table' => 'image_image',
        'field' => 'fid',
      ),
      'right' => array(
        'field' => 'fid'
      ),
      'extra' => array(
        'filename' => '_original',
      ),      
    ),
CommentFileSizeAuthor
views.inc_.innerjoin.patch752 bytesjoachim

Comments

drewish’s picture

what if you wanted one of the other filenames?

joachim’s picture

Maybe we need to add an option for the filename field...
though that field itself doesn't quite do what it says on the tin -- it just returns "_original", "preview", etc.

Getting back several entries per node seems to go against what you expect from Views: one thing per node.

drewish’s picture

i think the option is the way to go that or offer an inner joined table for each size...

joachim’s picture

I realised after I commented that a lot of it depends on your use case.
I've only been thinking of using those fields in views to make image galleries of one sort or another.

You might feasibly want to list all the files that are images, and all derivatives, in which case you don't want the inner join -- even though it does sort of go against the grain of Views.
Is that the sort of thing you had in mind?

We can have both kinds of functionality by specifying the table twice, I think -- there's something in the API docs about aliases.

We certainly need the inner join in some cases -- I tried implementing sorting by filename (http://drupal.org/node/204932) and it produces multiple copies of each node which definitely isn't desirable.

dman’s picture

Bump on this.
Thanks heaps for the patch(es) .. I eventually folded it in, but it's a pain that it seems that the current image/views.inc can't deal with it. I see the placeholder code is there, but it's not available in the UI, or working :(

Gosh it's scary messing around with SQL at this level of abstraction!
Plus ... and here's a biggie - views definitions get cached a lot, so every time you make a change in the hook_views_tables(), you also have to re-save the view before it takes effect. Lost an hour on that one!

sun’s picture

Status: Needs review » Needs work
joachim’s picture

Not so much needs work as needs decision...

sun’s picture

Status: Needs work » Closed (won't fix)

I think at least in D6, we have a dropdown to select the image size, so I guess this won't fix anymore.