2008-11-25 02:37:33 +00:00
< ? php
/**
* @ file
* Hooks provided by the User module .
*/
/**
* @ addtogroup hooks
* @ {
*/
2009-01-08 08:42:13 +00:00
/**
* Act on user account cancellations .
*
2010-12-01 00:29:41 +00:00
* This hook is invoked from user_cancel () before a user account is canceled .
* Depending on the account cancellation method , the module should either do
* nothing , unpublish content , or anonymize content . See user_cancel_methods ()
* for the list of default account cancellation methods provided by User module .
* Modules may add further methods via hook_user_cancel_methods_alter () .
*
* This hook is NOT invoked for the 'user_cancel_delete' account cancellation
2014-07-11 12:04:53 +00:00
* method . To react to that method , implement hook_ENTITY_TYPE_predelete () or
* hook_ENTITY_TYPE_delete () for user entities instead .
2009-01-08 08:42:13 +00:00
*
2010-12-01 00:29:41 +00:00
* Expensive operations should be added to the global account cancellation batch
* by using batch_set () .
2009-01-08 08:42:13 +00:00
*
2015-05-02 23:08:41 +00:00
* @ param array $edit
2009-01-08 08:42:13 +00:00
* The array of form values submitted by the user .
2015-05-02 23:08:41 +00:00
* @ param \Drupal\Core\Session\AccountInterface $account
2009-01-08 08:42:13 +00:00
* The user object on which the operation is being performed .
2015-05-02 23:08:41 +00:00
* @ param string $method
2009-01-08 08:42:13 +00:00
* The account cancellation method .
*
* @ see user_cancel_methods ()
* @ see hook_user_cancel_methods_alter ()
*/
2009-01-22 12:46:07 +00:00
function hook_user_cancel ( $edit , $account , $method ) {
2009-01-08 08:42:13 +00:00
switch ( $method ) {
case 'user_cancel_block_unpublish' :
// Unpublish nodes (current revisions).
module_load_include ( 'inc' , 'node' , 'node.admin' );
2014-01-25 19:18:28 +00:00
$nodes = \Drupal :: entityQuery ( 'node' )
2015-10-16 16:50:16 +00:00
-> condition ( 'uid' , $account -> id ())
2014-01-25 19:18:28 +00:00
-> execute ();
2017-03-04 01:20:24 +00:00
node_mass_update ( $nodes , [ 'status' => 0 ], NULL , TRUE );
2009-01-08 08:42:13 +00:00
break ;
case 'user_cancel_reassign' :
// Anonymize nodes (current revisions).
module_load_include ( 'inc' , 'node' , 'node.admin' );
2014-01-25 19:18:28 +00:00
$nodes = \Drupal :: entityQuery ( 'node' )
2015-10-16 16:50:16 +00:00
-> condition ( 'uid' , $account -> id ())
2014-01-25 19:18:28 +00:00
-> execute ();
2017-03-04 01:20:24 +00:00
node_mass_update ( $nodes , [ 'uid' => 0 ], NULL , TRUE );
2009-01-08 08:42:13 +00:00
// Anonymize old revisions.
2013-05-26 20:18:10 +00:00
db_update ( 'node_field_revision' )
2017-03-04 01:20:24 +00:00
-> fields ([ 'uid' => 0 ])
2013-07-11 17:29:02 +00:00
-> condition ( 'uid' , $account -> id ())
2009-04-30 16:10:10 +00:00
-> execute ();
2009-01-08 08:42:13 +00:00
break ;
}
}
/**
* Modify account cancellation methods .
*
* By implementing this hook , modules are able to add , customize , or remove
* account cancellation methods . All defined methods are turned into radio
* button form elements by user_cancel_methods () after this hook is invoked .
* The following properties can be defined for each method :
* - title : The radio button ' s title .
* - description : ( optional ) A description to display on the confirmation form
* if the user is not allowed to select the account cancellation method . The
* description is NOT used for the radio button , but instead should provide
* additional explanation to the user seeking to cancel their account .
* - access : ( optional ) A boolean value indicating whether the user can access
2012-12-02 15:33:15 +00:00
* a method . If 'access' is defined , the method cannot be configured as
* default method .
2009-01-08 08:42:13 +00:00
*
2015-05-02 23:08:41 +00:00
* @ param array $methods
2009-01-08 08:42:13 +00:00
* An array containing user account cancellation methods , keyed by method id .
*
* @ see user_cancel_methods ()
2014-07-08 18:29:13 +00:00
* @ see \Drupal\user\Form\UserCancelForm
2009-01-08 08:42:13 +00:00
*/
function hook_user_cancel_methods_alter ( & $methods ) {
2014-01-25 05:52:17 +00:00
$account = \Drupal :: currentUser ();
2009-01-08 08:42:13 +00:00
// Limit access to disable account and unpublish content method.
2014-01-25 05:52:17 +00:00
$methods [ 'user_cancel_block_unpublish' ][ 'access' ] = $account -> hasPermission ( 'administer site configuration' );
2009-01-08 08:42:13 +00:00
// Remove the content re-assigning method.
unset ( $methods [ 'user_cancel_reassign' ]);
// Add a custom zero-out method.
2017-03-04 01:20:24 +00:00
$methods [ 'mymodule_zero_out' ] = [
2009-01-08 08:42:13 +00:00
'title' => t ( 'Delete the account and remove all content.' ),
'description' => t ( 'All your content will be replaced by empty strings.' ),
// access should be used for administrative methods only.
2014-01-25 05:52:17 +00:00
'access' => $account -> hasPermission ( 'access zero-out account cancellation method' ),
2017-03-04 01:20:24 +00:00
];
2009-01-08 08:42:13 +00:00
}
2012-01-16 02:18:26 +00:00
/**
* Alter the username that is displayed for a user .
*
Issue #2112679 by Berdir, damiankloip, nlisgo, krlucas, lokapujya, m1r1k, piyuesh23, blueminds, stefan.r, subhojit777, dawehner, kerby70, Dustin LeBlanc, znerol, JeroenT, borisson_, scor, amitgoyal, s_leu: getUsername() should return the username getDisplayName() for the formatted user name
2015-09-24 00:26:38 +00:00
* Called by $account -> getDisplayName () to allow modules to alter the username
* that is displayed . Can be used to ensure user privacy in situations where
2016-07-16 21:55:17 +00:00
* $account -> getDisplayName () is too revealing .
2012-01-16 02:18:26 +00:00
*
Issue #2572821 by AbhishekLal, xxronis, Adita, ritzz, dimaro, hardik.p, amit.drupal, gianani, quietone, subhojit777, alexpott: Fix documentation on hook_user_format_name_alter()
2017-01-25 17:08:46 +00:00
* @ param string | Drupal\Component\Render\MarkupInterface $name
* The username that is displayed for a user . If a hook implementation changes
* this to an object implementing MarkupInterface it is the responsibility of
* the implementation to ensure the user ' s name is escaped properly . String
* values will be autoescaped .
* @ param \Drupal\Core\Session\AccountInterface $account
* The user object on which the operation is being performed .
2012-01-16 02:18:26 +00:00
*
2016-08-29 07:44:55 +00:00
* @ see \Drupal\Core\Session\AccountInterface :: getDisplayName ()
Issue #2572821 by AbhishekLal, xxronis, Adita, ritzz, dimaro, hardik.p, amit.drupal, gianani, quietone, subhojit777, alexpott: Fix documentation on hook_user_format_name_alter()
2017-01-25 17:08:46 +00:00
* @ see sanitization
2012-01-16 02:18:26 +00:00
*/
function hook_user_format_name_alter ( & $name , $account ) {
// Display the user's uid instead of name.
2013-07-11 17:29:02 +00:00
if ( $account -> id ()) {
2017-03-04 01:20:24 +00:00
$name = t ( 'User @uid' , [ '@uid' => $account -> id ()]);
2012-01-16 02:18:26 +00:00
}
}
2009-08-12 12:36:05 +00:00
/**
* The user just logged in .
*
2015-05-02 23:08:41 +00:00
* @ param object $account
2009-08-12 12:36:05 +00:00
* The user object on which the operation was just performed .
*/
2013-01-07 11:11:52 +00:00
function hook_user_login ( $account ) {
2013-09-16 03:58:06 +00:00
$config = \Drupal :: config ( 'system.date' );
2009-08-12 12:36:05 +00:00
// If the user has a NULL time zone, notify them to set a time zone.
2013-08-01 13:46:05 +00:00
if ( ! $account -> getTimezone () && $config -> get ( 'timezone.user.configurable' ) && $config -> get ( 'timezone.user.warn' )) {
2017-03-04 01:20:24 +00:00
drupal_set_message ( t ( 'Configure your <a href=":user-edit">account time zone setting</a>.' , [ ':user-edit' => $account -> url ( 'edit-form' , [ 'query' => \Drupal :: destination () -> getAsArray (), 'fragment' => 'edit-timezone' ])]));
2009-08-12 12:36:05 +00:00
}
}
/**
* The user just logged out .
*
* @ param $account
* The user object on which the operation was just performed .
*/
function hook_user_logout ( $account ) {
db_insert ( 'logouts' )
2017-03-04 01:20:24 +00:00
-> fields ([
2013-07-11 17:29:02 +00:00
'uid' => $account -> id (),
2009-08-12 12:36:05 +00:00
'time' => time (),
2017-03-04 01:20:24 +00:00
])
2009-08-12 12:36:05 +00:00
-> execute ();
}
2008-11-25 02:37:33 +00:00
/**
* @ } End of " addtogroup hooks " .
*/