11
votes

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.

5

5 Answers

3
votes

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);
}
?>
1
votes

I have a similar problem and this article has been very helpful in resolving the problem.

http://www.metaltoad.com/blog/drupal-7-tutorial-creating-custom-filters-views

And hook_views_data oficial documentation

http://api.drupal.org/api/views/docs%21docs.php/function/hook_views_data/6

0
votes

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.

0
votes

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)
0
votes

You can use viewsphpfilter module which allows filter views by node id. however there is a patch if you need to extend this for user views