Buenas, estoy intentando crear un resource donde una operation "retrieve" se le pase O NO un argumento por URL , y en función a eso devuelva un resultado u otro, pero por algún motivo no consigo lograrlo usando un valor por defecto , he aquí mi código. Si NO le paso un parámetro por url, no devuelve los roles, pero si le paso uno cualquiera si , ¿por qué?. La url sería :
http://localhost/misitio/api/v1/register/ (así no cargaría los roles) , pero si le pongo cualquier entero
http://localhost/misitio/api/v1/register/22 , así si los cargaría.
Código:
/**
* Implementation of hook_services_resources().
*/
function mymodule_services_resources(){
$resources = array();
$resources['register'] = array (
'operations' => array(
'retrieve' => array(
'help' => 'Retrieve users rols',
'file' => array('type' => 'inc', 'module' => 'mymodule_resources', 'name' => 'resources/mymodule_register_resource'),
'callback' => '_mymodule_register_retrieve',
// Do not need arguments for retrieve roles.
'args' => array(
// If argument is present, go to the next form register step.
array(
'name' => 'rol',
'optional' => TRUE,
'default value' => 0, // esto he probado a quitarlo, pero da lo mismo.
'source' => array('path' => 0),
'type' => 'int',
'description' => 'The id of the rol selected',
),
),
'access callback' => '_mymodule_register_access',
),
),
);
return $resources;
}
Esto sería en el PHP INC dentro de resources.
Código:
/**
* Return all roles types, except anonymous and register.
* @return array
*/
function _mymodule_register_retrieve($rol) {
$roles = user_roles(TRUE, NULL);
// Remove unnecesary roles.
$index_adm = array_search('administrator', $roles);
$index_reg = array_search('authenticated user', $roles);
if ($index_adm !== FALSE || $index_reg !== FALSE) {
unset($roles[$index_adm]);
unset($roles[$index_reg]);
}
// user_roles($membersonly = FALSE, $permission = NULL).
return $roles;
// Send selected rol to the next form step
}
function _mymodule_register_access(){
return TRUE;
}
Otra cuestión que me planteo,
¿cómo tendría que construir esto sino quisiera pasarle ningún argumento?. He probado con eliminar el array args o dejarlo vacío, y no funciona.
Gracias