I need to limit node creation to one item for a specific content_type (photography contest), and to redirect the user to its photography if he already posted one. The snippet below is almost working as expected.
function mymodule_menu_alter(&$items) {
$items['node/add/slide']['access callback'] = '_mymodule_limit_slide';
}
function _mymodule_limit_slide () {
global $user;
if(user_access("create slide content")) {
$allowed_limit = variable_get('allowed_limit', 1);
$slides = db_query("SELECT nid, count(*) ct FROM {node} WHERE type = :type AND uid = :uid", array(':type' => 'slide' ,':uid' => $user->uid))->fetchAssoc();
if ($slides['ct'] < $allowed_limit || in_array('administrator', $user->roles)) {
return TRUE;
}
else {
drupal_set_message('You cannot post more than one photography. But you can edit the one you posted below if yo wish to.');
drupal_goto(drupal_get_path_alias('node/'.$slides['nid']));
}
}
return FALSE;
}
If I dpm(slides)
, it returns the correct array and data.
But, as far as I could debug, if I try to connect a user who :
- already posted at least one node of other content type
- whether he posted or not a slide
Drupal tries to go to node/'.$slides['nid']
and that creates an infinite loop.
Summary : I don't understand why my function _mymodule_limit_slide ()
would be called out of node/add/slide
url, or what else is wrong ?
drupal_get_path_alias
, but unfortunately this did not help.