Hi

Recently google has added autocomplete to there settings i have noticed.
Once i've checked the checkbox they said that i have to regenerate the code
But where can i add that into the module?

this is the generated code:

<div id="cse" style="width: 100%;">Loading</div>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
  google.load('search', '1', {language : 'nl'});
  google.setOnLoadCallback(function() {

    customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
    var options = new google.search.DrawOptions();
    options.setAutoComplete(true);
    customSearchControl.draw('cse', options);
  }, true);
</script>

Comments

bryancasler’s picture

subscribe

marco.1984’s picture

Hi naievepanda,

I have the same issue with Google CSE.
I created a new CSE from scratch with a new ID that I pass to the module, but it still doesnt work.

Anyone for more help please?

michiellucas’s picture

subscribe

robinmcg’s picture

subscribe

healycn’s picture

Subscribing

udvranto’s picture

Subscribing

v8powerage’s picture

I think js should go to google_cse.js but that's not enough, some rewrite of the google_cse.module is needed.

Bahlool’s picture

subscribe

jlea9378’s picture

Version: 6.x-1.2 » 7.x-1.x-dev

Any update on this? How do we get autocomplete to work?

sirish.ayyagari’s picture

any update on this

jlea9378’s picture

any update on this???

dilipvertical’s picture

any updates still ???????

kevinchampion’s picture

Status: Active » Needs review
StatusFileSize
new2.51 KB

Here's a patch to add autocomplete to the search form fields in search-form and search-form-block.

jlea9378’s picture

I installed the patch and cleared my site cache, but I don't see the auto-completion working. Is there something else that needs to be done to enable it or get it working...?

kevinchampion’s picture

Make sure that in your actual Google Custom Search Engine settings that you've enabled autocomplete. Look under "Search Features" in the side menu, and then under the "Autocomplete" tab. If it wasn't set and you set it, you may run into another problem that the Autocomplete results are indexed separately, so it can take 5+ hours for Google to index the Autocomplete before you'll start seeing results.

There is also a potential problem with the way that the jsapi script in included in this patch, namely that there are environmental conditions that can get in the way of Drupal's ability to retrieve the external javascript file. I'm not totally versed on what all those might be, but I think the php setting allow_furl_open needs to be turned on (this would be in your php.ini, or wherever else you set your php settings).

If autocomplete is turned on and your CSE instance is working fine otherwise, I'd then check your browser's console to see if there are any javascript errors. If so, and if they're related to this patch, then the most likely problem is the jsapi script inclusion mentioned above. If that's the problem, then you can try downloading the jsapi script from Google and including it in the module locally.

jlea9378’s picture

Yes, it's enabled on my Search Features and I am successfully able to test it there in the preview of my CSE.

I checked the PHP settings and allow_url_fopen is enabled. I can also see in the browser console that the JS file is being successfully retrieved. However no autocomplete appears when typing in my search box on the web page, whereas typing the exact same thing in the preview of my CSE does cause the autocomplete to appear.

More ideas? My site is www.clatsopcc.edu in case you want to see it.

kevinchampion’s picture

I see 2 problems:

  1. Since you're forcing everything over https, but the jsapi script is included via http, the browser recognizes it as an insecure script and in Chrome it actually restricts the page from using it. You'll notice that if you visit your search page in Firefox, for instance, the autocomplete works: http://o7.no/10YcVUv
  2. The gse module hooks into the #search-block-form form, so the autocomplete is also looking for a form with that id. Your persistent search form that's in the upper-right corner of each page has a different id. So, in order for you to use that form with a different id, you'd want to add your own hook_form_alter to hook into that form and include the autocomplete javascript just like this patch demonstrates for #search-form and #search-block-form

To resolve #1, since Google serves its jsapi over http or https, before including the script it'd need to check the current page's protocol and attempt to retrieve https instead of http. I'm not sure what the best-practice way of doing this is. A quicker fix in your case might be to just manually change the http to https.

Alternatively, you can download the script and include it in the module so that it's loaded locally, which won't have issue with http vs. https. Ideally it would be made into a library so it can be used easily across the site.

jlea9378’s picture

Kevinchampion, thanks for looking at my site. I didn't even realize it was working on the actual "Search" page.

I added code to hook_form_search_form_alter() to attach the JS to google-cse-results-searchbox-form, which is the ID of the form in the upper-right corner of my page. Now both forms on the search page have functioning autocompletes.

However, hook_form_search_form_alter() isn't getting executed on every other page of my site, so the autocomplete doesn't work on the rest of the site. I tried adding a new hook_form_FORM_ID_alter() for the google-cse-results-searchbox-form named "google_cse_form_google_cse_results_searchbox_form_alter()" but that doesn't seem to work. Any idea why? According to the Drupal API I am pretty sure I named the function correctly.

I'm stumped for now. Thanks for any help you can offer!

kevinchampion’s picture

You must not have the correct form id. To figure it out, do an ordinary hook_form_alter and inspect the $form_id variable that's passed in to figure out what the form id is of that search form. You can leave your code inside a conditional statement that checks for the right form id, or move it to a hook_form_FORM_ID_alter statement.

Another note on http/https protocol complications. One common practice is to just leave off the protocol and do something like this:

//www.google.ca/jsapi

If I get a chance, I'll test this and re-roll another patch.

jlea9378’s picture

Ok I had the the form_id right and the method name right but apparently I was too impatient. It took several refreshes and cache clears before my dsm("TEST") message started appearing.

Ok, now my test message appears, so the function is entered, but the if statement is not entered because there is no 'module' id in the $form array. I looked at the dprint_r() of the $form variable and confirmed this to be the case.

This form (block) is the "Google GSE" block that was installed by the module, by the way.

Thanks for your help so far!

kevinchampion’s picture

Thanks for pointing out the block form, I wasn't using it so I had overlooked it. Here's another patch to try.

jlea9378’s picture

Patch on comment #21 works!

jat32’s picture

Thanks, Kevinchampion! Patch on comment #21 worked for me as well.

malcomio’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Category: Support request » Feature request
Issue summary: View changes
Status: Needs review » Needs work

Patch doesn't apply cleanly against 7.x-2.x

afinnarn’s picture

Status: Needs work » Closed (outdated)

I tried re-rolling this patch into 7.x-2.x and it didn't work. Adding to considerations for D8 version.