I'm trying to create a link in a block like so:

$block['content'] .= l($groups['title'], 'groupify/add/'. $groups['gid'] . '/'.$node->nid);

Which produces a link like:

http://localhost/mysite/?=groupify/add/11/3

groupify is my module, it's adding node 3 to group 11 - At least it's trying to... but it doesn't, I get page not found.

I think the problem lies in my hook_menu()

function groupify_menu() {

  $items = array();

    $items['groupify/add/%groupify/%'] = array(
      'callback' => 'groupify_add',
      'page arguements' => array(2,3),
      'type' => MENU_CALLBACK,
      'access arguments' => array(GROUPIFY_PERM_ADD)
    );
  
    $items['groupify/remove/%groupify/%'] = array(
      'page callback' => 'groupify_remove',
      'page arguements' => array(2,3),
      'type' => MENU_CALLBACK,
      'access arguments' => array(GROUPIFY_PERM_ADD)
    );

    $items['admin/settings/groupify'] = array(
      'title'              => 'Groupify',
      'description'        => 'Settings for Groupify',
      'page callback'      => 'drupal_get_form',
      'page arguments' => array('groupify_settings'),
      'type'               => MENU_NORMAL_ITEM,
      'access arguments' => array(GROUPIFY_PERM_ADMINISTER),
    );  

  return $items;
}

Here's the function I'm trying to call:


function groupify_add() {
 
  $gid = arg(2);
  $nid = arg(3);
  
  $sql = "SELECT position FROM {groupify_nodes} WHERE gid=%d";
  $res = db_query($sql,  $gid);
  if (!$res) {
  drupal_set_message(t('That class does not exist.'));
  return drupal_goto();
  }
  else {
    while ($row = db_fetch_array($res)) {
      $group['gid'] = $row['gid'];
      $group['title'] = $row['title'];
      $group['cat'] = $row['cat'];
    }
  $position = $group['position'];
  $position = max($position) + 1;
  db_query("INSERT INTO {groupify_nodes} (gid, nid, position) VALUES (%d, %d, %d)", $gid, $nid, $position);
  
  }

  // There should always be a destination set for this, so just goto wherever.
  drupal_set_message(t('Lesson added to class'));
}

All I want is that path to run that function, similar to how Favorite_Nodes does it, but I can't figure it out and it's driving me crazy!!!! HELP!!!!

Comments

nquocbao’s picture

Please check the menu api, i think you should add both parameter name for the path

It should be groupify/add/%groupify/%class , right ?

antkiewicz’s picture

Hey Agitpropist,

I see two problems, first, in your first menu item the first key's name should be 'page callback' and not 'callback' ( as is on other ones).
Two, the 'groupify' in in your menu callback path $item['groupify/add/%groupify/%'] expects that you have a function named groupify_load(). However from your code it looks like you don't need it, so just simply make it $item['groupify/add/%/%']

Also, your function 'groupify_add()' expects two parameters, so change it to something like this

function groupify_add($gid = NULL, $nid = NULL) {
// make sure parameters are numeric and that you have both of them
if (is_numeric($gid) && is_numeric($nid)) {
  $gid = $gid;
  $nid = $nid;
}
else {
  // else, set error message here and return
}
....
}

Please don't forget to sanitize/validate your input before sticking it into query.

I hope this helps.

sdutcher’s picture

I tried your suggestion but I'm still getting 'page not found'. Here's my updated code.

function groupify_add($gid = NULL, $nid = NULL) {
 /** if (!groupify_check_token($node->nid)) { //Decide if I want to use this
    return drupal_goto();
  }**/
  $add_node = $_COOKIE['groupify_node_add']; //Get the cookie
  $gid = $gid;
  $nid = $nid;
  
  if (is_numeric($gid) && is_numeric($nid)) {
    $sql = "SELECT position FROM {groupify_nodes} WHERE gid=%d";
    $res = db_query($sql,  $gid);
    if (!$res) {
      drupal_set_message(t('That class does not exist.'));
      return drupal_goto('node/' . $add_node);
    }
    else {
      while ($row = db_fetch_array($res)) {
        $group['gid'] = $row['gid'];
        $group['title'] = $row['title'];
        $group['cat'] = $row['cat'];
      }
    $position = $group['position'];
    $position = max($position) + 1;
    db_query("INSERT INTO {groupify_nodes} (gid, nid, position) VALUES (%d, %d, %d)", $gid, $nid, $position);
    drupal_set_message(t('Lesson added to class'));
    return drupal_goto('node/' . $add_node);
    }
  }
  else {
  drupal_set_message(t('Lesson added to class'));
  return drupal_goto('node/home_page');
  
  }

}

And here's Hook_Menu

function groupify_menu() {

  $items = array();

    $items['groupify/add/%/%'] = array(
      'page callback' => 'groupify_add',
      'page arguements' => array(2,3),
      'type' => MENU_CALLBACK,
      'access arguments' => array(GROUPIFY_PERM_ADD)
    );
  
    $items['groupify/remove/%/%'] = array(
      'page callback' => 'groupify_remove',
      'page arguements' => array(2,3),
      'type' => MENU_CALLBACK,
      'access arguments' => array(GROUPIFY_PERM_ADD)
    );

    $items['admin/settings/groupify'] = array(
      'title'              => 'Groupify',
      'description'        => 'Settings for Groupify',
      'page callback'      => 'drupal_get_form',
      'page arguments' => array('groupify_settings'),
      'type'               => MENU_NORMAL_ITEM,
      'access arguments' => array(GROUPIFY_PERM_ADMINISTER),
    );  

  return $items;
}

Is it my callback arguements? I don't understand what's going on, it's like drupal isn't registering the changes to hook_menu or something.

antkiewicz’s picture

Did you rebuild your menu?
If not, then just visiting the module listing page ".../admin/build/modules' will rebuild it. Otherwise drupal will not recognize new path.

sdutcher’s picture

Heh... Yea, I disabled it then re-enabled it and I'm getting some progress. What my problem is now is that my variables aren't getting transferred to my function.

antkiewicz’s picture

Hey, I have been reading your code too fast, and I missed it before.
You have a miss-spelled menu item key

'page arguements' => array(2,3),
// should be
'page arguments' => array(2,3),

Remember to rebuild the menu.

sdutcher’s picture

Ahh! I'm such a noob! Thanks, you get a special comment line!