Happy new year 2013

Happy new year 2013 everyone. Hopefully this year will be better than last year, we will strive to improve the quality of every aspect of our lives, we can do it.

This is the fifth year for this blog, a certain satisfaction when looking at to the archive pages. Thanks for your supports.

Purge all the comments

Recently, I cleaned all of the comments, too many comments are just spamming without giving the slightest value to the post. Comments should provide additional information or at least add a value to an existing post, and put a link only to support or to add further discussion possibility related to the topic.

Indeed, a lot of valuable comments but I do not want to spend my time just to re-examine previous comments. From now on I use Disqus for blog commenting system.

If you want some kind of signature to promote your blog, then this blog is not the right place, you can try to register on my forum that provides this feature.

Creating responsive Drupal theme (Simplified)

There are various ways to create a responsive Drupal theme, one of them is using template_preprocess_html() function in template.php.

First create template.php if your theme do not already have it. Than add below codes:

/**
* Preprocess html
*/
function themename_preprocess_html(&$vars) {

  drupal_add_css(path_to_theme() . '/css/smartphone.css', array('group' => CSS_THEME, 'media' => 'only screen and (min-width : 240px) and (max-width : 480px)', 'preprocess' => FALSE));

  $meta_viewport = array(
   '#type' => 'html_tag',
   '#tag' => 'meta',
   '#attributes' => array(
     'name' => 'viewport',
     'content' => 'width=device-width, initial-scale=1'
   )
  );

  drupal_add_html_head($meta_viewport, 'viewport');
}

It is clear if the minimum screen size of 240px and 480px maximum it will use smartphone.css, so please create smartphone.css in css folder and you can add your css code there.

Besides using template.php, you can also add the css via theme's info file like below

stylesheets[screen and (min-width: 240px) and (max-width: 480px)][] = css/smartphone.css
stylesheets[screen and (max-width: 320)][] = css/320.css

And do not forget to add meta viewport via template.php or Android devices will not recognize our responsive layout.

Clear your Drupal cache, once you make above changes.

Redirect non www to www or vice versa (main domain, subdomain)

There is a project that requires me to make the following domain redirection:

  • Redirect all users to access the site WITHOUT the 'www.' prefix for all subdomain.
  • Redirect all users to access the site WITH the 'www.' prefix for main domain only.

To fulfill this purpose, I simply add a few lines of code to the htaccess file:

RewriteEngine on

# Redirect the 'www.' version of the subdomains.
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com [NC]
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]

# Redirect the non 'www.' version of the main domain.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

For general use, the following is the code to redirect visitors from non www to www or vice versa.

Redirect all users to access the site WITH the 'www.' prefix:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect all users to access the site WITHOUT the 'www.' prefix:

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]

Creating custom user registration form in Drupal 7

Creating custom user registration form sometimes can be a little tricky, but lets try to create a custom registration form in Drupal 7 with just a few steps.

First we add a few lines of codes to template.php as follows

function themename_theme($existing, $type, $theme, $path){
  $hooks['user_register_form']=array(
    'render element'=>'form',
    'template' =>'templates/user-register',
  );
return $hooks;
}

function themename_preprocess_user_register(&$variables) {
  $variables['form'] = drupal_build_form('user_register_form', user_register_form(array()));
}

Then creating the user-register.tpl.php and place it inside templates folder in your theme, write the following in your user-register.tpl.php:

<?php 
  print render($form['form_id']);
  print render($form['form_build_id']);
  print render($form['account']['name']);
  print render($form['account']['mail']);
  print render($form['field_yourcustomfield']); // Your Drupal 7 profile field if any.
  print drupal_render($form['actions']); 
?>

Clear your Drupal cache and visit your user registration form to see the changes.

Send data and read JSON string from URL (HTTP Request)

Some time ago I need to creating a Webform where visitors need to fill in the data required before they can download a specific file.

So the order is as follows:

  1. Send data to the server such as name, email, company, etc. (site A and site B has different server).
  2. Read the string given by the server in the form of JSON. The given string is a place where we can download the file.

Examples of JSON string:

{"success":true,"downloadUrl":"http://www.example.com/webfiles/example.exe"}

As usual, I use Drupal, and using the Webform module.

To be much faster, I just changed the webform-confirmation.tpl.php and named according to the ID of the webform (eg webform-confirmation-1.tpl.php) and put it in the theme folder that is used. Here is the code:

$submission_data = webform_menu_submission_load($sid, $node->nid);
 $url = 'http://www.example.com/download.php?';
 $data = array (
  'manager' => $submission_data->data[1]['value'][0],
  'email' => $submission_data->data[2]['value'][0],
  'company' => $submission_data->data[3]['value'][0],
  'country' => $submission_data->data[4]['value'][0],
  'model' => $submission_data->data[5]['value'][0],
  'feature' => $submission_data->data[6]['value'][0],
  'srlnbr' => $submission_data->data[7]['value'][0],
  'prodid' => $submission_data->data[8]['value'][0]
 );
 $query = http_build_query($data, '', '&');
 $f_url = $url . $query;
 $content = file_get_contents($f_url);
 $json = json_decode($content);
 $dl = $json->{'downloadUrl'};
 if ($dl == '') {
  // null
 }
 else {
  header('Refresh: 3; URL=' . $dl);
 }

With the solution above, we do not need to use JavaScript / jQuery. We could also use drupal_http_request(); but more effective using above methods because I need to read the JSON string also.

SEOzoic: The Web and Business Directory

After several months of trying to find the right format for one of my projects I finally officially released a new service that is web and business directory named SEOzoic.

This is paid directory with an annual recurring charge, my expectations with that model it can minimizing the spam and to maintain the quality of the listing itself.

Currently there are already several companies, organizations and individuals listed there. Visit the directory here.