Chris@0: condition('uid', $account->id()) Chris@0: ->execute(); Chris@0: node_mass_update($nodes, ['status' => 0], NULL, TRUE); Chris@0: break; Chris@0: Chris@0: case 'user_cancel_reassign': Chris@0: // Anonymize nodes (current revisions). Chris@0: module_load_include('inc', 'node', 'node.admin'); Chris@0: $nodes = \Drupal::entityQuery('node') Chris@0: ->condition('uid', $account->id()) Chris@0: ->execute(); Chris@0: node_mass_update($nodes, ['uid' => 0], NULL, TRUE); Chris@0: // Anonymize old revisions. Chris@18: \Drupal::database()->update('node_field_revision') Chris@0: ->fields(['uid' => 0]) Chris@0: ->condition('uid', $account->id()) Chris@0: ->execute(); Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Modify account cancellation methods. Chris@0: * Chris@0: * By implementing this hook, modules are able to add, customize, or remove Chris@0: * account cancellation methods. All defined methods are turned into radio Chris@0: * button form elements by user_cancel_methods() after this hook is invoked. Chris@0: * The following properties can be defined for each method: Chris@0: * - title: The radio button's title. Chris@0: * - description: (optional) A description to display on the confirmation form Chris@0: * if the user is not allowed to select the account cancellation method. The Chris@0: * description is NOT used for the radio button, but instead should provide Chris@0: * additional explanation to the user seeking to cancel their account. Chris@0: * - access: (optional) A boolean value indicating whether the user can access Chris@0: * a method. If 'access' is defined, the method cannot be configured as Chris@0: * default method. Chris@0: * Chris@0: * @param array $methods Chris@0: * An array containing user account cancellation methods, keyed by method id. Chris@0: * Chris@0: * @see user_cancel_methods() Chris@0: * @see \Drupal\user\Form\UserCancelForm Chris@0: */ Chris@0: function hook_user_cancel_methods_alter(&$methods) { Chris@0: $account = \Drupal::currentUser(); Chris@0: // Limit access to disable account and unpublish content method. Chris@0: $methods['user_cancel_block_unpublish']['access'] = $account->hasPermission('administer site configuration'); Chris@0: Chris@0: // Remove the content re-assigning method. Chris@0: unset($methods['user_cancel_reassign']); Chris@0: Chris@0: // Add a custom zero-out method. Chris@0: $methods['mymodule_zero_out'] = [ Chris@0: 'title' => t('Delete the account and remove all content.'), Chris@0: 'description' => t('All your content will be replaced by empty strings.'), Chris@0: // access should be used for administrative methods only. Chris@0: 'access' => $account->hasPermission('access zero-out account cancellation method'), Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Alter the username that is displayed for a user. Chris@0: * Chris@0: * Called by $account->getDisplayName() to allow modules to alter the username Chris@0: * that is displayed. Can be used to ensure user privacy in situations where Chris@17: * $account->getDisplayName() is too revealing. This hook is invoked both for Chris@17: * user entities and the anonymous user session object. Chris@0: * Chris@0: * @param string|Drupal\Component\Render\MarkupInterface $name Chris@0: * The username that is displayed for a user. If a hook implementation changes Chris@0: * this to an object implementing MarkupInterface it is the responsibility of Chris@0: * the implementation to ensure the user's name is escaped properly. String Chris@0: * values will be autoescaped. Chris@0: * @param \Drupal\Core\Session\AccountInterface $account Chris@17: * The object on which the operation is being performed. This object may be a Chris@17: * user entity. If the object is an implementation of UserInterface you can Chris@17: * use instanceof operator before accessing user entity methods. For example: Chris@17: * @code Chris@17: * if ($account instanceof UserInterface) { Chris@17: * // Access user entity methods. Chris@17: * } Chris@17: * @endcode Chris@0: * Chris@0: * @see \Drupal\Core\Session\AccountInterface::getDisplayName() Chris@0: * @see sanitization Chris@0: */ Chris@17: function hook_user_format_name_alter(&$name, AccountInterface $account) { Chris@0: // Display the user's uid instead of name. Chris@0: if ($account->id()) { Chris@0: $name = t('User @uid', ['@uid' => $account->id()]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * The user just logged in. Chris@0: * Chris@17: * @param \Drupal\user\UserInterface $account Chris@0: * The user object on which the operation was just performed. Chris@0: */ Chris@17: function hook_user_login(UserInterface $account) { Chris@0: $config = \Drupal::config('system.date'); Chris@0: // If the user has a NULL time zone, notify them to set a time zone. Chris@0: if (!$account->getTimezone() && $config->get('timezone.user.configurable') && $config->get('timezone.user.warn')) { Chris@17: \Drupal::messenger() Chris@17: ->addStatus(t('Configure your account time zone setting.', [ Chris@18: ':user-edit' => $account->toUrl('edit-form', [ Chris@17: 'query' => \Drupal::destination() Chris@17: ->getAsArray(), Chris@17: 'fragment' => 'edit-timezone', Chris@18: ])->toString(), Chris@17: ])); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * The user just logged out. Chris@0: * Chris@17: * @param \Drupal\Core\Session\AccountInterface $account Chris@0: * The user object on which the operation was just performed. Chris@0: */ Chris@17: function hook_user_logout(AccountInterface $account) { Chris@18: \Drupal::database()->insert('logouts') Chris@0: ->fields([ Chris@0: 'uid' => $account->id(), Chris@0: 'time' => time(), Chris@0: ]) Chris@0: ->execute(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @} End of "addtogroup hooks". Chris@0: */