0

So, I have a view (D7, Views 3) that provides a list of staff with a thumbnail image based on a location "contextual filter". Some staff members are set as "sticky" and need to appear above all the others. All staff members also need to be sorted by last name, which i'm doing with a custom php sort.

So, first, stickied staff members should be shown sorted by last name. Next, unstickied staff should be shown also sorted by last name.

Last name sort works great, but I can't get the view to group them by sticky.

Here's a screenshot of the view: http://grab.by/h2Gg Here's a screenshot of the first part of the output: http://grab.by/h2GA (Jeremy Irwin is stickied, so should display at the top)

Here's the php code i'm using for the last name sort:

list($fname1, $lname1) = explode(' ', $row1->title , 2);
list($fname2, $lname2) = explode(' ', $row2->title , 2);
return(strcasecmp($lname1,$lname2));

Any suggestions why this isn't playing nicely with sorting by sticky?

4
  • Have you tried just using the views grouping instead? Add the content: sticky field and hide it. Then click settings and group by sticky. Not sure if it works which is why I havent added as an answer. Would be happy to add it though once you have tried it :)
    – 7wonders
    Oct 25, 2012 at 22:16
  • That actually works well- except it displays non-sticky nodes at the top. Is there any way to reverse this?
    – KraigH
    Oct 25, 2012 at 22:30
  • There should be a reverse checkbox on the field settings, not sure if that will affect the grouping order but you can try!
    – 7wonders
    Oct 26, 2012 at 1:12
  • Unfortunately it didn't affect the grouping order :( Great idea though.
    – KraigH
    Oct 26, 2012 at 3:31

2 Answers 2

1

So, first, stickied staff members should be shown sorted by last name. Next, unstickied staff should be shown also sorted by last name.

So you want 2 views (1 being an Attachment) and not 1 View. and you can do all this.

  • 1 View is of all the people fields with only stickied people as a filter. Sorted by last name.
  • The Attachment is all non-stickied people sorted by last name. You then "attach" this View after the 1st view. Get the page to look how you want using CSS in your theme.

As a final thought I tend to make 2 fields in my content types: first_name and last_name and then join them as a pseudo-field in Views using substitutions or use the Node Automatic Title to hide the title field by using the fname and lname as the title field upon save.

You could change your name column into 2 fields using Views Bulk Operations after creating the "last name" field in your content type ... just saying.

EDIT

The benefits to this approach is that you get rid of the odd custom sorting you've hand made and rely on Core Views utilities on your site. This makes moving your site or upgrading drupal versions easier in the long run ...

1
  • I would even go further and simply make two view blocks and put them one right after the other on the page. Oct 17, 2017 at 8:33
0

So, I ended up combining the two sorts into one php field, which works great. Here is the code I used:

list($fname1, $lname1) = explode(' ', $row1->title , 2);
list($fname2, $lname2) = explode(' ', $row2->title , 2);
$sticky1 = $row1->sticky;
$sticky2 = $row2->sticky;

// $row1 = sticky so favor it
if ($sticky1 == 1 && $sticky2 == 0) { return -1; }

// $row2 = sticky so favor it
else if ($sticky1 == 0 && $sticky2 == 1) { return 1; }

// Neither are so do normal sort
else { return(strcasecmp($lname1,$lname2)); }

Your Answer

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

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