Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 679 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding a custom filter to views in Drupal 7

#1
im using Drupal 7 and I want to add a new filter in views.

I have a custom table "clicks" with two fields; nid and clicks_left.

The filter should just contain a checkbox "Only display nodes with clicks left". So the filter should join node and clicks on nid..

I have read like thousands of pages of custom filters but can't get it to work =)

Please, could someone show me a working example so I understand?

I have come so far that the filter is displayed under filters but what do I need to add to do the join and get the checkbox? The relevant code below:

FILE clicks_views.inc:

function clicks_views_data() {
$data = array();

$data['clicks']['clicks_filter'] = array(
'group' => t('Clicks'),
'title' => t('Clicks left'),
'help' => t('Filter any Views based on clicks left'),
'filter' => array(
'field' => 'clicks_left',
'handler' => 'clicks_handler_filter',
),
);

return $data;
}


FILE clicks_handler_filter.inc:

<?php
class clicks_handler_filter extends views_handler_filter {

???

};

I know both functions are wrong ;)
Reply

#2
Actually, if your values in your tables clicks are numeric you don't need to create your own handler, you can use the default from Views `views_handler_filter_numeric`.

You can see all handlers that already exists in the Views `handlers`.
Reply

#3
Ok, I've found a solution. For anyone who needs it:

In clicks.module

function clicks_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'clicks') . '/includes'
);
}

In clicks.views.inc

function clicks_views_handlers() {
return array(
'info' => array(
'path' => drupal_get_path('module', 'clicks') . '/includes', // path to view files
),
'handlers' => array(
// register our custom filter, with the class/file name and parent class
'clicks_handler_filter' => array(
'parent' => 'views_handler_filter',
)
),
);
}

function clicks_views_data() {
$data = array();

if(module_exists('clicks')) {
$data['node']['clicks'] = array(
'group' => t('Clicks'),
'title' => t('Clicks left'),
'help' => t('Filter any Views based on clicks left'),
'filter' => array(
'handler' => 'clicks_handler_filter',
),
);
}
return $data;
}

In clicks_handler_filter.inc

class clicks_handler_filter extends views_handler_filter {

function admin_summary() { }
function operator_form() { }
function query() {
$table = $this->ensure_my_table();
$join = new views_join();
$join->construct('clicks', $this->table_alias, 'nid', 'nid');
$this->query->ensure_table('clicks', $this->relationship, $join);
$this->query->add_where($this->options['group'], "clicks.clicks_left", 0, ">");
}

}

This gives me a possibility to add a filter "clicks" that if enabled hides all results that doesn't have clicks left (clicks_left > 0)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through