Monday, 14 September 2015

Google autocomplete ambiguous address - same street name in different cities or suburbs

Ambiguous address issue

When entering the address of '29 Karaka Street' in the google autocomplete input, the first hint should be '29 Karaka Street, Auckland, New Zealand', but since there are quite a few streets in NZ having the same street name of 'Karaka', it may cause some issue/misunderstanding of using the location. In addition, when inputting this address in google maps, goold maps will note that 'we couldn't find this address'.

How to solve the issue

Use google maps geocoding API to check whether the address is unique as below:
https://maps.googleapis.com/maps/api/geocode/json?address=29%20karaka%20street&components=country:NZ&key=YOUR_KEY
If there are multiple 'address_components' in the result, it means the address is not unique and we need to display some notes for user such as 'add a suburb or a postcode to make the address more clear'


Wednesday, 2 September 2015

How to fix Theme Developer module not showing caused by the conflict with JQuery

Symptom and the error message from JS console

The theme developer pop-up doesn't show up on the website.
When open Chrome Console, there is an error message saying that 'Uncaught Error: Syntax error, unrecognized expression:
'


Solution

Open the file with path of 'sites/all/modules/devel_themer/devel_themer.js'
Search the following snippet which is the source of the issue:
$(Drupal.settings.thmr_popup)
      .appendTo($('body'));
Use the snippet below to replace the above one:
$($.parseHTML(Drupal.settings.thmr_popup))
       .appendTo($('body'));
Note: The reason of this issue is that JQuery doesn't support putting html string in to the dollar function anymore.

Monday, 31 August 2015

How to use svg logo

The easiest way of using svg logo

Upload the logo manually to the path of 'sites/all/themes/urbansherpa/images/urbansherpa-logo.svg' instead of using Drupal to upload the image to the drupal public folder, cause drupal image doesn't support svg format
Go to Drupal admin panel : admin/appearance/settings/urbansherpa
Untick the check-box of 'Use the default logo' and put the path where the logo was uploaded. In my example it's 'sites/all/themes/urbansherpa/images/urbansherpa-logo.svg'
All done and the svn logo should work as expected.

Trace the Drupal core code to understand the aforementioned steps

Open the file with the path of 'modules/system/system.admin.inc'
Search the function named 'system_theme_settings'
At the bottom of the function, the snippet of adding validate function can be found:
$form['#validate'][] = 'system_theme_settings_validate';
Search the validate function in the same file and at the bottom of this function you will find the magic snippet:
// If the user provided a path for a logo or favicon file, make sure a file
  // exists at that path.
  if (!empty($form_state['values']['logo_path'])) {
    $path = _system_theme_settings_validate_path($form_state['values']['logo_path']);
    if (!$path) {
      form_set_error('logo_path', t('The custom logo path is invalid.'));
    }
  }
  if (!empty($form_state['values']['favicon_path'])) {
    $path = _system_theme_settings_validate_path($form_state['values']['favicon_path']);
    if (!$path) {
      form_set_error('favicon_path', t('The custom favicon path is invalid.'));
    }
  }
From the snippet above, we can realize that it requires a logo path and a favicon path which are existing files.

Sunday, 30 August 2015

Make embeded video responsive

Settings when using 'Video Embed Field' Module

Go to Drupal admin : admin/config/media/vef_video_styles
Click the 'Edit' button of 'Normal'
Change the player width to 100% instead of default value of 640px


How to use CSS flex box in SASS

Add flex mixins to meet the browser compatibilities

@mixin flexbox() {
  display: -webkit-flex;
  display: -moz-flex;
  display: -ms-flex;
  display: flex;
}

@mixin flex($values) {
  -webkit-flex:  $values;
  -moz-flex:  $values;
  -ms-flex:  $values;
  flex:  $values;
}

@mixin flexflow($values){
  -webkit-flex-flow: $values; 
  -moz-flex-flow: $values;    
  -ms-flex-flow: $values;    
  flex-flow: $values;
}

@mixin order($val) {
  -webkit-order: $val;
  -moz-order: $val;    
  -ms-flex-order: $val;    
  order: $val;
}

@mixin alignitems($val){
    -webkit-align-items: $val;
    align-items: $val;
}
Note: these cover most of the frequently used flex css styles, but not all of them

A sample for displaying the images and making the images aligned centered which might not be able to achieve via div or table

.flex-box{
    @include flexbox();
    @include flexflow(row wrap);/* the items will fall down to the next line when the current line is full */
    @include alignitems(centre);/* the items will be aligned centred */
}

.flex-item{
    @include flex(1 15%); /*flex-grow flex-basis*/
}

Improve web page performance in Google PageSpeed

Google PageSpeed, a good tool to test and get advices from

Open the PageSpeed page: https://developers.google.com/speed/pagespeed/insights/ and enter the website URL you want to optimize
You will get a score and some suggestions. There should be some 'should fix' ones and some 'consider fixing' ones, and we should do our best to fix the ones marked as 'should fix'

Use drupal async_js module if there are numerous render-blocking javascript files

Install the module from the website : https://www.drupal.org/project/async_js and enable the module
One way to add the async for loading the js is using the built-in function in this module as below:
async_js_add_js("path/to/your/script.js")
The other way is to add an attribute in the options of drupal_add_js function as the following code:
drupal_add_js("path/to/your/script.js", array("async_js" => TRUE))

Note: some of the js might have dependencies, so this module also support setting the dependencies of the js by adding the attribute of async_dependencies. 


Always try to add the js via drupal_add_js function instead of in the template header

The reason is that when the js is added via the function, you can control where to load the js(in the header or footer) and the weight in the loading process.
Here are some examples of doing it:
//load js in the footer
drupal_add_js('sites/all/themes/ora_theme/js/jquery-1.9.1.js',
            array('type' => 'file', 'scope' => 'footer', 'weight' => 22));
//load js in the header
drupal_add_js('sites/all/themes/ora_theme/js/typekit.js',
            array('type' => 'file', 'scope' => 'header', 'weight' => 21));

Drupal Datatables module installation and usage

Install drupal Datatables plugin

Go to the web page: https://www.drupal.org/project/datatables
Use drupal update manager to install the module
Enable the module

Install JQuery Datatables

Go to JQuery Datatables  website: https://www.datatables.net/
Download the latest version
Unzip the downloaded plug-in and copy it to sites/all/libraries/datatables(datatables is a case-sensitive folder name which should be paid attention to)

Check whether JQuery Datatables was installed properly

Check whether the path of 'sites/all/libraries/datatables/media/js/jquery.dataTables.js' exists or not
Go to Drupal Admin page of 'admin/reports/status' and search 'datatables'. If there is no warning or error, it means the plug-in has been installed properly.

Use the Drupal Datatable module in drupal view

Go to Drupal admin page of admin/structure/views.
Click 'Add a new view' link
In the drop-down list of 'display format', the 'Data Tables' can be found.
Choose 'Data Tables' as the format and add some content for the view
You will see the paginated data table when you open the view on the front-end website