Mobile API and Drupal Multisite Bootstrap Issues

27. February 2014 Blog 0

At work I have been working on making my first mobile app with Appcelerator which is a Javascript based tool that ports to Android, IOS, etc… For the most part everything has been good. There are a lack of good tutorials out their, however, there are some sample projects out there so as long as you can look through code and figure out what it going on you should be fine. I personally decided to go with their MVC framework which is called Alloy. If you already understand Backbone.js that will help a lot as it is what is basically used for the Model part of the MVC.

The app is basically for a multi-site system in Drupal so I wrote an API to be the middle man between Drupal and the app. I had issues all along the way trying to get the API to act correctly when boostrapping into Drupal. It seems easy enough but since it was a multisite and sometimes I had to connect to multiple sites in one API call I got some weird results sometimes. After months of fighting with it I finally ran into a problem that I could work around. The main site didn’t have one of the modules installed on it and when I was trying to use the function drupal_write_record() it checks the cached database table schema for the table you are writing a record to. Well it was failing. Even though I was connected to the database for one of the slave sites it didn’t see the table schema for that table. I tried clearing cache, recreating the table schema, and many more things all to no avail. Even went to my trustworthy StackOverflow and asked a question and didn’t even get a comment. Finally after fighting with it for quite a while I tried to think what else would cause it. I finally did a google search for “drupal multisite bootstrap” and found one single page that discussed this. The solution is to tell Drupal which of the sites you are bootstrapping to. Apparently simply selecting the correct database doesn’t do it. So here is the solution:

define('DRUPAL_ROOT', '/path/to/drupal');
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
$variables['url'] = 'http://www.example.com/subsite1';
drupal_override_server_variables($variables);
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

This fixed not only the current problem I was having but many of the other quirky issues I had come across along the way. So hopefully this will save someone from having days of headaches related to this issue.


Leave a Reply

Your email address will not be published. Required fields are marked *