Thanks for a fantastic module, I've been using it for a few months now and it's absolutely brilliant.
However, I've recently set up a contextual filter on my view and I'm experiencing what I believe to be a bug…
Without the contextual filter triggered, the view works wonderfully and the items are sorted in the draggable views order. However, when the contextual filter is triggered (and some results are therefore excluded) the view seems to have no order at all. I would like for the results to remain sorted by the draggable views order when the contextual filter is applied, but with the items excluded from the view.
Below is my real world example:
Contextual filter: if user does not have role of X then exclude Y items from view, otherwise show view as normal
When the user does have role of X, no contextual filter is applied; the view works as expected and the items are sorted in the draggable views order.
When the user does not have role of X, the contextual filter is applied and results are therefore excluded; the items seem to be in no order at all.
Any help would be hugely appreciated as this is a major problem on my website.
Thanks,
totkoo
Comments
Comment #1
totkoo commentedIs anyone able to help with this?
Comment #2
istryker commentedI don't have time to look at this into great detail right now, but from what I remember, adding a contextual filter adds an argument.
If you have an exposed filter it adds
[name_of_filter] = "[argument]"
If you have a contextual filter it adds
[#] = "[argument]"
where # starts from 0...to...X
Arguments get saved to the native database table [draggableviews_structure]. A view with different arguments will produce 2 different orders.
When you add the draggableviews weight sort, you have a choice to use all arguments, use none, or custom. I would select none.
Advance: There is also a hook to remove arguments from the view before they are saved to the database
Let me know if this solves your problems
Comment #3
stijndmd commentedI had the same problem. Too bad op didn't log back to thank you, iStryker, because your solution works like a charm.
You are a lifesaver, thanks.
Comment #4
RobNL commentedI have the same problem, but solution #2 didn't work for me. Does somebody have a different idea?
Comment #5
istryker commentedI want to fix this in the next release, but I don't think it will be solved anytime in the next month or so, as it looks like I"m trying to add partial functionally to d8 core before the code freeze
Comment #6
RobNL commentedOk, thanks for the update.
Comment #7
RobNL commentedOk, thanks for the update.
Comment #8
seanrHas this been looked at again? Setting use arguments to none didn't work for me. Nothing I try works. :-(
Comment #9
argiepiano commentedI ran into the same problem with Draggableviews and contextual filters (and also with "regular" exposed filters). My View was performed on a specific node type. I found a good alternative to Draggableviews (works only for nodes): : the module Weight. It implements a similar interface to rearrange the order of the nodes in Views tables (see https://www.drupal.org/node/1466402 ). It works great with filters.
Comment #10
calebtr commentedI have a simple view, with a contextual filter for taxonomy term ID to show related nodes in a block. The Draggable Views sort order was not being applied.
When I looked at the query, I saw that it was setting the argument placeholder to
["4"]- the raw term ID - when the DB was holding{"tid":"4"}in the args column. The view was trying to matchjson_encode(array(4))withjson_encode(array('tid' => 4))and failing.I resolved this using the "Prepare arguments with PHP" option in the sorter to make the argument match. In my case, I used:
I had *set* the argument using an exposed filter on a page view, which stored the value with its key. My block view was passing in only the numeric value. This behavior is described in the README.txt for the module.
I'll see if I can come up with a patch, though views contextual filters seems pretty complex.
Comment #11
ahmed.raza commentedAgreed with #9, solved my problem :) Thanks!
Comment #12
calebtr commentedIf you have a view/display that is managing the order for another view/display, you can run into this problem.
When you save the order of a view, Draggable Views saves views contextual filters (arguments) and other filters with it.
If you have just one view or display to manage, you can get away with telling the DV sort widget to not use arguments for calculating the sort order.
If you have more than one view/display to manage and you want to save the order separately, you need to use the "Prepare arguments with PHP" option in the sort widget.
1. Before you get sorted poking at the database and code, set up your view and contextual filter how you want it and make sure it works before changing the draggable views order.
2. Figure out how the arguments are being saved. Save a new order of your contextual filter and then go into your database and run:
SELECT * FROM draggableviews_structure;Note the name of your table will be different if you are using table prefixes.
See how the order is encoded in the 'args' column. In my case, I have:
{"tid":"2"}This is a json-encoded variable. Decode it with PHP:
Or from the command line -
php -r 'var_export(json_decode("{\"tid\":\"2\"}", true));'I get:
So now I know what Draggable Views is expecting to pass in order to sort the view correctly.
3. Next, look inside your view and figure out what *is* getting passed.
The simplest way to do this is to enable the devel module and use the "Prepare arguments with PHP" in the sort widget and enter
dsm($arguments);$arguments is the variable Draggable Views is json_encode-ing and passing to views.
In my case I got:
2- that is, a string, one character long, the number two. This doesn't match anything in the database.4. Now write some code to make Draggable Views pass the right variable and put that in the "Prepare arguments with PHP box" instead of the DSM call. The help specifically says what you return here will be passed as the argument.
In my case, I want to return the array I json_decoded, above. I just copy and paste:
To make the contextual filter apply more generically, use the $arguments variable in your code:
return array('tid' => (string) $arguments);5. Drag some things around and sort some views and test it out.
Devel's dsm() and dpq() are handy for troubleshooting. You can see what views is up to in a custom module with hook_views_pre_execute() or in your theme's template.php with hook_preprocess_views_view().
If anyone comes up with a more generic solution, please share and post a patch if possible; I'll see what I can come up with as well.
Comment #13
squarecandy commentedWhen using "Content: Has taxonomy term ID (with depth)" I had to use the following under "Prepare arguments with PHP":
return array(array_pop($arguments),'0');Comment #14
formatix labs commentedIf you need to sort nodes in view according their menu order, consider views_menu_sort module using. I fount that here: https://www.drupal.org/node/313140#comment-5812780
Comment #15
michaeldknox commented@calebtr, thanks for the help, but I'm getting stuck on one part and its probably due to my lack of PHP experience. I"m trying to pull the contextual filter into the PHP argument via $arguments, but I keep getting [ ] in my result.
my current php
draggableviews_structure.args = '{\"field_gallary_image_category_tid\":[\"50\"]}
(yes, I know gallary is spelled wrong. Its on purpose)
when I add (string) before $arguments, I get
draggableviews_structure.args = '{\"field_gallary_image_category_tid\":\"Array\"}
I'm struggling to remove the [ ]
Comment #16
calebtr commentedMy solution may not be for you! Did you try the other suggestions?
Regarding your PHP - the value of $arguments is an array and you want it to be a string?
You can get the first value of the array lots of ways:
You may need to debug and figure out the exact value for $arguments - if all else fails, you can always write it to your error log: