Nasir Uddin June 12, 2013 1 Comment

Sometimes, we face drupal node filter by “This Month”, “Last Month”, “Current Year”. But how way we can solve this problem. Please follow below procedures…
What query we have to use …
For “This Month”
[sourcecode language=”bash”] SELECT DISTINCT FROM_UNIXTIME(node.created, "%M") from node where node.type = ‘fund’ and node.created > UNIX_TIMESTAMP( LAST_DAY( DATE_SUB(NOW(), INTERVAL 1 MONTH ) ) );
[/sourcecode] For “Last Month”
[sourcecode language=”bash”] SELECT DISTINCT FROM_UNIXTIME(node.created, "%M") from node where node.type = ‘fund’ and node.created between UNIX_TIMESTAMP( LAST_DAY( DATE_SUB(NOW(), INTERVAL 2 MONTH ) ) ) AND UNIX_TIMESTAMP( LAST_DAY( DATE_SUB(NOW(), INTERVAL 1 MONTH ) ) );
[/sourcecode] For “Current Year”
[sourcecode language=”bash”] SELECT DISTINCT FROM_UNIXTIME(node.created, "%Y") from node where node.type = ‘fund’ and from_unixtime(node.created, "%Y") = 2013;
[/sourcecode] I wanted output like in below image…
To develop above output i developed a view like in below image….
s2
Now you have to use form alter api for alter exposed form for date
[sourcecode language=”bash”] function happenings_filter_alter(&$form, &$form_state) {
$form[‘created’][‘#type’] = ‘select’;
$form[‘created’][‘#options’] = array(‘1’ => ‘This Month’, ‘2’ => ‘Last Month’, ’13’ => ‘Current Year’);
$form[‘created’][‘#value’] = isset($_POST[‘created’]) ? $_POST[‘created’] : ”;
$form[‘created’][‘#default_value’] = isset($_POST[‘created’]) ? $_POST[‘created’] : ”;
$form[‘created’][‘#size’] = ”;
return $form;
}
[/sourcecode] For modified date exposed filter you have to query alter by query alter api like in below code..
[sourcecode language=”bash”] function _happenings_filter_query_alter( $view, $query ) {
if(empty($view->exposed_raw_input[‘created’]) OR $view->exposed_raw_input[‘created’] == 1) {
$item = array();
$item[‘field’] = ‘node.created > UNIX_TIMESTAMP( LAST_DAY( DATE_SUB(NOW(), INTERVAL :month MONTH ) ) )’;
$item[‘value’][‘:month’] = 1;
$item[‘operator’] = ‘formula’;
$query->where[1][‘conditions’][3] = $item;
}
elseif($view->exposed_raw_input[‘created’] == 2) {
$item = array();
$item[‘field’] = ‘node.created between UNIX_TIMESTAMP( LAST_DAY( DATE_SUB(NOW(), INTERVAL :month1 MONTH ) ) ) AND UNIX_TIMESTAMP( LAST_DAY( DATE_SUB(NOW(), INTERVAL :month2 MONTH ) ) )’;
$item[‘value’][‘:month1’] = 2;
$item[‘value’][‘:month2’] = 1;
$item[‘operator’] = ‘formula’;
$query->where[1][‘conditions’][3] = $item;
}
elseif($view->exposed_raw_input[‘created’] == 13) {
$item = array();
$item[‘field’] = "FROM_UNIXTIME(node.created, ‘%Y’ ) = FROM_UNIXTIME(:time, ‘%Y’)";
$item[‘value’][‘:time’] = time();
$item[‘operator’] = ‘formula’;
$query->where[1][‘conditions’][3] = $item;
}
}
[/sourcecode] Thanks.

1 Comment

Leave a Reply