1

Now, i'm not quite sure on the vocabulary around entities and bundles, so apologies, but here's my problem.

I've got lots of content types, and in a view i want to sort nodes first by content type, and then by date

But there is no value on which to sort that makes sense - i want to sort content types into an bespoke order 'Clothes, animals, drinks, people' - i.e. not alphabetical.

I have tried using global php to sort these into a bespoke order - it works fine. i.e.

$myarray=array("films","shoes","people","animals");

This is the set up code and then for every row, views php allows you to sort thusly:

$pos1=array_search($row1->type, $myarray);
$pos2=array_search($row2->type, $myarray);
return $pos1-$pos2;

It works, but I can't combine it with the date sort, and anyway the overhead must be pretty bad.

So, my question is, is there a way that I can add a weighting field to the content types, at what I think might be the bundle level (!?) to allow me to sort my data in views by content type, in a bespoke order (non-alphabetical).

Phew. Any help appreciated.

gillarf
  • 114
  • 9

2 Answers2

1

In MySQL, there is a way to order by a given list order. See MySQL sort by some list

To do this in Drupal, you can try to add something like this in a hook_views_query_alter:

$query->orderby[0] = "FIELD(node.type, 'films', 'shoes', 'people', 'animal') ASC";

As explained in https://drupal.stackexchange.com/questions/35543/syntax-for-the-various-query-modifiers

Community
  • 1
  • 1
nmc
  • 8,724
  • 5
  • 36
  • 68
  • Thanks, I will try this, it looks right. I tried something similar already, which didn't work, but you've convinced me! – gillarf Oct 01 '12 at 17:38
0

In case anyone comes across this, nmc's solution is good, but I couldnt get it to work (my fault surely) so I used hook_views_pre_render:

function MYMODULE_views_pre_render(&$view){

if($view->name=='entries_for_a_journal') {

    $entryitemarray=array();

foreach($view->result as $entryitem){
    $entryitemarray[$entryitem->node_type][]=$entryitem;
    }
$listoftypes=array('destination','tourplan','packing_list','diary','accomodation','eatinganddrinking','placesofinterest','knowledge','recipe','peopleimet','usefulcontacts');

foreach($listoftypes as $typename){
foreach($entryitemarray[$typename] as $newviewentry){
$newviewresult[]=$newviewentry;

}

}


    $view->result=$newviewresult;  

}


}
gillarf
  • 114
  • 9