Sunday, 26 July 2015

Install Drupal Amazon S3 file system module (s3fs)

The modules that need to be installed

Drupal libraries: https://www.drupal.org/project/libraries
S3 File System : https://www.drupal.org/project/s3fs
AWS SDK for PHP: https://github.com/aws/aws-sdk-php/releases
Note: 
1. AWS SDK for PHP is a dependency, but you need to pay attention to the version, because the current version is 3, while S3 File System needs the version of 2. So remember to download the right version. 
2. There is also a drupal module trying to install AWS SDK for PHP by composer and the URL is https://www.drupal.org/project/awssdk2. It's not working for me, cause the composer manager module which the awssdk2 module depends on is not working with my drupal. So try to avoid using awssdk2 module to install AWS SDK.

Enable the modules

Put AWS SDK for PHP into the libraries folder and rename the AWS library folder as awssdk2.
Enable S3 File System module.

Check whether it's installed correctly

Go to Admin Pane : admin/config/media/s3fs
Switch to the Actions tab and click the 'Refresh file metadata cache'. If AWS SDK for PHP not found error doesn't show up, it means the installation is successful.

After installing it correctly, you just need to set the bucket name and amazon access keys and s3 will work

Tuesday, 7 July 2015

Drupal form API usage tips

How to set default value for date_popup

In New Zealand we often use 'd/m/Y H:i:s' as our time format, so I set it as the format of my date_popup field. But when I tried to set the default value using the same format as below, it wouldn't work for me:
'#default_value' =>date('d/m/Y H:i:s', strtotime('+1 hour')

The reason is that date_popup requires the default value to use the format of 'Y-m-d H:i:s' no matter what we are using for the date_popup format. Therefore it works well after I changed the default value using the required format as the following:
'#default_value' =>date('Y-m-d H:i:s', strtotime('+1 hour')

Always remember to set default value for the fields

For a simple form(one step form), it's better to pre-populate the fields to make it more user-friendly.
For a wizard form(multiple step form), to make it more convenient for users we need to both pre-populate the fields and keep user-input information when user go back and forth.
Based on the aforementioned reasons, it's required to always remember to set default value for the fields. Otherwise, we might have to set the default values for all the fields all at once which is a quite boring work.


 

Wednesday, 1 July 2015

Drupal Forena Report Module

Install and enable the module

Download and install from this link: http://ftp.drupal.org/files/projects/forena-7.x-4.5.tar.gz
Enable all Forena modules which should be three of them.

Configure forena report

 Go into admin/config/content/forena and set up Forena General settings.

Add data source

Go into admin/config/content/forena/data and add the data source.
Pay attention to the Path field which is the place where the data block files like sql files will be located, and the other fields can be left as default.

Grant permissions of data source

Go in to admin/people/permissions.
Search the data source created at the previous step and grant the permissions for specific roles.
Note: The data source won't show at the steps below if the permissions are not granted to the associated role.

Add sql data blocks

Add your sql query files under the path of the data source referring to the sql files under forena/repos/drupal folder.

Create forena report

Go in to admin/structure/forena and click 'Create New Report'.
Following the steps of 'Create Report' wizard and the new forena report will be created.
After the report created, the page will be redirected to reports/test_report/edit where you can add your data blocks.
Click the 'data' button and select the data block where your sql query resides in.

Add other formats of report such as csv html and email

Go to admin/structure/forena and click the edit link for the report you want to add formats for.
Switch to Document Type tab and the supported formats will show up.
Tick the formats you want to add and click the Update button, and the formats have been added.

Add data tables support for report pagination 

Download the JQuery Datatables plug-in on the website of https://www.datatables.net/
Unzip and copy the plug-in to the folder of 'sites/all/libraries/dataTables'(dataTables is case-sensitive).
Make sure the path named 'sites/all/libraries/dataTables/media/js/jquery.dataTables.js' exists
Go to Drupal status report page and search 'Forena dataTables support'. Make sure there are no warnings and errors

See the pagination results of the reports

Open any of the Forena reports and the pager should be added automatically