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

Thursday, 13 August 2015

Maxlength module used to set a limit and a counter for text related field

Install and enable the module

Go to the website and find the most recent stable version
Install and enable the module

Set the character limit for the text field of a node type which contains any content type, account type, etc.

Go the admin page of admin/structure/types
Click on the link of 'manage field' in the row of the content type containing the fields that needs to be set
Click the 'edit' button of the field that needs to be set a character limit on
Search 'Maxlength JS' and the input fields associated with the Maxlength module will be spotted
Set the value of the fields related to Maxlength module and it will take effect when you create a content of this content type

Set the character limit for the text field in the custom module using drupal form API

It's freaking easy to do it with just adding the #maxlength and #maxlength_js attribute for the form field. Here is an example:
$form['details'] = array(
      '#weight' => '2',
      '#required' => '0',
      '#type' => 'textarea',
      '#cols' => 60,
      '#rows' => 5,
      '#maxlength_js' => TRUE,
      '#maxlength' => 250,
      '#default_value' => isset($form_state['values']['futher_instructions']) ? $form_state['values']['futher_instructions'] : '',
      '#title' => t('Further Instructions'),
    );
In the example, the max length is set to be 250 characters and the maxlength_js is set to be true which means the Maxlength module has been enabled for this field.
Note: Both maxlength and maxlength_js have to be set to enable Maxlength module for the field.

Monday, 10 August 2015

Field Permission Moudle used to set the permission of any field of both the entity type and the account settings

Install and enable the module

Go to the module URL: https://www.drupal.org/project/field_permissions
Install and enable the module
Here is an example of using this module to set the permissions of a field of the account settings 

Set the permission of the field

Go to the Admin pane: admin/config/people/accounts
Switch to the Manage Fields tab
Click the edit link of the field that needs to be set permission for
Scroll down to the Field Settings part and choose Custom permissions radio button
The permission settings table will show up where we can set the permissions for the field

Check front page to see the effect

Use a front-end user to log in
Click the View tab of the profile page and the fields which is not empty and having the read permission will be displayed
Switch the Edit tab of the profile page and the fields which having the write permission will be editable, while the fields which having the read permission will show up with a read-only status