4

I would like to show the user delete confirmation form as a tab in user profile, just near View and Modity. I am trying the following code in a custom module but it is not working, I'm not sure I am calling the right callback and arguments:

function custom_morebuttons_menu_alter(&$items) {
  $items['user/%user/cancel'] = array (
    'title' => 'Cancel profile',
    'page callback' => 'drupal_get_form', 
    'page arguments' => array('user_cancel_confirm_form', 1), 
    'access callback' => 'user_cancel_access', 
    'access arguments' => array(1), 
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  );
}
0

2 Answers 2

12
+50

You need to use hook_menu() instead of hook_menu_alter() because you are adding a new menu item rather than altering an existing one.

function YOUR_MODULE_menu() {
  $items['user/%user/cancel'] = array(
    'title' => 'Cancel profile',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('user_cancel_confirm_form', 1),
    'access callback' => 'user_cancel_access',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
    'file' => 'user.pages.inc',
    'file path' => drupal_get_path('module', 'user'),
  );

  return $items;
}

Don't forget to return $items at the end of the function, since it is not passed by reference.

Though I'm not sure if the page callback and arguments are right, I didn't check that, I've just copied from your code.

3
  • 3
    I would add that the reason OP needs to use hook_menu() and not hook_menu_alter() is because they are not altering an existing entry, but adding a new menu entry; it just happens to be a subtab for an existing primary task. Commented Feb 3, 2013 at 20:49
  • This gives me this error: Notice: Undefined index: user_cancel_confirm_form in drupal_retrieve_form() (linea 763 di D:\Progetti\Sito\xampp\htdocs\drupal\includes\form.inc). Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'user_cancel_confirm_form' not found or invalid function name in drupal_retrieve_form() (linea 798 di D:\Progetti\Sito\xampp\htdocs\drupal\includes\form.inc). Commented Feb 3, 2013 at 21:51
  • 4
    You need to also specify "file" and "file path" in the menu hook - see my EDIT. Commented Feb 5, 2013 at 21:05
6

using hook_menu() and a custom module

function mymodule_menu() {
    $items['user/%user/canceltask'] = array(
    'title' => 'Cancel profile',
      'page callback' => 'mymodule_goto_user_cancel',
      'page arguments' => array(1),
      'access arguments' => array('cancel_account'),
      'access callback' => TRUE,
      'type' => MENU_LOCAL_TASK,
      'weight' => 10,
    );
    return $items;
}

function mymodule_goto_user_cancel($arguments=0) {
    drupal_goto('user/'.(int)$arguments->uid.'/cancel');
}

This solution offers the advantage of not needing to mess around with any forms at all. Its a simple redirect Button.

3
  • Not the most elegant of the solutions. But I like the simplicity :)
    – Gokul N K
    Commented Feb 8, 2013 at 7:30
  • 1
    I dont think it's elegant to mess around in the system. The other solution here might introduce problems with changing dependancies. starting with using $items['user/%user/cancel'] -> what will ensure this hook_menu is called after the other one? unsolved race condition I say.. Commented Feb 8, 2013 at 23:49
  • In fact I used $items['user/%user/delete'] as $items['user/%user/cancel'] is already present in the user edit form. Commented Feb 9, 2013 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.