--- user_cancel_example/CHANGELOG.txt
+++ user_cancel_example/CHANGELOG.txt
@@ -0,0 +1,1 @@
+# $Id$

--- user_cancel_example/user_cancel_example.info
+++ user_cancel_example/user_cancel_example.info
@@ -0,0 +1,7 @@
+; $Id$
+name = User cancel example
+description = Demonstrates Drupal user cancellation API usage.
+package = Example modules
+core = 7.x
+files[] = user_cancel_example.module
+files[] = user_cancel_example.test

--- user_cancel_example/user_cancel_example.module
+++ user_cancel_example/user_cancel_example.module
@@ -0,0 +1,107 @@
+<?php
+// $Id$
+/**
+ * @file user_cancel_example.module
+ * Extends Drupal's user cancel options. This module alter some cancel options
+ * and add a custom method to disable the account and disable comments in the
+ * nodes of the cancelled user.
+ *
+ * @see user_cancel_methods()
+ * @see hook_user_cancel_methods_alter()
+ * @see user_cancel_example_user_cancel_methods_alter()
+ * @see user_cancel()
+ */
+
+/**
+ * Implements hook_menu().
+ */
+function user_cancel_example_menu() {
+  $items['examples/user_cancel_example'] = array(
+    'title' => 'User cancel example',
+    'page callback' => 'user_cancel_example_info',
+    'access callback' => TRUE,
+  );
+  return $items;
+}
+
+/**
+ * Implements hook_user_cancel_methods_alter();
+ *
+ * Provide custom cancel methods and alter current existing methods. With this
+ * hook it is possible to overide already existing methods and include new
+ * options to the cancel account selection form. Here are some examples:
+ *
+ * @see hook_user_cancel_methods_alter()
+ * @see user_cancel_example_user_cancel()
+ */
+function user_cancel_example_user_cancel_methods_alter(&$methods) {
+
+  // Remove the content re-assigning method. From now on, this method will not
+  // be shown as an option when selecting the cancel method.
+  unset($methods['user_cancel_reassign']);
+
+  // Limit access to disable account and unpublish content method. The
+  // 'Cancel account and unpublish all content' method will only be availble
+  // to users with 'administer site configuration' options.
+  $methods['user_cancel_block_unpublish']['access'] = user_access('administer site configuration');
+
+  // Add a method to disable comments on the nodes of the disabled account.
+  $methods['user_cancel_example_disable_comment'] = array(
+    'title' => t('Disable account and disable comments on its own content.'),
+    'description' => t('All the content will remain published, but no more comments could be added.'),
+    // access should be used for administrative methods only. This is totally
+    // optional, but is better for the example to show how to create this
+    // cancel methods more documented.
+    'access' => user_access('access disable comments cancel method'),
+  );
+
+}
+
+/**
+ * Simple basic information about the module; an entry point.
+ */
+function user_cancel_example_info() {
+  return t('The example provides a new option when cancelling an account, and alter existing default options.');
+}
+
+/**
+ * Implements permission();
+ *
+ * Create our custom permission to access the 'keep content' option during
+ * account cancellation method selection
+ */
+function user_cancel_example_permission() {
+  return array(
+    'access disable comments cancel method' =>  array(
+      'title' => t('Use the option to disable comments cancellation method.'),
+      'description' => t('Enable the option to "Disable the account and disable comments on its own posts" in the cancell method forms.'),
+    ),
+  );
+}
+
+/**
+ * Implements hook_user_cancel();
+ *
+ * Now it is time to operate when the account is deleted. We can handle our
+ * custom methods and also provide additional batch entries for other existing
+ * cancel methods.
+ *
+ * @see hook_user_cancel()
+ */
+function user_cancel_example_user_cancel($edit, $account, $method) {
+
+  switch ($method) {
+    // This is our custom method.
+    case 'user_cancel_example_disable_comment':
+      // Select all the nodes of this user.
+      module_load_include('inc', 'node', 'node.admin');
+      $nodes = db_select('node', 'n')
+        ->fields('n', array('nid'))
+        ->condition('uid', $account->uid)
+        ->execute()
+        ->fetchCol();
+      // Disable comments for these nodes in current batch operation.
+      node_mass_update($nodes, array('comment' => 0));
+      break;
+  }
+}

--- user_cancel_example/user_cancel_example.test
+++ user_cancel_example/user_cancel_example.test
@@ -0,0 +1,101 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Test for the user cancel example module.
+ */
+class UserCancelExampleTestCase extends DrupalWebTestCase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'User cancel functionality',
+      'description' => t('Test User cancel Example'),
+      'group' => 'Examples',
+    );
+  }
+
+  /**
+   * Enable modules and create user with specific permissions.
+   */
+  function setUp() {
+    parent::setUp('user_cancel_example');
+  }
+
+  function testCustomDisableAccountOption() {
+
+    // Create a regular account able to add some content.
+    $account = $this->drupalCreateUser(
+      array('access content', 'create article content')
+    );
+
+    // Log in using this account
+    $this->drupalLogin($account);
+
+    // Create some nodes (by default they have comment enabled), but just
+    // in case.
+    $options = array(
+      'comment' => 2,
+    );
+    $nodes = array();
+    $nodes[] = $this->drupalCreateNode($options);
+    $nodes[] = $this->drupalCreateNode($options);
+    $nodes[] = $this->drupalCreateNode($options);
+    $nodes[] = $this->drupalCreateNode($options);
+    $nodes[] = $this->drupalCreateNode($options);
+
+    // Create the admin user and log in (automatically logout current user).
+    $admin = $this->drupalCreateUser(
+      array('administer users', 'access disable comments cancel method')
+    );
+    $this->drupalLogin($admin);
+
+    // Go to cancel account with some nodes.
+    $this->drupalPost(
+      'user/'. $account->uid .'/edit',
+      array(),
+      t('Cancel account')
+    );
+
+    // Verify that altered methods are not available.
+    $this->assertNoFieldById('edit-user-cancel-method--1');
+
+    // Verify that custom option is available, considering the default options.
+    $this->assertFieldById(
+      'edit-user-cancel-method--5',
+      'user_cancel_example_disable_comment'
+    );
+
+    // Select this option and disable the account.
+    $options = array(
+      'user_cancel_method' => 'user_cancel_example_disable_comment',
+    );
+
+    // Disable the account
+    $this->drupalPost(NULL, $options, t('Cancel account'));
+
+    // Verify the nodes now don't have comment allowed.
+    foreach($nodes as $node) {
+      $entry = $this->drupalGetNodeByTitle($node->title);
+      $this->assertEqual($entry->comment, 0, t('Node has comments disabled.'));
+    }
+
+    // Logout admin user.
+    $this->drupalLogout();
+
+    // Try to log in with disabled account.
+    $edit = array(
+      'name' => $account->name,
+      'pass' => $account->pass_raw
+    );
+    $this->drupalPost('user', $edit, t('Log in'));
+
+    // Verify user is unable to log in.
+    $this->assertRaw(
+      t('The username %name has not been activated or is blocked.',
+        array('%name' => $account->name)
+      )
+    );
+  }
+
+}


