views:

10268

answers:

4

I have the Profile, CCK, and Views2 modules installed on a Drupal 6 site. I added a string field to the user profile. I can filter easily on preset values, thru the Views GUI builder, really nicely. However, I'd like the filter criteria to be dynamically set based on other environment variables (namely the $_SERVER['SERVER_NAME']).

Is there a basic 'How-to-write-a-custom-drupal-views-filter' somewhere out there? I've been looking thru the documentation, but it's not obvious to my simple mind on how to do it.

Thanks!

A: 

There is the possibility, having looked at the sort of filters installed for my own site, that filters have to be based on some database field, in which case what you're trying to achieve is not possible. It appears that the filters provide the WHERE clause to the generated SQL query.

Having said all that, if you want to pursue it further, your best bet is to start with a module that already provides filters for Views. There are filters provided with Views for the Node module; alternatively, you could look at the audio module which also provides some filters. Additionally, posting to the Drupal forums or support list may turn up another module that will allow you to achieve what you're attempting.

alastairs
A: 

yes you can do it. Try using the module "views filter block". Once you enable the block .. extract the html of the block from "view source" when viewing the page. Now disable the "views filter block" ... create your own custom block .. add the code to it with whatever css you like to make it look pretty . Within this code use php to dynamically specify what you want for the filter initial selection to be. Make sure you actually choose the field the filter is based on .. then within the custom php block use php code to write IF condition to check for the server_name value and accordingly assign the filter variable the right value."

There maybe other (possibly even better) ways to do it to actually write a module to use the filter . So this is but one suggestion. Also give "Views PHP Filter" a try. I have not used it yet but sounds like its worth a shot.

  • by drupal user (drupal username: drupdrips)
+1  A: 

You can create your own function like following to add your own filters.

<?php custom_views_embed_view($view_name, $display_id) {
$view = views_get_view($view_name);
$view->set_display($display_id);
$id = $view->add_item($display_id, 'filter', 'node', 'created',
                      array( 'value' => array('type' => 'date', 'value' => date('c')), 'operator' => '<='));
return $view->execute_display($display_id);
}
?>
AbhiG
@AbhiG have you ever tried this method? this seems to be an interesting piece of code. i should place my code in operator, replacing '<=' right?
barraponto
A: 

how do i add a filter without installing filter views block module?

this is not an answer. Please use comments to ask further questions or make your own question.
Chuck Vose