Hi,
I had a search page using View + exposed filter. I would like to reuse the exposed filter in a link. That is, I am interested passing arguments in URL to the exposed view filter.
Currently, I found 2 possible related ways.
1) One is view PHP filter(http://drupal.org/node/690670). It seems that I can add a new filter with php code.
2) Another is creating a Mymodule and use hook function to replace the filter(refer to http://drupal.org/node/360780#comment-form).
I am not sure which the best way is, could anyone give me some opinions?
By the way, my exposed filter is CONTAIN ANY WORDS to the NODE TITLE. I also would like to adjust it a little bit. I mean changing the criteria of searching nodes. Can I apply another validation(with php) after the result of the [CONTAIN ANY WORDS to the NODE TITLE] search?
Many thanks.
hosais
Comments
Comment #1
dawehnerYou could also write some kind of filter handlers to be really as flexible as possible.
But if you can't take the time option b) which is hook_form_alter is probably much better as option a)
Comment #2
hosais CreditAttribution: hosais commentedChanging the query ( CONTAIN ANY WORDS ...) is about changing the construction of the SQL. This is related to the Filter object.
I changed code directly (views_handler_filter_string class) and add the new operator successfully. Now I would like to implement it in the way of the child class of views_handler_filter_string, instead of changing it directly.
So, I created Mymodule and do the following:
I do the following:
1) register my module to views by implementing views_api()
2) implement the class MYMODULE_handler_filter_MYFILTER (extends views_handler_filter_string), and overide the functions related to my modification. Since I just would like to add one option to the operator to the exposed filter, there is not much to change for the form.
3) In Mymodule_views_handlers() register the object to the parent,
'parent' => 'views_handler_filter_string',.
However, after the module enabled without errors, the code (number 2 step) is not executed. I miss something here? Anyone knows the problem? Thanks.
Hosais
Comment #3
hosais CreditAttribution: hosais commentedChanging this to coding issue.
Comment #4
dawehnerThe handlers are always attached to views data
Therefore you have to implement hook_views_data_alter(&$data) and change the 'handler' of what you want to extend.
This can be done in your .views.inc file.
Comment #5
hosais CreditAttribution: hosais commentedFantastic! Genial! Thank you dereine.
With your help, I found the definition of handler in node.view.inc.
After modifying handler in hook_views_data_alter(&$data), it works.
hosais
Comment #6
dawehnerPerfect!
Let's mark this issue as fixed.
Comment #7
hosais CreditAttribution: hosais commentedOK.
About "I would like to reuse the exposed filter in a link. That is, I am interested passing arguments in URL to the exposed view filter.", I think it was discussed at Pull filter value from an argument (http://drupal.org/node/357082).
hosais
Comment #8
hosais CreditAttribution: hosais commentedHi,
I just finished tracing the code and conclude my little module. To pass the argument to filter, the key point is
1) disable query of argument handler (Not add where clause);
2) let the filter handler read the argument to value property of a view(for example, $this->value = $this->view->args[0];). The code could be put into per_query().
hosais