Theming the autocomplete suggestions (like in the screenshot)
You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules
A frequently asked question is how to get the kind of output in the autocomplete suggestions popup that is shown on the project page in the screenshot.
As far as the setup of the finder goes, it's just a basic Views finder (not a node finder), with only one autocomplete element which searches on the node's title field. The field chosen represents what you want to be prefilled into the textfield, and then submitted to get the results page. So it's good to keep it simple and just use a single field that has a value less than one line of text. Searching multiple fields may cause duplicate nodes - each with a different prefill value, and using long values will be unfriendly for users.
The solution is pretty simple really; theme the title into a node teaser. If you don't know what I mean by 'theme', you need to learn the Drupal theme system first, particularly overriding themeable output.
The original theme function lives in modules/finder_autocomplete/finder_autocomplete.module
<?php
/**
* Theme an autocomplete popup suggestion.
*
* This is a very useful theme function as it lets you pretty much display any
* sort of HTML you want. By default the CSS styles will be really ugly, but
* with a bit of work you can really make this do something special. For
* example - if your autocomplete searches for a value that is unique to
* different nodes (like a node title or sku perhaps) you can assume that each
* match corresponds to a node. If you debug the $option and $element variables,
* you will see that you have enough data there to node_load() or user_load()
* a full object, and display it any way you like, such as teasers (using the
* node_view() function). You can even have pictures and links in the HTML!
* Even if you don't expect unique nodes/users/etc.. there is still a lot of
* potential for adding stylized output here.
* Here is some CSS to get you started on styling something like a node teaser
* in an autocomplete popup suggestion. Note that this CSS is very general and
* may affect other forms on your site - take care to add more specific
* selectors.
*
* <style type='text/css'>
* #autocomplete li.selected {
* background: #eee;
* color: inherit;
* }
* #autocomplete li {
* white-space: normal;
* margin: 0;
* }
* #autocomplete ul.links,
* #autocomplete ul.links li {
* background: none;
* }
* #autocomplete p {
* margin: 0;
* }
* </style>
*
* @param $option
* The result data.
* @param $finder_element
* The finder element object.
*/
function theme_finder_autocomplete_suggestion($option, $finder_element) {
return $option->{$option->display_field};
}
?>
There are some good tips there for CSS, so we'll use those later.
If you'd like to see whats in those variables ($option, and $finder_element) get the Devel module and then add code into the function like dpm($option);. Then you can test the autocomplete out, reload the page, and you'll see a message containing a printout of the variable.
You'll notice one of the array keys in $option is 'nid'. This lets us know which node the record came from. (NOTE: if you can't see 'nid' then you may be using a Node finder when you need to be using a Views finder.) So this is what we can use to get our node teaser:
<?php
function YOURTHEME_finder_autocomplete_suggestion($option, $finder_element) {
$node = node_load($option->nid);
$teaser = node_view($node, TRUE);
return $teaser;
}
?>
You can get more creative with it and output particular stuff from the $node with your own HTML and such, up to you.
Just add that into your theme's template.php and replace YOURTHEME with the name of the theme. Now clear your cache under 'performance', and you'll notice that your autocomplete suggestions will have changed.
But they probably look ugly. The CSS suggested in the comment for the original theme function is what you'll need to tame the teasers a bit.
#autocomplete li.selected {
background: #eee;
color: inherit;
}
#autocomplete li {
white-space: normal;
margin: 0;
}
#autocomplete ul.links,
#autocomplete ul.links li {
background: none;
}
#autocomplete p {
margin: 0;
}
But, you probably won't want to affect every autocomplete on your website, so you can prefix those CSS selectors with something to restrict what you affect. This is pretty easy to determine with Firebug or the Chrome Inspector, if you don't know about those... look them up now and save yourself the headaches.
The finder is usually wrapped in something like "div#finder-page-6-wrapper" which is the 'page' output for the finder with finder_id '6'. A more generic wrapper for the finder is found inside this "div.finder-view-6" which simply denotes output for finder 6. To select a particular element within the finder; use the ID of the div with class "form-item" that wraps the element.
From there it's up to you what to do. But be warned; it isn't easy styling this thing because Firebug and the Chrome Inspector don't work so well on the autocomplete suggestion popup - it keeps disappearing. If that drives you mad, you can find this line in the JavaScript and comment it out: $(popup).fadeOut('fast', function() { $(popup).remove(); }); - the file in which this is located is usually misc/autocomplete.js in Drupal core, or if you've enabled submit upon selection then it will be the following file in the Finder module: modules/finder_autocomplete/finder_autocomplete.js
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion