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

dawehner’s picture

You 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)

hosais’s picture

Component: Code » User interface

Changing 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()

function tcfilter_views_api()
{
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'MYMODULE') . '/inc',
  );
}

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',.

function MYMODULE_views_handlers() {
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'MYMODULE') . '/inc',
    ),
    'handlers' => array(
      // The name of my handler
      'MYMODULE_handler_filter_MYFILTER' => array(
        // The name of the handler we are extending.
        '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

hosais’s picture

Title: Customizing View + exposed filter <= View PHP filter v.s. MyModule+hook function » Customizing View + exposed filter <= How to make the added class executed?
Component: User interface » Code

Changing this to coding issue.

dawehner’s picture

The 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.

hosais’s picture

Fantastic! Genial! Thank you dereine.

With your help, I found the definition of handler in node.view.inc.

$data['node']['title'] = array(
    'title' => t('Title'), // The item it appears as on the UI,
    'help' => t('The title of the node.'), // The help that appears on the UI,
     // Information for displaying a title as a field
    'field' => array(
      'field' => 'title', // the real field. This could be left out since it is the same.
      'group' => t('Node'), // The group it appears in on the UI. Could be left out.
      'handler' => 'views_handler_field_node',
      'click sortable' => TRUE,
     ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    // Information for accepting a title as a filter
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  ); 

After modifying handler in hook_views_data_alter(&$data), it works.

hosais

dawehner’s picture

Component: User interface » Code
Status: Active » Fixed

Perfect!

Let's mark this issue as fixed.

hosais’s picture

OK.

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

hosais’s picture

Version: 6.x-2.9 » 6.x-2.12

Hi,

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

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.