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 ]:

Authentication: 

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 Apache Solr Configuration

http://www.lullabot.com/blog/article/installing-solr-use-drupal

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 !

 

Resource: 

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';
else
  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.

Using Drush ( Power tool for Drupal ) in Windows XP

At first when I saw drush i thought it’s not that handy as it seems to be. I thought some geek created this just to have fun and feel command line flexibility. But after i dig deeper i found it amazing. If you understand how to use drush you will be amazed by it’s power.

Now what is drush .. drush is a command line shell and Unix scripting interface for Drupal. as stated in here.  The most noteable features of drush from my point of view is :   module enable/disable, update script,  module download, cache clear. Just from command mode i can check latest version of module and dl them and enable them in just simple 3 to 4 commands.

I am not going to briefly explain drush functionality but will try to explain how to run drush in your system in this case Windows XP ( 😛 ) .  Here it goes:

  • Download the latest version of drush from drupal
  • Now open Command prompt and check and set initial settings.
    • Type ‘php’ and press enter if php command is not found then set php.exe path in your Environment
    • Also add the drush path into the environment path
    • By Closing and opening  command prompt again. check by typing ‘drush’
    • If by typing ‘drush’ shows error regarding drush.php open drush.bat file in an editor and  add the abosulute path of drush.php in that bat file.
    • This will fix the drush.php path problem.
  • Let’s play with drush. Open command prompt again and traverse to a drupal installation for example:  ‘cd d:/www/mydrupal’  and then type  ‘drush status’. It will show initial settings of mydrupal installation. If it does not check for settings and environment path.

Drush has some nice extensions. You can play around with them. Here is another simple tip for you. If you study drush you will definately like “drush -v dl ” and ‘drush info” command. “drush info <module_name>” shows particular modules downloadable version and “drush -v dl” can help you to download and copy the file directly into your installation by just 1 single command. But to use ‘drush -v dl’ is little tricky. It uses gzip and tar command to extract the downloaded module file. You can download gzip and tar exe for windows from here.

It seems some ppl did able to use gzip and tar without any hasle but in my case i had trouble with tar. So i hacked into the core of drush and changed the tar command into izarce . If you face problem with tar then go with izarce. It worked for me.

Oops. To use izarce first install the command line version. Then add the izarce location into your environment path. THen goto drush directory and open file ‘drush\commands\pm\package_handler\wget.inc’.  Now find the line which says:

drush_shell_exec(“tar -xf $tarpath -C \”$path\””);

Now replace this with

drush_shell_exec(“izarce -e -d -p”. $path . ” $tarpath”);

It worked for me hope this does for you too. Have a nice drushed life.

[ N.B. At first i tried to use 7zip instead of izarce and gzip+tar but they have some absolute path issue; the solution of which i could not find. So, if you do know of any solution better than this do let me know.

For setting environment   press “windows key+ pausebreak” >> Advanced >> ‘environment variable’ >> now find variable path .. 🙂 enjoy]