Comments

jweowu’s picture

Status: Active » Needs review
StatusFileSize
new4.07 KB
jweowu’s picture

I found that it was quite difficult to observe the DOM and CSS changes that the autocomplete widget inserts, so here's some documentation of this. It could potentially be included in the module.


/*
Styles for the Google auto-completion table are provided by default.css from google. The selectors used are:

  .gsc-completion-container
  .gsc-completion-selected

They can be over-ridden by making the styles !important, or the selector more precise. The following selectors describe the markup that is inserted:

body > table.gsc-completion-container {
  styling for the table which contains the completions.
  top, left, and width styles are all inserted inline into the table element's style attribute.
}
.gsc-completion-container > tbody > tr > td {
  styling for a suggested query
}
.gsc-completion-container > tbody > tr > td > strong {
  auto-completed text (i.e. the bits that the user did NOT type)
}
.gsc-completion-container > tbody > tr > td.gsc-completion-selected,
#csc-completion-selected {
  styling for a suggested query which the user has moused-over
}
*/
body .gsc-completion-selected {
  background: #D5E2FF;
}
body .gsc-completion-container {
  font-family: Arial, sans-serif;
  font-size: 13px;
  position: absolute;
  background: white;
  border: 1px solid #666666;
  margin-left: 0;
  margin-right: 0;
  /* The top, left, and width are set in JavaScript. */
}
meba’s picture

Status: Needs review » Needs work
+      $variables['scripts'] .= theme('google_cse_adv_autocompletion_script');

I have seen several times (including myself) that we needed to drupal_add_js in preprocess page, which is obviously too late. We solved that (and there is at least one blogpost about it) by doing:

drupal_add_js('...');
$vars['scripts'] = drupal_get_js();

This will break your patch. Can we theme the script, wrap it in

(which is already done) and add it as inline using drupal_add_Js so it's in the registry?
jweowu’s picture

Frustratingly you cannot use drupal_add_js() to generate <script src="http://www.google.com/jsapi" type="text/javascript"></script>. Its data can either be inline javascript, or a relative path from the Drupal site's base URL. External URLs are not supported at all, which is a bit crap.

The recommended workaround appears to be using drupal_set_html_head() instead, and that should be fail-safe, but that's also really annoying if you want all the javascript output at the end of the page.

This is why the patch implements both approaches, and provides an option. I've documented the issue in the code, and indicated how to deal with theme issues if you wish to use the theme option and you are also regenerating the $scripts variable.

If there's a genuine solution to this other than patching drupal_add_js(), I'd love to hear it.

The second script could be added inline with drupal_add_js(), but given this first issue, I didn't see any point in treating the two in different ways.

meba’s picture

I have used something like this in past:

    drupal_add_js('document.write(unescape("%3Cscript type=\"text/javascript\" src=\"http://maps.google.com/maps?file=api&amp;v=0&amp;key=ATg&amp;hl=en\"%3E%3C/script%3E"));', 'inline');
jweowu’s picture

Oh, nice. That's a much better hack :)

Will do.

jweowu’s picture

StatusFileSize
new5.54 KB

New and improved.

The AJAX API loader URL looks pretty set in stone, so I didn't include that in the theme function. People may wish to add their Google API key, though, so I added a setting for that.

I also added that CSS documentation into the README file.

jweowu’s picture

Status: Needs work » Needs review
meba’s picture

I committed this and then realized I can't get this working. First of all, the autocomplete is inserted in search results only, not in all forms anywhere. That is intentional? Secondly, even there, no autocomplete shows - is there anything that needs to be done at google administration?

jweowu’s picture

Yes, you need to enable it at the Google end as well, and then wait at least several hours before Google will start to return autocompletion suggestions.

Use the Google search interface as a test bed. Once autocompletions are appearing there, you should be getting them for the same terms from Drupal.

It was intentional inserting it into the google_cse_adv_search_view() function. I did wonder whether it might be desirable in the search block as well. It shouldn't be hard to make that change, provided that the widget plays nice when attached to multiple forms on a single page.

Ah... now that you say it, though... I guess it's not currently activated when you go to '/search/google_cse_adv' with no search keys? I think that page is still served by the original search_view(). If so, that was a genuine blunder. We currently have a weird bug where that path is timing out to a server error (not related to this module AFAIK) which I haven't gotten around to investigating, so I wouldn't have tested that page.

Although maybe the more appropriate fix there would be to add a hook_menu entry for '/search/google_cse_adv'? Writing this now, it does seem odd having that one case handled by a different function.

(n.b. I can't verify any of this right now, so apologies if I have it wrong.)

jweowu’s picture

Version: 6.x-1.1 » 6.x-1.2
StatusFileSize
new6.62 KB

I've updated this to resolve the issues. It now works at the form generation stage (hook_form_alter, #after_build), and can be enabled for any or all of the standard search forms (search_form, search_theme_form, search_block_form).

I've changed the autocompletion settings variable type in the process, so if you still have that value in your database from the previous patch, you'll see an error on the settings form as it tries to use the stored non-array value as an array. Just submit the form to fix that up.

jweowu’s picture

StatusFileSize
new6.72 KB

Added an informative link to the label of the AJAX API key setting

jweowu’s picture

StatusFileSize
new10.39 KB

Added the README changes back

jweowu’s picture

StatusFileSize
new10.39 KB

Last one, I think :) Just tweaking some text to make the warning that you need to enable this at the Google end as well a little more visible than it was before.

jweowu’s picture

StatusFileSize
new10.39 KB

Trivial change of a variable name to something more appropriate.

jweowu’s picture

StatusFileSize
new9.27 KB

Hmm... I see something went amiss with my attempt to revert the README changes!

I also thought of another problem/solution: the Google AJAX API Loader may already be present by the time we go to load it (for example, if another module had used it to load up the Google Maps API), so now the javascript will only write that script tag if there is no existing google.loader object.

If google.loader doesn't exist, we definitely need it because we're going to use it immediately. If some other module wants to load the AJAX API Loader again after we do, then they should either be doing the same thing (checking for it first) to avoid unnecessary duplication, or else the site admin can change the module weightings so that the other code runs before ours.

I've also changed a persistent variable name from google_cse_adv_google_api_key to the more specific google_cse_adv_ajax_api_key

The Computer Audiophile’s picture

This may sound elementary but how do I implement the 923694-9 patch into release 6.x-1.2?

jweowu’s picture

StatusFileSize
new15.55 KB

For general information on patches, see http://drupal.org/patch

The short version is that you could use something like this from within the module's directory:
patch -p0 <google_cse_adv-923694-9_0.patch

I most certainly recommend learning about patching so that you know how to deal with them in future, but I'm also attaching a pre-patched version so that you can provide some test feedback regardless.

Take note of the comments from #10:

Yes, you need to enable it at the Google end as well, and then wait at least several hours before Google will start to return autocompletion suggestions.

Use the Google search interface as a test bed. Once autocompletions are appearing there, you should be getting them for the same terms from Drupal.