url as callback argument
Hello all, I am writing a module and I am trying to use a url as a callback argument, but it does not work. Here is the code: $items[] = array( 'path' => 'admin/settings/featured-comments/feature', 'title' => t('featured comment'), 'callback' => 'feature_comment', 'callback_args' => $_GET['cid'], 'access' => user_access('administer comments'), 'type' => MENU_CALLBACK ); and the callback function: function feature_comment($cid) { $comment = _comment_load($cid); db_query("UPDATE {featured_comments} SET featured=1 WHERE cid=%d", $cid); drupal_goto("node/$comment->nid"); } going to an example url: http://example.com/admin/settings/featured-comments/feature?cid=10840 gives the following error: warning: Missing argument 1 for feature_comment() in /var/www/html/sites/all/modules/featured_comments/featured_comments.module on line 149. So my question is, what is the best way to use parts of the linked url as a callback argument for a function, because obviously it is not what I am doing! Thanks, -- Cooper Quintin Freelance Programmer, Indymedia Journalist http://CooperQ.com (510) 827-5382
You'll want to use http://example.com/admin/settings/featured-comments/feature/10840<http://example.com/admin/settings/featured-comments/feature?cid=10840> as url. But also: function feature_comment($cid = NULL) { if (!isset($cid)) { // Do something else. drupal_goto(...); } $comment = _comment_load($cid); db_query("UPDATE {featured_comments} SET featured=1 WHERE cid=%d", $cid); drupal_goto("node/$comment->nid"); } On Tue, Feb 3, 2009 at 11:34 AM, cooper Quintin <cooperq@cooperq.com> wrote:
Hello all, I am writing a module and I am trying to use a url as a callback argument, but it does not work. Here is the code:
$items[] = array( 'path' => 'admin/settings/featured-comments/feature', 'title' => t('featured comment'), 'callback' => 'feature_comment', 'callback_args' => $_GET['cid'], 'access' => user_access('administer comments'), 'type' => MENU_CALLBACK );
and the callback function: function feature_comment($cid) { $comment = _comment_load($cid); db_query("UPDATE {featured_comments} SET featured=1 WHERE cid=%d", $cid); drupal_goto("node/$comment->nid"); }
going to an example url: http://example.com/admin/settings/featured-comments/feature?cid=10840 gives the following error: warning: Missing argument 1 for feature_comment() in /var/www/html/sites/all/modules/featured_comments/featured_comments.module on line 149.
So my question is, what is the best way to use parts of the linked url as a callback argument for a function, because obviously it is not what I am doing! Thanks,
-- Cooper Quintin Freelance Programmer, Indymedia Journalist http://CooperQ.com (510) 827-5382
On Tue, Feb 3, 2009 at 11:34 AM, cooper Quintin <cooperq@cooperq.com> wrote:
Hello all, I am writing a module and I am trying to use a url as a callback argument, but it does not work. Here is the code:
$items[] = array( 'path' => 'admin/settings/featured-comments/feature', 'title' => t('featured comment'), 'callback' => 'feature_comment', 'callback_args' => $_GET['cid'], 'access' => user_access('administer comments'), 'type' => MENU_CALLBACK );
and the callback function: function feature_comment($cid) { $comment = _comment_load($cid); db_query("UPDATE {featured_comments} SET featured=1 WHERE cid=%d", $cid); drupal_goto("node/$comment->nid"); }
I think you can bypass the problem loading the $_GET in feature_comment function and obviously setting $cid = NULL, so: function feature_comment($cid = NULL) { if (!isset($cid)) { if (!$cid = $_GET['cid']) { drupal_goto(); } } $comment = _comment_load($cid); db_query("UPDATE {featured_comments} SET featured=1 WHERE cid=%d", $cid); drupal_goto("node/$comment->nid"); } -- Paolo Mainardi Vice Presidente Assoc.ILDN (http://www.ildn.net) Blog: http://www.paolomainardi.com
I'm not how to relates to development of Drupal, so I'll try to kill this thread, here's the main issue: Your hook_menu returns an item with a 'callback_args', key, that key should be: 'callback arguments' and the value should be an array of arguments to pass to the callback. http://api.drupal.org/api/function/hook_menu/5 Regards Steven Jones ComputerMinds ltd - Perfect Drupal Websites Phone : 0121 288 0434 Mobile : 07951 270 026 http://www.computerminds.co.uk 2009/2/3 cooper Quintin <cooperq@cooperq.com>:
Hello all, I am writing a module and I am trying to use a url as a callback argument, but it does not work. Here is the code:
$items[] = array( 'path' => 'admin/settings/featured-comments/feature', 'title' => t('featured comment'), 'callback' => 'feature_comment', 'callback_args' => $_GET['cid'], 'access' => user_access('administer comments'), 'type' => MENU_CALLBACK );
and the callback function: function feature_comment($cid) { $comment = _comment_load($cid); db_query("UPDATE {featured_comments} SET featured=1 WHERE cid=%d", $cid); drupal_goto("node/$comment->nid"); }
going to an example url: http://example.com/admin/settings/featured-comments/feature?cid=10840 gives the following error: warning: Missing argument 1 for feature_comment() in /var/www/html/sites/all/modules/featured_comments/featured_comments.module on line 149.
So my question is, what is the best way to use parts of the linked url as a callback argument for a function, because obviously it is not what I am doing! Thanks,
-- Cooper Quintin Freelance Programmer, Indymedia Journalist http://CooperQ.com (510) 827-5382
$items[] = array( 'path' => 'admin/settings/featured-comments/feature', 'title' => t('featured comment'), 'callback' => 'feature_comment', 'callback_args' => $_GET['cid'], should be.. $items[] = array( 'path' => 'admin/settings/featured-comments/feature/%', 'title' => t('featured comment'), 'callback' => 'feature_comment', 'callback_args' =>array(4), ... try this.. On Tue, Feb 3, 2009 at 12:46 PM, Steven Jones <darthsteven@gmail.com> wrote:
I'm not how to relates to development of Drupal, so I'll try to kill this thread, here's the main issue:
Your hook_menu returns an item with a 'callback_args', key, that key should be: 'callback arguments' and the value should be an array of arguments to pass to the callback.
http://api.drupal.org/api/function/hook_menu/5
Regards Steven Jones ComputerMinds ltd - Perfect Drupal Websites
Phone : 0121 288 0434 Mobile : 07951 270 026 http://www.computerminds.co.uk
2009/2/3 cooper Quintin <cooperq@cooperq.com>:
Hello all, I am writing a module and I am trying to use a url as a callback argument, but it does not work. Here is the code:
$items[] = array( 'path' => 'admin/settings/featured-comments/feature', 'title' => t('featured comment'), 'callback' => 'feature_comment', 'callback_args' => $_GET['cid'], 'access' => user_access('administer comments'), 'type' => MENU_CALLBACK );
and the callback function: function feature_comment($cid) { $comment = _comment_load($cid); db_query("UPDATE {featured_comments} SET featured=1 WHERE cid=%d", $cid); drupal_goto("node/$comment->nid"); }
going to an example url: http://example.com/admin/settings/featured-comments/feature?cid=10840 gives the following error: warning: Missing argument 1 for feature_comment() in
/var/www/html/sites/all/modules/featured_comments/featured_comments.module
on line 149.
So my question is, what is the best way to use parts of the linked url as a callback argument for a function, because obviously it is not what I am doing! Thanks,
-- Cooper Quintin Freelance Programmer, Indymedia Journalist http://CooperQ.com (510) 827-5382
On Tuesday 03 February 2009, Iñaki Lopez wrote:
$items[] = array( 'path' => 'admin/settings/featured-comments/feature', 'title' => t('featured comment'), 'callback' => 'feature_comment', 'callback_args' => $_GET['cid'],
should be..
$items[] = array( 'path' => 'admin/settings/featured-comments/feature/%', 'title' => t('featured comment'), 'callback' => 'feature_comment', 'callback_args' =>array(4), ...
Bad advice: 1. There is no '%' wildcard in Drupal 5, and Drupal 6 puts the path as array key instead of having a separate 'path' element. 2. There is no 'callback_args' property. array(...) is correct, but it also needs to be renamed to 'callback arguments' for Drupal to do anything with it. In Drupal 5, that would be "'callback arguments' => array($_GET['cid'])". (Or 'page callback' and 'page arguments' in Drupal 6, which I understand the original poster is not using at this time.) Maybe the thread is better off dead - http://api.drupal.org/api/function/hook_menu/5 has been posted already - but it's hard to leave the above example uncommented as is. Cheers, j
sorry, I don't know how did I realize the drupal version was six, and my reply was a "finish-porting-this-to-d6".. Now I see, only half of the menu entry is 'ported' as well.. Indeed, a very bad advice at all.. where the hell is my head?.. brbr On Wed, Feb 4, 2009 at 12:32 AM, Jakob Petsovits <jpetso@gmx.at> wrote:
On Tuesday 03 February 2009, Iñaki Lopez wrote:
$items[] = array( 'path' => 'admin/settings/featured-comments/feature', 'title' => t('featured comment'), 'callback' => 'feature_comment', 'callback_args' => $_GET['cid'],
should be..
$items[] = array( 'path' => 'admin/settings/featured-comments/feature/%', 'title' => t('featured comment'), 'callback' => 'feature_comment', 'callback_args' =>array(4), ...
Bad advice:
1. There is no '%' wildcard in Drupal 5, and Drupal 6 puts the path as array key instead of having a separate 'path' element.
2. There is no 'callback_args' property. array(...) is correct, but it also needs to be renamed to 'callback arguments' for Drupal to do anything with it. In Drupal 5, that would be "'callback arguments' => array($_GET['cid'])". (Or 'page callback' and 'page arguments' in Drupal 6, which I understand the original poster is not using at this time.)
Maybe the thread is better off dead - http://api.drupal.org/api/function/hook_menu/5 has been posted already - but it's hard to leave the above example uncommented as is.
Cheers, j
Since I started this thread I feel it is my duty to put it to rest. The problem was indeed that I had callback_args instead of callback arguments, sorry to bother this list with my stupid typo. Additionally the below poster is correct, there is no wildcard in paths in drupal 5 which was also giving me some interesting problems. I thought that the development list was intended for all development of drupal and its modules but I guess its only for drupal core development, sorry about that! Anyway, I declare this thread closed, thanks everyone for your help. Cooper Iñaki Lopez wrote:
sorry, I don't know how did I realize the drupal version was six, and my reply was a "finish-porting-this-to-d6".. Now I see, only half of the menu entry is 'ported' as well..
Indeed, a very bad advice at all.. where the hell is my head?.. brbr
On Wed, Feb 4, 2009 at 12:32 AM, Jakob Petsovits <jpetso@gmx.at <mailto:jpetso@gmx.at>> wrote:
On Tuesday 03 February 2009, Iñaki Lopez wrote: > $items[] = array( > 'path' => 'admin/settings/featured-comments/feature', > 'title' => t('featured comment'), > 'callback' => 'feature_comment', > 'callback_args' => $_GET['cid'], > > should be.. > > $items[] = array( > 'path' => 'admin/settings/featured-comments/feature/%', > 'title' => t('featured comment'), > 'callback' => 'feature_comment', > 'callback_args' =>array(4), > ...
Bad advice:
1. There is no '%' wildcard in Drupal 5, and Drupal 6 puts the path as array key instead of having a separate 'path' element.
2. There is no 'callback_args' property. array(...) is correct, but it also needs to be renamed to 'callback arguments' for Drupal to do anything with it. In Drupal 5, that would be "'callback arguments' => array($_GET['cid'])". (Or 'page callback' and 'page arguments' in Drupal 6, which I understand the original poster is not using at this time.)
Maybe the thread is better off dead - http://api.drupal.org/api/function/hook_menu/5 has been posted already - but it's hard to leave the above example uncommented as is.
Cheers, j
-- Cooper Quintin Freelance Programmer, Indymedia Journalist http://CooperQ.com (510) 827-5382
participants (6)
-
cooper Quintin -
Iñaki Lopez -
Jakob Petsovits -
Paolo Mainardi -
Robrecht Jacques -
Steven Jones