2008-11-25 02:37:33 +00:00
< ? php
2012-09-12 09:18:04 +00:00
use Drupal\Core\Entity\EntityInterface ;
2012-06-15 16:43:34 +00:00
2008-11-25 02:37:33 +00:00
/**
* @ file
* Hooks provided by the User module .
*/
/**
* @ addtogroup hooks
* @ {
*/
2013-01-16 17:37:23 +00:00
/**
* Act on a newly created user .
*
* This hook runs after a new user object has just been instantiated . It can be
* used to set initial values , e . g . to provide defaults .
*
2013-06-06 10:20:38 +00:00
* @ param \Drupal\user\UserInterface $user
2013-01-16 17:37:23 +00:00
* The user object .
*/
function hook_user_create ( \Drupal\user\Plugin\Core\Entity\User $user ) {
if ( ! isset ( $user -> foo )) {
$user -> foo = 'some_initial_value' ;
}
}
2009-03-14 23:01:38 +00:00
/**
* Act on user objects when loaded from the database .
*
* Due to the static cache in user_load_multiple () you should not use this
* hook to modify the user properties returned by the { users } table itself
* since this may result in unreliable results when loading from cache .
*
* @ param $users
* An array of user objects , indexed by uid .
*
* @ see user_load_multiple ()
* @ see profile_user_load ()
*/
function hook_user_load ( $users ) {
2011-05-17 05:50:19 +00:00
$result = db_query ( 'SELECT uid, foo FROM {my_table} WHERE uid IN (:uids)' , array ( ':uids' => array_keys ( $users )));
2009-03-14 23:01:38 +00:00
foreach ( $result as $record ) {
2011-05-17 05:50:19 +00:00
$users [ $record -> uid ] -> foo = $record -> foo ;
2009-03-14 23:01:38 +00:00
}
}
2008-11-25 02:37:33 +00:00
2010-03-12 15:56:30 +00:00
/**
2011-12-17 12:43:37 +00:00
* Act before user deletion .
2010-03-12 15:56:30 +00:00
*
2011-12-17 12:43:37 +00:00
* This hook is invoked from user_delete_multiple () before
* field_attach_delete () is called and before the user is actually removed from
* the database .
2010-03-12 15:56:30 +00:00
*
2010-12-01 00:29:41 +00:00
* Modules should additionally implement hook_user_cancel () to process stored
* user data for other account cancellation methods .
*
2010-03-12 15:56:30 +00:00
* @ param $account
2011-12-17 12:43:37 +00:00
* The account that is about to be deleted .
2010-03-12 15:56:30 +00:00
*
2011-12-17 12:43:37 +00:00
* @ see hook_user_delete ()
2010-03-12 15:56:30 +00:00
* @ see user_delete_multiple ()
*/
2011-12-17 12:43:37 +00:00
function hook_user_predelete ( $account ) {
2010-03-12 15:56:30 +00:00
db_delete ( 'mytable' )
-> condition ( 'uid' , $account -> uid )
-> execute ();
}
2011-12-17 12:43:37 +00:00
/**
* Respond to user deletion .
*
* This hook is invoked from user_delete_multiple () after field_attach_delete ()
* has been called and after the user has been removed from the database .
*
* Modules should additionally implement hook_user_cancel () to process stored
* user data for other account cancellation methods .
*
* @ param $account
* The account that has been deleted .
*
* @ see hook_user_predelete ()
* @ see user_delete_multiple ()
*/
function hook_user_delete ( $account ) {
drupal_set_message ( t ( 'User: @name has been deleted.' , array ( '@name' => $account -> name )));
}
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
2011-12-17 12:43:37 +00:00
* method . To react to that method , implement hook_user_predelete () or
* hook_user_delete () 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
*
2009-01-22 12:46:07 +00:00
* @ param $edit
2009-01-08 08:42:13 +00:00
* The array of form values submitted by the user .
2009-01-22 12:46:07 +00:00
* @ param $account
2009-01-08 08:42:13 +00:00
* The user object on which the operation is being performed .
* @ param $method
* 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' );
2013-05-26 20:18:10 +00:00
$nodes = db_select ( 'node_field_data' , 'n' )
2009-04-30 16:10:10 +00:00
-> fields ( 'n' , array ( 'nid' ))
-> condition ( 'uid' , $account -> uid )
-> execute ()
-> fetchCol ();
2009-01-08 08:42:13 +00:00
node_mass_update ( $nodes , array ( 'status' => 0 ));
break ;
case 'user_cancel_reassign' :
// Anonymize nodes (current revisions).
module_load_include ( 'inc' , 'node' , 'node.admin' );
2013-05-26 20:18:10 +00:00
$nodes = db_select ( 'node_field_data' , 'n' )
2009-04-30 16:10:10 +00:00
-> fields ( 'n' , array ( 'nid' ))
-> condition ( 'uid' , $account -> uid )
-> execute ()
-> fetchCol ();
2009-01-08 08:42:13 +00:00
node_mass_update ( $nodes , array ( 'uid' => 0 ));
// Anonymize old revisions.
2013-05-26 20:18:10 +00:00
db_update ( 'node_field_revision' )
2009-04-30 16:10:10 +00:00
-> fields ( array ( 'uid' => 0 ))
-> condition ( 'uid' , $account -> uid )
-> 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
*
2011-05-08 19:50:38 +00:00
* @ param $methods
2009-01-08 08:42:13 +00:00
* An array containing user account cancellation methods , keyed by method id .
*
* @ see user_cancel_methods ()
* @ see user_cancel_confirm_form ()
*/
function hook_user_cancel_methods_alter ( & $methods ) {
// Limit access to disable account and unpublish content method.
$methods [ 'user_cancel_block_unpublish' ][ 'access' ] = user_access ( 'administer site configuration' );
// Remove the content re-assigning method.
unset ( $methods [ 'user_cancel_reassign' ]);
// Add a custom zero-out method.
$methods [ 'mymodule_zero_out' ] = array (
'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.
'access' => user_access ( 'access zero-out account cancellation method' ),
);
}
2012-01-16 02:18:26 +00:00
/**
* Alter the username that is displayed for a user .
*
* Called by user_format_name () to allow modules to alter the username that ' s
* displayed . Can be used to ensure user privacy in situations where
* $account -> name is too revealing .
*
* @ param $name
* The string that user_format_name () will return .
*
* @ param $account
* The account object passed to user_format_name () .
*
* @ see user_format_name ()
*/
function hook_user_format_name_alter ( & $name , $account ) {
// Display the user's uid instead of name.
if ( isset ( $account -> uid )) {
$name = t ( 'User !uid' , array ( '!uid' => $account -> uid ));
}
}
2009-08-12 12:36:05 +00:00
/**
2012-04-26 03:51:09 +00:00
* Act on a user account being inserted or updated .
2009-10-10 16:48:39 +00:00
*
2012-04-26 03:51:09 +00:00
* This hook is invoked before the user account is saved to the database .
*
2009-10-10 16:48:39 +00:00
* @ param $account
2012-04-26 03:51:09 +00:00
* The user account object .
2009-10-10 16:48:39 +00:00
*
* @ see hook_user_insert ()
* @ see hook_user_update ()
*/
2012-04-26 03:51:09 +00:00
function hook_user_presave ( $account ) {
2012-11-27 22:26:22 +00:00
// Ensure that our value is an array.
2012-04-26 03:51:09 +00:00
if ( isset ( $account -> mymodule_foo )) {
2012-11-27 22:26:22 +00:00
$account -> mymodule_foo = ( array ) $account -> mymodule_foo ;
2009-10-10 16:48:39 +00:00
}
}
/**
2012-04-26 03:51:09 +00:00
* Respond to creation of a new user account .
2009-08-12 12:36:05 +00:00
*
2012-10-09 17:29:51 +00:00
* Note that when this hook is invoked , the changes have not yet been written to
* the database , because a database transaction is still in progress . The
2012-11-10 15:12:06 +00:00
* transaction is not finalized until the insert operation is entirely completed
* and \Drupal\user\DataStorageController :: save () goes out of scope . You should
* not rely on data in the database at this time as it is not updated yet . You
* should also note that any write / update database queries executed from this hook
* are also not committed immediately . Check \Drupal\user\DataStorageController :: save ()
* and db_transaction () for more info .
2012-10-09 17:29:51 +00:00
*
2009-08-12 12:36:05 +00:00
* @ param $account
2012-04-26 03:51:09 +00:00
* The user account object .
2009-10-10 16:48:39 +00:00
*
* @ see hook_user_presave ()
* @ see hook_user_update ()
2009-08-12 12:36:05 +00:00
*/
2012-04-26 03:51:09 +00:00
function hook_user_insert ( $account ) {
db_insert ( 'user_changes' )
2009-08-12 12:36:05 +00:00
-> fields ( array (
'uid' => $account -> uid ,
2012-04-26 03:51:09 +00:00
'created' => time (),
2009-08-12 12:36:05 +00:00
))
-> execute ();
}
2009-10-10 16:48:39 +00:00
/**
2012-04-26 03:51:09 +00:00
* Respond to updates to a user account .
2009-10-10 16:48:39 +00:00
*
2012-10-09 17:29:51 +00:00
* Note that when this hook is invoked , the changes have not yet been written to
* the database , because a database transaction is still in progress . The
2012-11-10 15:12:06 +00:00
* transaction is not finalized until the update operation is entirely completed
* and \Drupal\user\DataStorageController :: save () goes out of scope . You should not
* rely on data in the database at this time as it is not updated yet . You should
* also note that any write / update database queries executed from this hook are
* also not committed immediately . Check \Drupal\user\DataStorageController :: save ()
* and db_transaction () for more info .
2012-10-09 17:29:51 +00:00
*
2009-10-10 16:48:39 +00:00
* @ param $account
2012-04-26 03:51:09 +00:00
* The user account object .
2009-10-10 16:48:39 +00:00
*
* @ see hook_user_presave ()
* @ see hook_user_insert ()
*/
2012-04-26 03:51:09 +00:00
function hook_user_update ( $account ) {
2009-10-10 16:48:39 +00:00
db_insert ( 'user_changes' )
-> fields ( array (
'uid' => $account -> uid ,
'changed' => time (),
))
-> execute ();
}
2009-08-12 12:36:05 +00:00
/**
* The user just logged in .
*
* @ param $account
* The user object on which the operation was just performed .
*/
2013-01-07 11:11:52 +00:00
function hook_user_login ( $account ) {
2012-12-06 16:19:03 +00:00
$config = config ( 'system.timezone' );
2009-08-12 12:36:05 +00:00
// If the user has a NULL time zone, notify them to set a time zone.
2012-12-06 16:19:03 +00:00
if ( ! $account -> timezone && $config -> get ( 'user.configurable' ) && $config -> get ( 'user.warn' )) {
2010-12-11 19:16:42 +00:00
drupal_set_message ( t ( 'Configure your <a href="@user-edit">account time zone setting</a>.' , array ( '@user-edit' => url ( " user/ $account->uid /edit " , array ( 'query' => drupal_get_destination (), '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' )
-> fields ( array (
'uid' => $account -> uid ,
'time' => time (),
))
-> execute ();
}
/**
* The user ' s account information is being displayed .
*
* The module should format its custom additions for display and add them to the
* $account -> content array .
*
2013-06-06 10:20:38 +00:00
* @ param \Drupal\user\UserInterface $account
2009-08-12 12:36:05 +00:00
* The user object on which the operation is being performed .
2013-01-08 19:16:16 +00:00
* @ param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
* The entity_display object holding the display options configured for the
* user components .
2009-12-26 16:50:09 +00:00
* @ param $view_mode
* View mode , e . g . 'full' .
2010-10-03 01:15:34 +00:00
* @ param $langcode
* The language code used for rendering .
2010-10-23 15:30:34 +00:00
*
* @ see hook_user_view_alter ()
* @ see hook_entity_view ()
2009-08-12 12:36:05 +00:00
*/
2013-06-06 10:20:38 +00:00
function hook_user_view ( \Drupal\user\UserInterface $account , \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display , $view_mode , $langcode ) {
2013-01-08 19:16:16 +00:00
// Only do the extra work if the component is configured to be displayed.
// This assumes a 'mymodule_addition' extra field has been defined for the
// user entity type in hook_field_extra_fields().
if ( $display -> getComponent ( 'mymodule_addition' )) {
$account -> content [ 'mymodule_addition' ] = array (
'#markup' => mymodule_addition ( $account ),
'#theme' => 'mymodule_my_additional_field' ,
);
2012-11-26 10:38:45 +00:00
}
2009-08-12 12:36:05 +00:00
}
2009-06-03 07:28:28 +00:00
2009-11-07 13:35:21 +00:00
/**
* The user was built ; the module may modify the structured content .
*
* This hook is called after the content has been assembled in a structured array
* and may be used for doing processing which requires that the complete user
* content structure has been built .
*
* If the module wishes to act on the rendered HTML of the user rather than the
* structured content array , it may use this hook to add a #post_render callback.
2012-05-04 19:53:43 +00:00
* Alternatively , it could also implement hook_preprocess_HOOK () for
2012-12-13 12:11:18 +00:00
* user . tpl . php . See drupal_render () and theme () documentation
2012-05-04 19:53:43 +00:00
* respectively for details .
2009-11-07 13:35:21 +00:00
*
* @ param $build
* A renderable array representing the user .
2013-06-06 10:20:38 +00:00
* @ param \Drupal\user\UserInterface $account
2012-06-15 16:43:34 +00:00
* The user account being rendered .
2013-01-08 19:16:16 +00:00
* @ param \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display
* The entity_display object holding the display options configured for the
* user components .
2009-11-07 13:35:21 +00:00
*
2009-12-21 13:47:32 +00:00
* @ see user_view ()
2010-10-23 15:30:34 +00:00
* @ see hook_entity_view_alter ()
2009-11-07 13:35:21 +00:00
*/
2013-01-08 19:16:16 +00:00
function hook_user_view_alter ( & $build , \Drupal\user\Plugin\Core\Entity\User $account , \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display ) {
2009-11-07 13:35:21 +00:00
// Check for the existence of a field added by another module.
if ( isset ( $build [ 'an_additional_field' ])) {
// Change its weight.
$build [ 'an_additional_field' ][ '#weight' ] = - 10 ;
}
// Add a #post_render callback to act on the rendered HTML of the user.
$build [ '#post_render' ][] = 'my_module_user_post_render' ;
}
2011-05-14 20:29:36 +00:00
/**
* Inform other modules that a user role is about to be saved .
*
* Modules implementing this hook can act on the user role object before
* it has been saved to the database .
*
* @ param $role
* A user role object .
*
* @ see hook_user_role_insert ()
* @ see hook_user_role_update ()
*/
function hook_user_role_presave ( $role ) {
// Set a UUID for the user role if it doesn't already exist
if ( empty ( $role -> uuid )) {
$role -> uuid = uuid_uuid ();
}
}
2009-08-27 20:25:29 +00:00
/**
* Inform other modules that a user role has been added .
*
* Modules implementing this hook can act on the user role object when saved to
* the database . It ' s recommended that you implement this hook if your module
* adds additional data to user roles object . The module should save its custom
* additions to the database .
*
* @ param $role
* A user role object .
*/
function hook_user_role_insert ( $role ) {
// Save extra fields provided by the module to user roles.
db_insert ( 'my_module_table' )
-> fields ( array (
Issue #1479454 by Hugo Wetterberg, galooph, andypost, marcingy, heyrocker, larowlan, alexpott, tim.plunkett, fubhy, sun, dawehner: Convert user roles to configurables.
2013-01-19 21:53:56 +00:00
'rid' => $role -> id (),
2009-08-27 20:25:29 +00:00
'role_description' => $role -> description ,
))
-> execute ();
}
/**
* Inform other modules that a user role has been updated .
*
* Modules implementing this hook can act on the user role object when updated .
* It ' s recommended that you implement this hook if your module adds additional
* data to user roles object . The module should save its custom additions to
* the database .
*
* @ param $role
* A user role object .
*/
function hook_user_role_update ( $role ) {
// Save extra fields provided by the module to user roles.
db_merge ( 'my_module_table' )
Issue #1479454 by Hugo Wetterberg, galooph, andypost, marcingy, heyrocker, larowlan, alexpott, tim.plunkett, fubhy, sun, dawehner: Convert user roles to configurables.
2013-01-19 21:53:56 +00:00
-> key ( array ( 'rid' => $role -> id ()))
2009-08-27 20:25:29 +00:00
-> fields ( array (
'role_description' => $role -> description
))
-> execute ();
}
/**
* Inform other modules that a user role has been deleted .
*
* This hook allows you act when a user role has been deleted .
2009-12-26 16:50:09 +00:00
* If your module stores references to roles , it ' s recommended that you
2009-08-27 20:25:29 +00:00
* implement this hook and delete existing instances of the deleted role
* in your module database tables .
*
* @ param $role
* The $role object being deleted .
*/
function hook_user_role_delete ( $role ) {
// Delete existing instances of the deleted role.
db_delete ( 'my_module_table' )
Issue #1479454 by Hugo Wetterberg, galooph, andypost, marcingy, heyrocker, larowlan, alexpott, tim.plunkett, fubhy, sun, dawehner: Convert user roles to configurables.
2013-01-19 21:53:56 +00:00
-> condition ( 'rid' , $role -> id ())
2009-08-27 20:25:29 +00:00
-> execute ();
}
2008-11-25 02:37:33 +00:00
/**
* @ } End of " addtogroup hooks " .
*/