3

Suppose I am printing name tags for thousands of content nodes in one content type called "Attendee." Each of these nodes specifies a single "Attendee" for an event, (and their respective name tag). Suppose these attendees also needed name tags printed out for their “friends” who are attending with them. That is not a problem: These friends need their own node with the same fields to get their own name tag, so I keep them in the same content type and distinguish that they are a friend of an attendee with a simple checkbox.

When rendering the view, I need these friends of attendees to appear right after the attendee they are going with. So to link them, I used a node reference or a entity reference field. How do I get the Views module to sort the friends of attendees right after the attendee they are going with?

P.S. I am technically using the Views PDF module, but it has the basic views functions, so if it's possible with Views, it will probably be possible with this module.

2
  • If you create a relationship to the referenced entity (friends), does it provide their name as a field to sort by? Commented Jul 1, 2013 at 5:29
  • @KyleTaylored, thanks for the response. When I use the Entity Reference field to make a relationship, it gives me two options, the "Referenced" entity & the "Referencing" entity. I have tried both of these and do not see extra fields to sort by appear. When the relationship is added, only the Referencing or Referenced node entities show (no others even though I've checked for it to "not require the relationship"). Not sure if that is normal behavior; with this method I've been able to target just one or the other (parent/child) in the link. Just not both together & certainly in no sort order.
    – WebMW
    Commented Jul 1, 2013 at 21:48

1 Answer 1

1

Since you haven't been able to see the new fields for ordering (which is odd), you will probably have to perform a query alter on the views query and do some modifications:

<?php
function mymodule_views_query_alter(&$view, &$query) {
  if ($view->name == 'myviewname') {
    $view->query->somecodetoselectfields
    $view->query->somecodeforleftjoin
    $view->query->orderby[0]['field'] = "FIELDNAME";
    $view->query->orderby[0]['direction'] = "ASC";
  }
}
?>

What I usually do is just add a tag on the views query so that I can modify it using the standard Drupal query interface, which is much easier to use IMO.

<?php
function mymodule_views_query_alter(&$view, &$query) {
  if ($view->name == 'myviewname') {
    $query->tag[] = 'mytag';
  }
}
?>

Then

<?php
function mymodule_query_mytag_alter(QueryAlterableInterface $query) {
  $query->blablabla;
}
?>

Take a look at:

https://www.drupal.org/node/310077 https://api.drupal.org/api/views/views.api.php/function/hook_views_query_alter/7 https://api.drupal.org/api/views/plugins%21views_plugin_query_default.inc/class/views_plugin_query_default/7

Apologies if the solution is not straightforward enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.