Drupal Module Review: Services

URL :    Services
Sample Gist:  Request

Client wants to create a mobile application, now asks for JSON endpoints to fetch datas from your Drupal website. The easiest route has always been to create a menu route and a callback. Easy to work and we have control. In this setup things gets complicated when:

  • All these requests can only be accessed by logged in user.
  • Coding gets messy: Like you want to through an output of a view. In some harsh setup we had to get views result set then manually handled output. There are few supportive modules like views_data_export.
  • Need more fine grained control based on permission.
  • Today its json later they might ask for SOAP layer.

A scratch ( i call this) solution may seem attractive and faster but in a large system it all depends on the architecture you set up.

Services module provides robust base for mobile application to website communication. It provides multiple interfaces : XML, JSON etc. It ships with few helpful resources: user, node, views etc. Then you can create custom resource that can be exposed via services.

Here are few lessons I learnt during integration [ More to come ]:


I used session authentication. Services module has a huge handbook that covers in details. But it took some time for me get hang on authentication. Here is gist that based on guzzle which shows sample request to drupal7 services.

Drupal 7 Migrate

Every now and then we came across projects that requires migration. The old website could be a WordPress, CodeIgniter or some other database driven application. Usually we do such migrations using Custom module with batch processing. In last project in Drupal 7 i decided to give a shot to migrate module. After few trial and error i found this to be ultimate solution to migration. I tried with csv data and mysql table; both solution took 25% time to solve compare to my old custom module solution.  I have also seen couple posts that explains how to migrate html raw files.

You can learn more about migrate module from their documentation, their sample migrate_example module also has tons of examples. Do look at migrate_extras that provides support for additional contributed modules.

Here i am collecting some obstacles that i faced and their solution.

I am migrating contents from old website but want to keep their url alias due to SEO factor. I have pathauto module setup but don’t want to trigger that.

// Block pathauto from interfering. This requires migrate_extras
// Lets Put new Path
$this->addFieldMapping('path', 'old_path');

After i migrate body content it seems full_html text format is not placed. Drupal is splitting the html code as plain text. Solution:

$this->addFieldMapping('body', 'content');  
$this->addFieldMapping('body:format')->defaultValue('full_html'); // You can set other formats

In that old system there was this type which is a select option with value : On and off . In D7 i have set this as  a Term Reference field. I had to use prepareRow to map select value to tid.

class MyNodeMigration extends Migration{

 public function __construct( $arguments )
   #.. Other Code 
   $this->addFieldMapping('field_status', 'status');
   #.. Other code

