Hi

In my post I just want to ask if I used proper way to add one more custom filter to view?

Because I didn't find any materials on how to do it,
I decide to add filter by editing original query form view module.

Below U will find how I do it:

$ReplaceString1 = "SELECT";
$ReplaceString2 = "FROM {node} node";

$PlusString0   = "SELECT * FROM (SELECT node.uid AS node_uid, ";
$PlusString1 = "FROM {node} node LEFT JOIN {users} users ON node.uid = users.uid";
$PlusString2 = "ORDER BY RAND() LIMIT 10) AS tmp GROUP BY node_UID";


  if($view->vid == 4) //vid 4 is a custom view where I want to have extra filter
  {
   //building string for new query
   $newquery  = str_replace($ReplaceString1,$PlusString0,$info['query']);
   $newquery  = str_replace($ReplaceString2,$PlusString1,$newquery);
   $newquery .= $PlusString2;
   $query = db_rewrite_sql($newquery, 'node');
   //drupal_set_message(''.print_r($query,TRUE).'');

  }
  else{
   $query = db_rewrite_sql($info['query'], 'node');
  }

Thx

Comments

nevets’s picture

It would probably help if you said what you want to filter on. Views provides ways that are probably more appropriate but more details on your goal would help.

CristinaM’s picture

hmmm I think I cannot even say that it was filter limitation because filters are in WHERE clause.

I wanted to create a view (gallery) for image nodes which will display one random picture per user.

Bellow example of my query:
-code marked by strong tag shows what I add to original query
-sorry that I didn't saround my example with code tag, but I wanted to show a places which I change and with code tag active I couldn't do it

SELECT * FROM (SELECT node.uid AS node_uid, node.nid, node.title AS node_title, node.changed AS node_changed, node_data_field_image.field_image_fid AS node_data_field_image_field_image_fid, node_data_field_image.field_image_title AS node_data_field_image_field_image_title, node_data_field_image.field_image_alt AS node_data_field_image_field_image_alt FROM {node} node LEFT JOIN {users} users ON node.uid = users.uid LEFT JOIN {users_roles} users_roles ON node.uid = users_roles.uid LEFT JOIN {content_type_image} node_data_field_image ON node.vid = node_data_field_image.vid WHERE (node.status = '1') AND (node.type IN ('image')) AND (users_roles.rid = '3')ORDER BY RAND() LIMIT 10) AS tmp GROUP BY node_UID

This what I did its working without any problems but my question is if I did it in correct way?
Maybe I could use some options on view other than filters?

Thx

kadog’s picture

Hi, CristinaM,

Great job!! I have been looking around on the forum for two weeks to look for a idea like this.

But to make it work, could you please tell me where to put your mySQL query command? Do you need to change the 'Views module' or need to create a new module?

Another thing, I need to do is to pass user's input to the 'Views module' as parameter, and let "Views' run a query on my costumed content type. It seems I have to create a new module with form in there and make a button to to a URL with user's input. My question is if there is better and easy way do it.

I think querying web database according to user's input should be very common work. There should be easy way to do it. Thanks, ldong

CristinaM’s picture

Hi

Regarding first question:

U need to edit views.module file and add few code lines.
Generally find a function db_rewrite_sql and replace it with my code from top of the topic.
Basically I'm doing simple string edition by php functions.

The most important is to know "view id" for which u want to update query.
This id u will put in if statement. Something like if(vid == "yours vies id")

U can check it in several ways, I'm checking directly from views table by simple query.

The only thing is that Im also a very new in Drupal and I don't know if this is the way to change queries.
I think If u will make update of view module then u will have to replace code all the time,
its not best solution.

Regarding second question:

Im not sure about it but check arguments section for views.

caver456’s picture

Hi - I think I figured out maybe the best place for it; take a look at:

http://drupal.org/node/409808

Thanks, your code was a real help in figuring things out.

voj’s picture

For those interested, it's worth taking a look.

http://zugec.com/drupal/creating-custom-filters-in-views-2