Custom Service Resource with Arguments
Edit the my_module_services.inc
file to the following for passing arguments.
<?php
function my_module_services_resources() {
$resources = array(
'my_module_resources' => array(
'actions' => array(
'get_user_email' => array(
'help' => t('Gets user email of uid passed.'),
'file' => array(
'type' => 'inc',
'module' => 'my_module',
'name' => 'my_module.resource',
),
'callback' => 'my_module_get_user_email',
'args' => array(
array(
'name' => 'my_param',
'type' => 'int',
'description' => t(' uid to pass.'),
'source' => array('data' => 'my_param'),
'optional' => FALSE,
),
),
'access callback' => '_drupalgap_resource_access',
'access callback file' => array(
'type' => 'inc',
'module' => 'drupalgap',
'name' => 'drupalgap.resource',
),
'access arguments' => array('access user profiles'),
'access arguments append' => TRUE,
),
),
),
);
return $resources;
}
Note: Since we changed the Resources action you will need to follow the steps from # 4 of Create a Custom Service Page.
Now adjust your my_module.resources.inc
file to include a new function like the following:
function my_module_get_user_email($uid) {
$query = db_select('users', 'u')
->fields('u', array('mail'))
->condition('uid', $uid, '=');
$result = $query->execute();
return $result->fetchField();
}
If using Method 1 from the previous page, you will need to add a new function to your custom DrupalGap module's JavaScript file like so:
function my_module_get_user_email(options) {
try {
options.method = 'POST';
options.path = 'my_module_resources/get_user_email.json';
options.service = 'my_module';
options.resource = 'get_user_email';
Drupal.services.call(options);
}
catch (error) {
console.log('my_module_get_user_email - ' + error);
}
}
Still using Method 1 from previous page you can edit your javascript in your module to the following:
var my_args = {
my_param: 123
};
my_module_get_user_count({
data: my_args,
success: function(result) {
var user_count = result[0];
alert('There are ' + user_count + ' registered user(s)!');
}
});
Depending on your service resource, you may have to turn your args into a string when sending them:
data: JSON.stringify(my_args)