Downloading the 7.x-3.x-dev version of this module, I found that all calls to the services endpoint return an error indicating that there are missing arguments.
Looking into the issue further, it seems that all of the callbacks accept arguments incorrectly. This may be because the Services API changed the way that hook_services_resources() passes arguments to the callbacks.
In any case, functions like flag_service_is_flagged($flag_name, $content_id, $uid = NULL) should be defined flag_service_is_flagged($data), where $data is an associative array keyed according to argument names. To assign default values to the arguments, hook_services_resources() allows that 'default value' be defined when the arguments are defined.
Example (corrected) resource:
$resources['flag']['actions']['flag'] = array(
'file' => array(
'type' => 'inc',
'module' => 'flag_service',
'name' => 'flag_service',
),
'help' => t('Flags (or unflags) a content.'),
'access callback' => 'flag_service_flag_content_access',
'callback' => 'flag_service_flag_content',
'args' => array(
array(
'name' => 'flag_name',
'type' => 'string',
'description' => t('The name of the flag.'),
'source' => 'data',
'optional' => FALSE,
),
array(
'name' => 'content_id',
'type' => 'int',
'description' => t('The content ID.'),
'source' => 'data',
'optional' => FALSE,
),
array(
'name' => 'action',
'type' => 'string',
'description' => t('Optional; The action to perform, default is "flag". Should be "flag" or "unflag".'),
'source' => 'data',
'optional' => TRUE,
'default value' => 'flag',
),
array(
'name' => 'uid',
'type' => 'int',
'description' => t('The user ID for which to flag.'),
'source' => 'data',
'optional' => TRUE,
),
array(
'name' => 'skip_permission_check',
'type' => 'boolean',
'description' => t('Optional; Flag the content even if the user does not have permission to do so. FALSE by default'),
'source' => 'data',
'optional' => TRUE,
'default value' => FALSE,
),
),
);
Example (corrected) resource callback:
/**
* Service wrapper to flag a content.
*
* @param $flag_name
* The flag name.
* @param $content_id
* The content ID to check if it was flagged.
* @param $uid
* The user ID to check if they flagged the content.
* @param $action
* The action. Should be "flag" or "unflag".
* @return
* TRUE if content was flagged.
*/
function flag_service_flag_content($data) {
global $user;
$account = empty($data['uid']) ? $user : user_load($data['uid']);
$flag = flag_get_flag($data['flag_name']);
$skip_permission_check = (boolean) $data['skip_permission_check'];
if (!$flag) {
watchdog('services', '<pre>' . print_r(debug_backtrace(), TRUE) . '</pre>');
// Flag does not exist.
return services_error(t('There is no flag with the name @flag_name', array('@flag_name' => $data['flag_name'])), 406);
}
return $flag->flag($data['action'], $data['content_id'], $account, $data['skip_permission_check']);
}
Making these changes on all callbacks and resources has fixed the issue, and flag_service now works as expected.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | countall.png | 43.29 KB | jerenus |
| #4 | flag_flag.png | 46.85 KB | jerenus |
| #4 | flag_unflag.png | 46.53 KB | jerenus |
| #4 | is_flageed_1.png | 44.2 KB | jerenus |
| #4 | is_flagged_2.png | 43.95 KB | jerenus |
Comments
Comment #1
grasmash commentedAttaching patch.
Comment #2
grasmash commentedFixing documentation.
Comment #3
syakely commentedpatch flag_service-783460-2.patch is working for me. Tested is_flagged, flag and countall.
thanks,
SY.
Comment #4
jerenus commentedThanks, it is working for me too. Committed.
Following is the way to use all these actions.
PS: @Matthew Grasmick Sorry about my mistake of the patch's author name, I'm learning how to commit a patch of others in right way. Hope I can put your name change back, sorry again!