Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom filter for the Drupal View's Module?

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.

like image 899
user6824 Avatar asked Oct 27 '08 22:10

user6824


People also ask

Is used for filtering content in Drupal?

The Content Management Filter is a contributed module that adds an easier way for administrators to filter the content on a Drupal site for administration purposes.

What is Drupal filter?

The Filter core module allows you to configure text formats for processing text input for your site. These settings are under Configuration > Content authoring > "Text formats and editors" ( /admin/config/content/formats ).


2 Answers

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);
}
?>
like image 57
AbhiG Avatar answered Sep 27 '22 15:09

AbhiG


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

like image 33
mjimcua Avatar answered Sep 27 '22 17:09

mjimcua