 public function prepareRow($row)
   $row->status = $row->status === 'On' ? 12 : 13;

I am migrating over 65,000 Contents. All my trials were less than 100 entries and i was happy to see the throughput which was over 800/min. Then I start a full run and things start to look horrific. At some point i am getting throughput of 2/min which means to finish all content migration will take 541 Hours . Impossible! . After few tinkering i re-run the test and i able to migrate 11000 posts within 15minutes.  Awesome!

$query = db_select('tbl_pages', 'page');
  #.. Other codes
  #.. Other code
 $this->highwaterField = array(
     'name' => 'id',
     'alias' => 'page',
     'type' => 'int', );

In my case, i had to skip particular rows based on some logic which i could not able to ignore in MySQL. You can put such logics inside prepareRow function.

public function prepareRow($row){
   // This will skip this row 
   if( $row->type == 'blah' and $row->some == 'foo' )  return FALSE; 

Make your terminal cool

Screen Shot 2014-07-03 at 4.01.45 PM


You can achieve this terminal look easily with few quick steps. In my case, I am using iterm2 in osx mavericks along with zsh powered by oh-my-zsh. I do know that its possible in bash too. My recipe does works in Mint, Ubutu too. Not sure of Fedora :( .. So lets begin.

Installing zsh and make it default shell

Install zsh using yum, brew or apt-get. In case of ubuntu run :    $sudo apt-get install zsh   . It will  ask for your password and finally it should complete its setup. Then we set zsh as default shell by running : $chsh -s /bin/zsh . To see the effect you have to logout then login. But dont logout now. As running zsh first time ; it asks to setup zshrc file for you. We want to logout after configuring our zshrc first.

Configuring oh-my-zsh

Lets quickly run few more commands. Dont blindly run commands try to understand each command .

$git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
$mkdir -p ~/.bin/dotfiles/zsh/
$cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.bin/dotfiles/zsh/zshrc
$ln -s ~/.bin/dotfiles/zsh/zshrc  ~/.zshrc

By running all this we are setting our default zshrc file. Now you can logout and login again to find zsh as your default shell.

Installing ducknorris theme and Powerline fonts

You are so close. We have to configure ducknorris theme and powerline fonts. Download powerline fonts from here and configure one of the fonts in your terminal profile preference. Then create ducknorris.zsh-theme file inside ~/.bin/dotfiles/zsh/ducknorris.zsh-theme  .  Copy the theme content from that website and paste it into the file. Now run following command :

$ln -s ~/.bin/dotfiles/zsh/ducknorris.zsh-theme ~/.oh-my-zsh/themes/

Edit ~/.bin/dotfiles/zsh/zshrc file and change replace ZSH_THEME setting to following :




Reopen terminal to see your cool new terminal look. I usually keep all my dotfiles inside ~/.bin/dotfiles and sync them to a private repository in bitbucket. You can do that too. Have Fun !!

Drupal 7 Apache Solr Configuration


Thanks to lullabot for this nice post which produces exact output that it says. In my case i had to make few other changes in my CENTOS cloud server.


1.  Instead of recommended version of drupal apachesolr module; I took the dev version for solr configuration. 

2. You wont be able to access http://localhost:8983  

The reason behind this is firewall if you have iptables enabled . Open /etc/sysconfig/iptables and add following line:

# Tomcat Port
-A INPUT -p tcp --dport 8983 -j ACCEPT

Then restart iptables #/etc/init.d/iptables restart .  Now you can access tomcat server.

3. That article didnt speak much about security.  VISIT I found this post to secure SOLR admin panel. There is truncated tags in that XML . See the github gist  comment to fix this. 

4.  In drupal admin level use http://username:password@localhost:8983/solr/drupal 

5. Setup Tomcat to run on startup . This post of rackspace is useful. I did have to tweak few lines in that shell. I removed java path variables and put correct path to tomcat startup and shutdown shell.


Finally  configure the basic settings for solr inside drupal admin. I used drush solr-index  to index my contents. Thats it , Enjoy SOLR !



https://drupal.org/node/1333076   This post discusses above security and configuration related to admin.


Admin_views Module with multiple user

Module URL:  admin_views

We use this module to provide nice filtering features for our client. In few cases we share create different roles based on client request. Such as, This particular user can only edit his contents or specific content types. A good feature of admin_views is that it does considers user permission and renders admin links based on that. But one problem is that if the site has huge list of contents; its hard to find a single content from a list of thousand items.

To make this user intuitive, we want to show only user contents in Content Management but for specific roles we will show them all. To do that follow these steps:

1. Go to Views and open edit form for “Administration: Nodes” .  Admin_views module utilises this module.

2. Add a contextual Filter Author: uid  . Click on “Provide Default Value” and choose PHP code. Note: If you dont see php code in here then enable PHP filter module from Modules.

3. Now put following snippet in the “PHP contextual filter code” :

global $user;

if( in_array( 'administrator', $user->roles) )
  return 'all';
  return $user->uid;

4. Finally Save the views .

Thats it . Now only administrator can view entire list but for other roles only authored contents will be visible to logged in user.

VIM really enhanced

I am a regular SublimeText user. Vim has always been my favourite editor of choice when i need to do small tweaks and etc. I kept an eye on recent vim plugins and posts made by numerous vim lovers. The ultimate goal was to make vim more easier and more friendly. Here are my tips and tricks that i used to make vim close to Sublime :

  1. Vundle : I have used pathogen to manage my plugins. Vundle takes plugins management to a whole new level. Think of it as Sublime Package manager. Just put the github repo name and run “BundleInstall” . Before you do this make sure your vim supports ruby and install Vundle in your system. To check if your system has ruby support run :
    $vim –version | grep ruby
    If nothing comes up then make sure you update your vim with ruby support.
  2. Vim-airline: Makes vim status and tab bar intuitive. Some cool themes for eyecandy.
  3. Vim-unimpaired : Buffer is a cool way to edit multiple files in vim. But switching them can be hectic. [b  a nice key map to switch between buffers.
  4. ctrlP : NerdTree provides a cool file explorer but nothing can beat the fuzzy search to load specific file. Those who have used it never looked back. This plugin provides the exact same feature in vim.
  5. vim-multiple-cursor : First saw this feature in Notepad++ . A very handy way to refactor code lines. Better than typical Search/Replace.


Finally there are tons of dotfiles around . Dont copy and paste those into your vimrc. I used to do that which does not helps you to learn. Instead pick one dotfile then try to understand each lines. Only move those that you think will help you eventually.

My vimrc file. :)


Lots of complains adopting git, is not to have a server that support ssh let alone git. A nice post by jefferey way in nettuts suggests deployhq. Thats a nice service with nice GUI . But i was looking for a free solution and found one under a comment in that post.


This not only provides FTP protocol also support SFTP. Here are the steps that i took in my mac os environment.

  • I used brew to install git-ftp:
    $brew install git-ftp
  • This relies on libcurl so next thing i had to install curl with ssh support :
    $brew install curl –with-ssh
  • Brew does not overrides default curl library that comes with osx,  so i had to replace that using
    $sudo rm /usr/bin/curl
    $sudo ln -s /usr/local/Cellar/curl/7.35.0/bin/curl /usr/bin/curl
  • Now I cd into my repository and run :
    $git ftp init -u <username> -p – sftp://<host_url>/<path>
  • It syncs my current HEAD branch into the server and keeps track of last commit. So after making few other commits, i am ready to push latest changes to server. By running:
    $git ftp push -u <username> -p – sftp://<host_url>/<path>

I hope from now on you will use GIT for server deployment.

NOTE: git-ftp stores commit log in a file .git-ftp.log file. If any mishaps happens or you want to re-sync everything. Remove that file and you are good to go.