Click here to Skip to main content
13,250,182 members (69,413 online)
Click here to Skip to main content
Add your own
alternative version

Stats

23.1K views
165 downloads
5 bookmarked
Posted 19 Feb 2013

Drupal with Android integration: make posts and upload photos. Part I - Drupal.

, 19 Feb 2013
Rate this:
Please Sign up or sign in to vote.
Drupal is powerful and popular web CMS, with many useful features and hundreds of different modules that can extend core functionality. Android is powerful, popular and very fast growing mobile platform. It's leading smartphone and tablets platform. In this article i'll show you how to integrate Dru
This is an old version of the currently published technical blog.

Drupal is powerful and popular web CMS, with many useful features and hundreds of different modules that can extend core functionality. Android is powerful, popular and very fast growing mobile platform. It's leading smartphone and tablets platform. In this article i'll show you how to integrate Drupal with Android powered mobile devices, to authorize on site and to perform some actions. 
If you are interested in development mobile applications, that can backup or store files to a website, upload photos, make posts, administer site, or perform other remote actions - this article is for you. Here you can find guidelines and principles description of how mobile and web integration can be made. 
Example in this article will be a practical task: post pages and upload photos from Android device to Drupal website. In example, i show how to use it with Drupal 6, but all is also applicable to Drupal 7 (only minor modifications of Drupal module are required). This article show how to integrate Drupal with Android OS. If you need another platform, it can be useful for you too, as it describes principles and issues of communication. 

How it will be done?

Our task is to perform actions on Drupal website, and we need a method to communicate with it.
We will use XML-RPC protocol for communication between Android and Drupal. It's lightweight remote procedure call protocol, and you don't need any additional knowledge, as with SOAP. Also, it's not require  interface definitions, as with SOAP.

