The "File Usage: File" relationship does not seem to respect the sort order of the image field.

If I use Content: Images to display images from an image field with multiple values, then the sort order (delta) is respected, but I cannot rewrite the image tag using this method.

I need to rewrite the image tag in order to get the Title and Description attributes included so they can be used as captions.

In order to rewrite the image tag in this way, I need to use File Usage: File relationship, and then use

(File) File: Alt (Alt)
(File) File: Title (Title)
(File) File: Name

and rewrite the name as an img tag using replacement tokens, i.e.

<img src="/sites/example.com/files/[nid]/[filename]" title="[field_title]" alt="[field_alt]" />

This works as far as displaying the images, but the delta sort order of the images appears to be ignored as soon as the "File Usage: File" relationship is added to the view.

Comments

jamesfk’s picture

+1 - in fact the file relationship will output all files with no respect to the field either.

dawehner’s picture

Status: Active » Fixed

Well the file usage: file is a relation from node to file_usage not from the images of a node to file_usage.

That's basically impossible to fix as the sql data simply can't do what you want, i'm sorry for that.

Status: Fixed » Closed (fixed)

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

lorique’s picture

I know this is closed, but for those who come across this looking for a solution, i can say that there is one if you don't mind getting your hands dirty with some code.

My case:
In my case we have a gallery page where we need to take out all media on a node, and list them in the correct order. The node contains 2 media fields, primary and secondary, where primary is used for displaying the node in list views, and secondary is shown in some list views, and always in full view. When displaying the gallery.

My solution
I created a content view, added a contextual filter on NID on it for placing on the node page. Then i added a relationship to file usage, and then i set the view to render fields, only rendering the File: Rendered with the file usage relationship on it. This gives us a view which takes out all the files on a node, and renders them (We use file entity and file view modes for rendering) the way we want it.

Next comes the code bit.
I added the following code to the feature where i exported my view:

 
/**
 * Implements hook_views_pre_execute()
 *
 *    Changes the view query to order by the delta from primary and secondary media,
 *  around views, because views does not know how to handle this.
 *
 */
function mymodule_views_pre_execute(&$view) {
  if ($view->name == 'files_on_node') {
    $query = &$view->build_info['query'];
    $query->leftJoin('field_data_field_primary_media', 'pm', 'pm.field_primary_media_fid = file_usage.fid');
    $query->leftJoin('field_data_field_secondary_media', 'sm', 'sm.field_secondary_media_fid = file_usage.fid');
    $query->orderBy('ifnull(pm.delta, sm.delta)', 'ASC');
  }
}

The code adds a left join on both my field data tables, and then orders them by the delta's. The ifnull command checks if the first variable (pm.delta) is null, and if it is uses the other argument (sm.delta) for ordering. Otherwise its pretty streight forward.

I'm planning a module with a new views relationship, and ordering option for views, so it can be configured rather than coded. Until then, this will hopefully help anyone running into this issue on their way to a solution.

lorique’s picture

Issue summary: View changes

wrapped img tag in code tags.