Tuesday, 23 June 2015

When must we clear drupal cache

Principle theories


The cache must be cleared when first add a drupal hook function.

Adding or changing menus in hook_menu function

Whenever you add a menu or change the menu in the hook_menu function, it won't take effect immediately, because it's requiring the Drupal cache to be cleared even if you have disabled drupal cache.

First add hook_theme function in a module or a theme

The cache needs to be cleared when you first add the hook_theme function in you code. After that you don't need to clear it when you change the code in the function.

First add hook_form_FORM_ID_alter/hook_form_alter function

The cache needs to be cleared only when you first add the hook function.

Tuesday, 16 June 2015

AWS drupal issue: nginx+php missing client IP address when using reverse proxy

Symptoms of this issue

The most front end presentation for this issue is that you will get an error of "the IP has been blocked because of log-in  failure for too many times".
The source of this issue can be found when you print the result of the function called ip_address() in drupal, cause the only IP address you can get from drupal is 127.0.0.1.

Reason of this issue

At default, drupal gets the IP address from the PHP variable called $_SERVER['REMOTE_ADDR'], but this variable is set to 127.0.0.1 by nginx proxy server. However, a new variable named $_SERVER['HTTP_X_FORWARDED_FOR'] is set as the real IP by nginx reverse proxy if nginx realip module  is installed. And drupal also get the ability to detect this variable by doing some configuration.

Solution of this issue

Go to drupal root folder and open the file named sites/default/settings.php.
Go to the line 404 and you will find the following php code of  $conf['reverse_proxy'] = TRUE; which is commented. What we need to do is to un-comment it.
Jump to a few lines after, the variable definition of $conf['reverse_proxy_addresses'] will be found. We can just add '127.0.0.1' in the array which will be eliminated from client IP addresses.
After the previous configuration, the correct IP address should be received by drupal.


AWS drupal issue: https missing when using ELB

Symptoms of this issue

First of all, some of the js, css, images will be lost when using https as the protocol to visit the website.

If http authentication is used or the grammatically log-in code, like drupal_form_submit('user_login', $form_state);, is used, the user logged-in session, which is in the global $user variable , might be missing in other modules.

Reason of this issue

ELB will terminate the https which will make $_SERVER['HTTPS'] disabled even if we are doing a https request. However, ELB sets another variable $_SERVER['HTTP_X_FORWARDED_PROTO'] to https which we can use to define the right protocol.

Solution of this issue

Go in drupal root folder and open the file named includes/bootstrap.inc.
Go to line 735 and the line with the code of $is_https = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on'; should be found.
Add the code below after the code of line 735:
//to make it compatible with AWS ELB
  $is_https = $is_https ? $is_https :
          isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https';
Here we are, drupal should work well with AWS ELB for https protocol.

Sunday, 14 June 2015

google places autocomplete js api business search doesn't work

If Google Places Autocomplete doesn't show business search results, you may solve the issue by following the checks below.

Check whether google places API has been activated

Go to google console with the URL of https://console.developers.google.com, and check whether google places service is in the list of activated services

Check the whether google client auth is included in the js code

Usually a google url like https://apis.google.com/js/client.js?onload=handleClientLoad will be the html scripts if the google client has been set up in your code.
Compare the APIKey in you js code with that in google console to make sure they are the same

Check the types attribute of Google Places Autocomplete API

When instantiating the google places autocomplete object, the types can be passed as a parameter. So check the types value to see whether it's including 'establishment' type.
The types can also be set up by calling the setTypes method of Autocomplete, so we need to check the types here as well.

After all of the checks and modifications, the business search should work if everything has been done properly.