To use it, we need services module (http://drupal.org/project/services).

1. Services module
This module allows to create services within Drupal modules. A service exposes a set of methods, that can be called remotely by third party applications. Each method has defined name, parameters and return type. In our case they will be called from Android-powered mobile device.

Services module supports many communication protocols. REST, XMLRPC, JSON, JSON-RPC, SOAP, AMF are supported. 

In this example we will use XMLRPC.
2. Configuring services module
Services module is not included in Drupal core. So, you need to download it from http://drupal.org/project/services. Unpack it into your Drupal website modules subdirectory (sites/all/modules), and enable it in modules administration page.
If all is fine, you will see it in modules admin page.

Next, you need to define the service endpoint. Open the services admin page of your site, and add "android" endpoint:
Then click "Add" link:
And then, enter endpoint's parameters:  machine-readable name, server type and endpoint path. 
Session authentication must be checked. If it's unchecked, all rpc calls will be executed as anonymous user.
Example is shown below:
Next, open resources tab. And select resouces, that will be available through this endpoint.
Resources are nodes, comments, files, users, etc. You need to enable user login and logout, and node create and delete. You can enable more resouces. For example: to allow users registration from mobile, to add/remove comments and etc. But, it's good practice to allow only that's really needed. (For example, if you use captcha for users registration to block bots, enabling user.register method through services will allow them to avoid captcha).
Now, services module is configured.
3. Drupal module
The purpose of our module is to get uploads from Android devices and attach them to pages. 
We call our module "photoupload". Create subdirectory photoupload in sites/all/modules directory of Drupal. Now we create photoupload.infoSet up mandatory fields:

name = Photo upload
description = Module that manages photo uploads from Android devices

core = 6.x
version = "6.x-1.0"
project = "photoupload"
Now, create module file photoupload.module
Begin with php opening tag.
<?php
First, our module will define "upload photos" permission. Only users with this permission are allowed to upload photos.

<span style="color: rgb(34, 34, 34);">function photoupload_perm() </span>
<span style="color: rgb(34, 34, 34);">{</span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span"> </span>return array('upload photos');</span>
<span style="color: rgb(34, 34, 34);">}</span>
Next, we need to define menu item for uploads. Call it "photoupload". Menu item is needed to handle urls, so it will be hidden (callback). Upload is made through form, so define page callback to be drupal_get_form, page arguments as array containing name of function to create form. Access arguments is array containing permission, required for access to menu item. In this case, it will an array with "upload photos" element.

<span style="color: rgb(34, 34, 34);">function photoupload_menu() </span>

<span style="color: rgb(34, 34, 34);">{</span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span"> </span>$items = array();</span>

<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span"> </span>// mobile photo upload</span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span"> </span>$items['photoupload'] = array(</span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span">  </span>'title' => 'Mobile photo upload',</span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span">  </span>'description' => 'Upload photo',</span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span">  </span>// setup page callback to return form</span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span">  </span>'page callback' => 'drupal_get_form',</span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span">  </span>// form function</span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span">  </span>'page arguments' => array('photoupload_upload_file'),</span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span">  </span>// users with 'upload photos' permission is only allowed</span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span">  </span>'access arguments' => array('upload photos'),</span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span">  </span>// hide it from menus, available to users</span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span">  </span>'type' => MENU_CALLBACK, </span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span"> </span>);</span>
<span style="color: rgb(34, 34, 34);"><span style="white-space: pre;" class="Apple-tab-span"> </span>return $items;</span>
<span style="color: rgb(34, 34, 34);">}</span>
Now, we create form for upload. It will contain image file and page node identifier elements.

function photoupload_upload_file($form_state)

{
<span style="white-space: pre;" class="Apple-tab-span"> </span>// prepare the file upload form
<span style="white-space: pre;" class="Apple-tab-span"> </span>// set multipart/form-data encoding type
<span style="white-space: pre;" class="Apple-tab-span"> </span>$form = array('#attributes' => array('enctype' => 'multipart/form-data'));

<span style="white-space: pre;" class="Apple-tab-span"> </span>// image file selector
<span style="white-space: pre;" class="Apple-tab-span"> </span>$form['image'] = array(
<span style="white-space: pre;" class="Apple-tab-span"> </span>    '#type' => 'file',
<span style="white-space: pre;" class="Apple-tab-span"> </span>    '#title' => 'Upload photo',
<span style="white-space: pre;" class="Apple-tab-span"> </span>    '#description' => t('Pick a image file to upload')
<span style="white-space: pre;" class="Apple-tab-span"> </span>);

<span style="white-space: pre;" class="Apple-tab-span"> </span>// page node identifier. The page to image be attached
<span style="white-space: pre;" class="Apple-tab-span"> </span>$form['nid'] = array(
<span style="white-space: pre;" class="Apple-tab-span">  </span>'#type' => 'textfield',
<span style="white-space: pre;" class="Apple-tab-span">  </span>'#title' => 'Page nid',
<span style="white-space: pre;" class="Apple-tab-span"> </span>);

<span style="white-space: pre;" class="Apple-tab-span"> </span>$form['#token'] = FALSE;
<span style="white-space: pre;" class="Apple-tab-span"> </span>
<span style="white-space: pre;" class="Apple-tab-span"> </span>// submit button
<span style="white-space: pre;" class="Apple-tab-span"> </span>$form['submit'] = array('#type' => 'submit', '#value' => 'Upload');
<span style="white-space: pre;" class="Apple-tab-span"> </span>return $form;
}
Create form submission handler.

function photoupload_upload_file_submit($form, &$form_state)
{
<span style="white-space: pre;" class="Apple-tab-span"> </span>$dir = file_directory_path();
<span style="white-space: pre;" class="Apple-tab-span"> </span>
<span style="white-space: pre;" class="Apple-tab-span"> </span>// unlike form submissions, multipart form submissions are not in
<span style="white-space: pre;" class="Apple-tab-span"> </span>// $form_state, but rather in $FILES, which requires more checking
<span style="white-space: pre;" class="Apple-tab-span"> </span>//

<span style="white-space: pre;" class="Apple-tab-span"> </span>if (!isset($_FILES) || empty($_FILES) || $_FILES['files']['size']['image'] == 0) {
    <span style="white-space: pre;" class="Apple-tab-span">  </span>drupal_set_message("Your file doesn't appear to be here.");
<span style="white-space: pre;" class="Apple-tab-span">  </span>return ;
<span style="white-space: pre;" class="Apple-tab-span"> </span>}

<span style="white-space: pre;" class="Apple-tab-span"> </span>$name = $_FILES['files']['name']['image'];
<span style="white-space: pre;" class="Apple-tab-span"> </span>$size = $_FILES['files']['size']['image'];
<span style="white-space: pre;" class="Apple-tab-span"> </span>$type = $_FILES['files']['type']['image'];

<span style="white-space: pre;" class="Apple-tab-span"> </span>// get page nid
<span style="white-space: pre;" class="Apple-tab-span"> </span>$nid = $form_state['values']['nid'];

<span style="white-space: pre;" class="Apple-tab-span"> </span>// this is the actual place where we store the file
<span style="white-space: pre;" class="Apple-tab-span"> </span>$file = file_save_upload('image', array() , $dir);
<span style="white-space: pre;" class="Apple-tab-span"> </span>if ($file) {
<span style="white-space: pre;" class="Apple-tab-span">  </span>$filename = $dir."/".$file->filename;

<span style="white-space: pre;" class="Apple-tab-span">  </span>$import = _photoupload_attach_photo($filename, $nid);
<span style="white-space: pre;" class="Apple-tab-span">  </span>drupal_set_message($import); 
<span style="white-space: pre;" class="Apple-tab-span"> </span>}
<span style="white-space: pre;" class="Apple-tab-span"> </span>else {
<span style="white-space: pre;" class="Apple-tab-span"> </span>    drupal_set_message("Something went wrong saving your file.");
<span style="white-space: pre;" class="Apple-tab-span"> </span>}
}
Add function to attach photo to page.

function _photoupload_attach_photo($file, $nid)
{
<span style="white-space: pre;" class="Apple-tab-span"> </span>global $user;

<span style="white-space: pre;" class="Apple-tab-span"> </span>// load node by nid
<span style="white-space: pre;" class="Apple-tab-span"> </span>$node = node_load($nid);

<span style="white-space: pre;" class="Apple-tab-span"> </span>// create file object
<span style="white-space: pre;" class="Apple-tab-span"> </span>$name = basename($file);

<span style="white-space: pre;" class="Apple-tab-span"> </span>$file_obj = new stdClass();
<span style="white-space: pre;" class="Apple-tab-span"> </span>$file_obj->filename = $name;
<span style="white-space: pre;" class="Apple-tab-span"> </span>$file_obj->filepath = $file;
<span style="white-space: pre;" class="Apple-tab-span"> </span>$file_obj->filemime = file_get_mimetype($name);
<span style="white-space: pre;" class="Apple-tab-span"> </span>$file_obj->filesize = $stats['size'];
<span style="white-space: pre;" class="Apple-tab-span"> </span>$file_obj->filesource = $file;
<span style="white-space: pre;" class="Apple-tab-span"> </span>$file_obj->status = FILE_STATUS_TEMPORARY;
<span style="white-space: pre;" class="Apple-tab-span"> </span>$file_obj->timestamp = time();
<span style="white-space: pre;" class="Apple-tab-span"> </span>$file_obj->list = 1;
<span style="white-space: pre;" class="Apple-tab-span"> </span>$file_obj->new = true;

<span style="white-space: pre;" class="Apple-tab-span"> </span>// write it
<span style="white-space: pre;" class="Apple-tab-span"> </span>drupal_write_record('files', $file_obj);

<span style="white-space: pre;" class="Apple-tab-span"> </span>file_set_status($file_obj, 1);
<span style="white-space: pre;" class="Apple-tab-span"> </span>// attach file to node

<span style="white-space: pre;" class="Apple-tab-span"> </span>$node->files[$file_obj->fid] = $file_obj;

<span style="white-space: pre;" class="Apple-tab-span"> </span>// and finally, save node
<span style="white-space: pre;" class="Apple-tab-span"> </span>node_save($node);
<span style="white-space: pre;" class="Apple-tab-span"> </span>return "OK";
}
And php closing tag.

?>

We finished with Drupal's module code.

Now, you need enable it in modules administration page.

It's all done with Drupal part. In the next part of article i'll describe how to implement Android application.
Drupal with Android integration: make posts and upload photos. Part II - Android.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Andrey Moskvichev
CEO BitGriff LLC
Russian Federation Russian Federation
My name is Andrey Moskvichev.

I'm a software developer with more than 14 years of programming experience.

I specialize in networking, Unix systems (Linux, FreeBSD), mobile programming, computer graphics, software architecture, reverse engineering, data processing systems, AI, computer vision.

I'm interested in all new cutting edge technologies.

You may also be interested in...

Pro
Pro

Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.
 
QuestionDrupal 7 Pin
Raghunath Avula25-Feb-14 6:14
memberRaghunath Avula25-Feb-14 6:14 
QuestionLogin is failed, Protocol not found: Pin
Jerry Oh29-Sep-13 18:32
memberJerry Oh29-Sep-13 18:32 
AnswerRe: Login is failed, Protocol not found: Pin
Андрей Москвичёв30-Sep-13 3:01
professionalАндрей Москвичёв30-Sep-13 3:01 
GeneralRe: Login is failed, Protocol not found: Pin
Jerry Oh3-Oct-13 4:31
memberJerry Oh3-Oct-13 4:31 
GeneralRe: Login is failed, Protocol not found: Pin
Андрей Москвичёв3-Oct-13 6:43
professionalАндрей Москвичёв3-Oct-13 6:43 
QuestionMake it Drupal 7.x compatible? Pin
RickStranberg4-May-13 15:02
memberRickStranberg4-May-13 15:02 
QuestionThanks and Help Pin
Member 989951210-Mar-13 18:37
memberMember 989951210-Mar-13 18:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 19 Feb 2013
Article Copyright 2013 by Andrey Moskvichev
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid