Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
It would be nice if it was possible not only to specify an options callback for views_handler_filter_in_operator, but also options arguments that are passed to the callback.
In my specific situation, I want the callback to be a reusable caching wrapper around the real callback, like this in my.module:
function my_options_daily_cached_callback($callback) {
$today = date_make_date('now');
$today->setTime(0, 0, 0);
$cache = cache_get($callback . '_daily');
if (!$cache || $cache->created < $today->format('U')) {
$options = call_user_func($callback);
}
else {
$options = $cache->data;
}
return $options;
}
function my_options_callback() {
// expensive SQL query here that can have different results each day
// $sql = ...
// build options from query results
// ...
return $options;
}
In my.views.inc:
function my_views_data_alter(&$items) {
$items['node']['nid_my_options'] = array(
'title' => t('Nid my options'),
'filter' => array(
'field' => 'nid',
'handler' => 'views_handler_filter_in_operator',
'options callback' => 'my_options_daily_cached_callback',
'options arguments' => array('my_options_callback'),
),
);
}
Patch for the 6-3.x branch attached that implements the necessary 'options arguments' handling for the code example above in views_handler_filter_in_operator.
Comment | File | Size | Author |
---|---|---|---|
#11 | 927270-views_filter_in_operator_options_arguments-6.x-2.x.patch | 1.36 KB | cafuego |
#6 | views_927270.patch | 1.52 KB | Cyberwolf |
views_filter_in_operator_options_arguments.patch | 1.11 KB | Cyberwolf | |
Comments
Comment #1
Cyberwolf CreditAttribution: Cyberwolf commentedCode example above was missing a crucial part with cache_set() call:
Comment #2
Cyberwolf CreditAttribution: Cyberwolf commentedComment #3
dawehnerCode looks fine. I'm wondering whether this could use used something in views to reduce a bit of code.
Comment #4
merlinofchaos CreditAttribution: merlinofchaos commentedThis option (and it looks like the 'options callback') needs to be documented in the doxygen for this handler.
Comment #5
Cyberwolf CreditAttribution: Cyberwolf commentedOk, I will take care of that merlinofchaos and will post a new patch soon.
Thanks for the feedback.
Comment #6
Cyberwolf CreditAttribution: Cyberwolf commentedAttaching the new patch. I added class-level documentation, under "Definition items:".
Comment #7
bojanz CreditAttribution: bojanz commentedHow about making this "The function to call in order to generate the value options.", and putting the next sentence (If omitted...) into the next line?
Let's get another opinion and then finish this.
Comment #8
merlinofchaos CreditAttribution: merlinofchaos commentedbojanz' wording correction makes sense to me.
Comment #9
merlinofchaos CreditAttribution: merlinofchaos commentedCommitted to 3.x for D6 and D7 with bojanz' wording change.
Comment #11
cafuego CreditAttribution: cafuego commentedThis is an exceedingly useful addition to help users not have to create callback functions on the fly if they have a lot of different option lists to generate.
Attached patch applies to 6.x-2.x. Only the line numbers have changed.
Comment #12
MustangGB CreditAttribution: MustangGB commented