Currently the autocomplete function that allows users to add nodes to a queue either runs the default db_select to obtain matching nodes or, in cases where a third party module is implementing its own nodequeue, it passes to that module.

This patch adds a drupal_alter so that any third party module can modify nodequeue api autocomplete matches.

Example:

function hook_nodequeue_api_autocomplete_alter(&$matches, $context) {
  if ($context['queue']->name == 'my_queue') {
    foreach ($matches as $nid => $title) {
      $matches[$nid] = str_replace('foo', 'bar', $title);
    }
  }
}

And of course there are plenty of other uses where adding or removing matches would be useful (e.g. a permissions module could remove matches based on arbitrary parameters)

p.s. - You could run hook_query_alter() to adjust the match results, but the query is only tagged with "node_access" and "i18n_select", but not "nodequeue"... so it's difficult to isolate the query.

Comments

jaydub’s picture

StatusFileSize
new899 bytes

I made a change to this patch which I agree can be useful when a full implementation of {$queue->owner}_nodequeue_autocomplete() is not needed.

The alter hook should be able to alter not only the results of the default nodequeue module implementation of {$queue->owner}_nodequeue_autocomplete() but also an actual custom implementation of {$queue->owner}_nodequeue_autocomplete().

Hope that makes sense :)

Note this patch is for 7.3 branch. Not sure if 7.2 or 7.3 is where new development is happening.

jaydub’s picture

StatusFileSize
new899 bytes

Here is patch for 7.2 branch

rajiv.singh’s picture

#1: 1463552-autocomplete-alter.patch queued for re-testing.

rajiv.singh’s picture

alexkb’s picture

StatusFileSize
new1.85 KB

I'm struggling to see how I can customise the autocomplete function for nodequeue created nodequeues. That is, where the queue owner passed to nodequeue_api_autocomplete() is equal to "nodequeue" and so a call to $queue->owner . "_nodequeue_autocomplete"; is pointless.

The alter hook should be able to alter not only the results of the default nodequeue module implementation of {$queue->owner}_nodequeue_autocomplete() but also an actual custom implementation of {$queue->owner}_nodequeue_autocomplete().

If this were true, shouldn't a drupal_alter exist instead of the $function($queue, $subqueue, $string, $where, $where_args); so that it doesn't do multiple queries on the database unnecessarily?

It's possible this whole nodequeue_api_autocomplete() function needs a rewrite, as there are references to $where and $where_args that aren't initialized.

Attached is a patch for the 2.x branch of nodequeue, to add a hook call above the queries, to let other modules do their own version of the autocomplete function.

Or, perhaps I've misunderstood how to set the queue owner - I had a look around, but couldn't find a solution. Any pointers on this would be greatly appreciated, and obviously if this is the preferred way to do things, then feel free to ignore this patch.

alexkb’s picture

StatusFileSize
new1.52 KB

Ignore the previous patch, it won't work very well because when module_invoke_all() runs as it resets the index of the returned array, which causes issues for autocomplete.

Instead, I've opted for adding an sql tag to the query so that we can simply do a query_alter to the existing query.

Updated patch attached for 7.x-2.x branch.

steven jones’s picture

Issue summary: View changes
Status: Needs review » Closed (duplicate)
Related issues: +#2231793: Autocomplete does not pass autocomplete text to hook

Going to mark this as a duplicate of #2231793: Autocomplete does not pass autocomplete text to hook because the patch there is doing the same thing, but doing it a little better by using the metadata API on the query object.

Let's get some more reviews over there and get this RTBC