1

I want to change the sorting of a view with something very specific which needs 2 subqueries.

So far, I tried to do something like this:

$sorted = false;

function mymodule_query_alter(&$query) {
  global $sorted;

  $view = $query->getMetaData('view');
  if( !$sorted && $view->name == 'my_view' ) {
    $sorted = true;

    $subquery_1 = clone $query;
    $subquery_2 = clone $query;

    // Apply sorting stuff...

    $query = $subquery_1->union($subquery_2);
}

Then I just realized that this desn't even work (while my view is working):

$subquery = clone $query;
$query = $subquery;
dpm($query->__toString());
dpm($query->arguments());

It doesn't return any result, but everything seems ok in the dpm dump.

Have I done something wrong? Or maybe should I handle this problem in another way?

EDIT:

I need to get a list of nodes sorted this way:

  1. Sticky nodes | order by date.
  2. Nodes in one of the taxonomy-term selected in user profile | order by date.
  3. Other nodes | order by date.

I've already a working SQL request (actually 3 with UNION), but I need to make it work with Drupal.

6
  • I would expect it to be more useful to use views hook_views_query_alter rather core's hook_alter. Have you tried this? Dec 15 '14 at 10:30
  • with reference to other ways to handle the problem, we might need some additional detail about what you are trying to accomplish. Might it be possible to use Relationships, for example? Dec 15 '14 at 10:34
  • @autopoietic I tried with the views hook, but it uses a views_plugin_query object which doesn't provide UNION instead of a SelectQuery. I'll edit my answer for further details.
    – Arthur
    Dec 15 '14 at 11:07
  • "It doesn't return any result, but everything seems ok in the dpm dump." - looks like views creates the query you told it to, but not the one you want... What happens if you run it directly in DB, using commandline client, or interface like phpmyadmin?
    – Mołot
    Dec 15 '14 at 11:20
  • @Mołot As I said, the SQL query seems to work. Without alteration query alteration: $query returns results, and doesn't if I just clone it before.
    – Arthur
    Dec 15 '14 at 13:00
1

So, I was working on a test for @Mołot to reproduce the clone bug, and I couldn't with a fresh view.

I tracked down the difference :

  • With the filter Published or admin: the cloned query didn't return anything.
  • No problem with the simple Published filter.

Don't ask me why.

As @autopoietic said, working with core query_alter_hook wasn't a good idea. I switched to views hook, and got the SelectQuery object :

function mymodule_views_pre_execute(&$view) {
  $query = &$view->build_info['query'];
}

Then I was able to do my sorting.

1
  • 1
    I would guess the clone didn't work because further in the views processing the node access checks are added for published and user role check. These checks are adding during processing via meta data to the Query object.
    – tenken
    Dec 19 '14 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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