annotate modules/user/user.api.php @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
rev   line source
danielebarchiesi@0 1 <?php
danielebarchiesi@0 2
danielebarchiesi@0 3 /**
danielebarchiesi@0 4 * @file
danielebarchiesi@0 5 * Hooks provided by the User module.
danielebarchiesi@0 6 */
danielebarchiesi@0 7
danielebarchiesi@0 8 /**
danielebarchiesi@0 9 * @addtogroup hooks
danielebarchiesi@0 10 * @{
danielebarchiesi@0 11 */
danielebarchiesi@0 12
danielebarchiesi@0 13 /**
danielebarchiesi@0 14 * Act on user objects when loaded from the database.
danielebarchiesi@0 15 *
danielebarchiesi@0 16 * Due to the static cache in user_load_multiple() you should not use this
danielebarchiesi@0 17 * hook to modify the user properties returned by the {users} table itself
danielebarchiesi@0 18 * since this may result in unreliable results when loading from cache.
danielebarchiesi@0 19 *
danielebarchiesi@0 20 * @param $users
danielebarchiesi@0 21 * An array of user objects, indexed by uid.
danielebarchiesi@0 22 *
danielebarchiesi@0 23 * @see user_load_multiple()
danielebarchiesi@0 24 * @see profile_user_load()
danielebarchiesi@0 25 */
danielebarchiesi@0 26 function hook_user_load($users) {
danielebarchiesi@0 27 $result = db_query('SELECT uid, foo FROM {my_table} WHERE uid IN (:uids)', array(':uids' => array_keys($users)));
danielebarchiesi@0 28 foreach ($result as $record) {
danielebarchiesi@0 29 $users[$record->uid]->foo = $record->foo;
danielebarchiesi@0 30 }
danielebarchiesi@0 31 }
danielebarchiesi@0 32
danielebarchiesi@0 33 /**
danielebarchiesi@0 34 * Respond to user deletion.
danielebarchiesi@0 35 *
danielebarchiesi@0 36 * This hook is invoked from user_delete_multiple() before field_attach_delete()
danielebarchiesi@0 37 * is called and before users are actually removed from the database.
danielebarchiesi@0 38 *
danielebarchiesi@0 39 * Modules should additionally implement hook_user_cancel() to process stored
danielebarchiesi@0 40 * user data for other account cancellation methods.
danielebarchiesi@0 41 *
danielebarchiesi@0 42 * @param $account
danielebarchiesi@0 43 * The account that is being deleted.
danielebarchiesi@0 44 *
danielebarchiesi@0 45 * @see user_delete_multiple()
danielebarchiesi@0 46 */
danielebarchiesi@0 47 function hook_user_delete($account) {
danielebarchiesi@0 48 db_delete('mytable')
danielebarchiesi@0 49 ->condition('uid', $account->uid)
danielebarchiesi@0 50 ->execute();
danielebarchiesi@0 51 }
danielebarchiesi@0 52
danielebarchiesi@0 53 /**
danielebarchiesi@0 54 * Act on user account cancellations.
danielebarchiesi@0 55 *
danielebarchiesi@0 56 * This hook is invoked from user_cancel() before a user account is canceled.
danielebarchiesi@0 57 * Depending on the account cancellation method, the module should either do
danielebarchiesi@0 58 * nothing, unpublish content, or anonymize content. See user_cancel_methods()
danielebarchiesi@0 59 * for the list of default account cancellation methods provided by User module.
danielebarchiesi@0 60 * Modules may add further methods via hook_user_cancel_methods_alter().
danielebarchiesi@0 61 *
danielebarchiesi@0 62 * This hook is NOT invoked for the 'user_cancel_delete' account cancellation
danielebarchiesi@0 63 * method. To react on this method, implement hook_user_delete() instead.
danielebarchiesi@0 64 *
danielebarchiesi@0 65 * Expensive operations should be added to the global account cancellation batch
danielebarchiesi@0 66 * by using batch_set().
danielebarchiesi@0 67 *
danielebarchiesi@0 68 * @param $edit
danielebarchiesi@0 69 * The array of form values submitted by the user.
danielebarchiesi@0 70 * @param $account
danielebarchiesi@0 71 * The user object on which the operation is being performed.
danielebarchiesi@0 72 * @param $method
danielebarchiesi@0 73 * The account cancellation method.
danielebarchiesi@0 74 *
danielebarchiesi@0 75 * @see user_cancel_methods()
danielebarchiesi@0 76 * @see hook_user_cancel_methods_alter()
danielebarchiesi@0 77 */
danielebarchiesi@0 78 function hook_user_cancel($edit, $account, $method) {
danielebarchiesi@0 79 switch ($method) {
danielebarchiesi@0 80 case 'user_cancel_block_unpublish':
danielebarchiesi@0 81 // Unpublish nodes (current revisions).
danielebarchiesi@0 82 module_load_include('inc', 'node', 'node.admin');
danielebarchiesi@0 83 $nodes = db_select('node', 'n')
danielebarchiesi@0 84 ->fields('n', array('nid'))
danielebarchiesi@0 85 ->condition('uid', $account->uid)
danielebarchiesi@0 86 ->execute()
danielebarchiesi@0 87 ->fetchCol();
danielebarchiesi@0 88 node_mass_update($nodes, array('status' => 0));
danielebarchiesi@0 89 break;
danielebarchiesi@0 90
danielebarchiesi@0 91 case 'user_cancel_reassign':
danielebarchiesi@0 92 // Anonymize nodes (current revisions).
danielebarchiesi@0 93 module_load_include('inc', 'node', 'node.admin');
danielebarchiesi@0 94 $nodes = db_select('node', 'n')
danielebarchiesi@0 95 ->fields('n', array('nid'))
danielebarchiesi@0 96 ->condition('uid', $account->uid)
danielebarchiesi@0 97 ->execute()
danielebarchiesi@0 98 ->fetchCol();
danielebarchiesi@0 99 node_mass_update($nodes, array('uid' => 0));
danielebarchiesi@0 100 // Anonymize old revisions.
danielebarchiesi@0 101 db_update('node_revision')
danielebarchiesi@0 102 ->fields(array('uid' => 0))
danielebarchiesi@0 103 ->condition('uid', $account->uid)
danielebarchiesi@0 104 ->execute();
danielebarchiesi@0 105 // Clean history.
danielebarchiesi@0 106 db_delete('history')
danielebarchiesi@0 107 ->condition('uid', $account->uid)
danielebarchiesi@0 108 ->execute();
danielebarchiesi@0 109 break;
danielebarchiesi@0 110 }
danielebarchiesi@0 111 }
danielebarchiesi@0 112
danielebarchiesi@0 113 /**
danielebarchiesi@0 114 * Modify account cancellation methods.
danielebarchiesi@0 115 *
danielebarchiesi@0 116 * By implementing this hook, modules are able to add, customize, or remove
danielebarchiesi@0 117 * account cancellation methods. All defined methods are turned into radio
danielebarchiesi@0 118 * button form elements by user_cancel_methods() after this hook is invoked.
danielebarchiesi@0 119 * The following properties can be defined for each method:
danielebarchiesi@0 120 * - title: The radio button's title.
danielebarchiesi@0 121 * - description: (optional) A description to display on the confirmation form
danielebarchiesi@0 122 * if the user is not allowed to select the account cancellation method. The
danielebarchiesi@0 123 * description is NOT used for the radio button, but instead should provide
danielebarchiesi@0 124 * additional explanation to the user seeking to cancel their account.
danielebarchiesi@0 125 * - access: (optional) A boolean value indicating whether the user can access
danielebarchiesi@0 126 * a method. If #access is defined, the method cannot be configured as default
danielebarchiesi@0 127 * method.
danielebarchiesi@0 128 *
danielebarchiesi@0 129 * @param $methods
danielebarchiesi@0 130 * An array containing user account cancellation methods, keyed by method id.
danielebarchiesi@0 131 *
danielebarchiesi@0 132 * @see user_cancel_methods()
danielebarchiesi@0 133 * @see user_cancel_confirm_form()
danielebarchiesi@0 134 */
danielebarchiesi@0 135 function hook_user_cancel_methods_alter(&$methods) {
danielebarchiesi@0 136 // Limit access to disable account and unpublish content method.
danielebarchiesi@0 137 $methods['user_cancel_block_unpublish']['access'] = user_access('administer site configuration');
danielebarchiesi@0 138
danielebarchiesi@0 139 // Remove the content re-assigning method.
danielebarchiesi@0 140 unset($methods['user_cancel_reassign']);
danielebarchiesi@0 141
danielebarchiesi@0 142 // Add a custom zero-out method.
danielebarchiesi@0 143 $methods['mymodule_zero_out'] = array(
danielebarchiesi@0 144 'title' => t('Delete the account and remove all content.'),
danielebarchiesi@0 145 'description' => t('All your content will be replaced by empty strings.'),
danielebarchiesi@0 146 // access should be used for administrative methods only.
danielebarchiesi@0 147 'access' => user_access('access zero-out account cancellation method'),
danielebarchiesi@0 148 );
danielebarchiesi@0 149 }
danielebarchiesi@0 150
danielebarchiesi@0 151 /**
danielebarchiesi@0 152 * Add mass user operations.
danielebarchiesi@0 153 *
danielebarchiesi@0 154 * This hook enables modules to inject custom operations into the mass operations
danielebarchiesi@0 155 * dropdown found at admin/people, by associating a callback function with
danielebarchiesi@0 156 * the operation, which is called when the form is submitted. The callback function
danielebarchiesi@0 157 * receives one initial argument, which is an array of the checked users.
danielebarchiesi@0 158 *
danielebarchiesi@0 159 * @return
danielebarchiesi@0 160 * An array of operations. Each operation is an associative array that may
danielebarchiesi@0 161 * contain the following key-value pairs:
danielebarchiesi@0 162 * - "label": Required. The label for the operation, displayed in the dropdown menu.
danielebarchiesi@0 163 * - "callback": Required. The function to call for the operation.
danielebarchiesi@0 164 * - "callback arguments": Optional. An array of additional arguments to pass to
danielebarchiesi@0 165 * the callback function.
danielebarchiesi@0 166 *
danielebarchiesi@0 167 */
danielebarchiesi@0 168 function hook_user_operations() {
danielebarchiesi@0 169 $operations = array(
danielebarchiesi@0 170 'unblock' => array(
danielebarchiesi@0 171 'label' => t('Unblock the selected users'),
danielebarchiesi@0 172 'callback' => 'user_user_operations_unblock',
danielebarchiesi@0 173 ),
danielebarchiesi@0 174 'block' => array(
danielebarchiesi@0 175 'label' => t('Block the selected users'),
danielebarchiesi@0 176 'callback' => 'user_user_operations_block',
danielebarchiesi@0 177 ),
danielebarchiesi@0 178 'cancel' => array(
danielebarchiesi@0 179 'label' => t('Cancel the selected user accounts'),
danielebarchiesi@0 180 ),
danielebarchiesi@0 181 );
danielebarchiesi@0 182 return $operations;
danielebarchiesi@0 183 }
danielebarchiesi@0 184
danielebarchiesi@0 185 /**
danielebarchiesi@0 186 * Retrieve a list of user setting or profile information categories.
danielebarchiesi@0 187 *
danielebarchiesi@0 188 * @return
danielebarchiesi@0 189 * An array of associative arrays. Each inner array has elements:
danielebarchiesi@0 190 * - "name": The internal name of the category.
danielebarchiesi@0 191 * - "title": The human-readable, localized name of the category.
danielebarchiesi@0 192 * - "weight": An integer specifying the category's sort ordering.
danielebarchiesi@0 193 * - "access callback": Name of the access callback function to use to
danielebarchiesi@0 194 * determine whether the user can edit the category. Defaults to using
danielebarchiesi@0 195 * user_edit_access(). See hook_menu() for more information on access
danielebarchiesi@0 196 * callbacks.
danielebarchiesi@0 197 * - "access arguments": Arguments for the access callback function. Defaults
danielebarchiesi@0 198 * to array(1).
danielebarchiesi@0 199 */
danielebarchiesi@0 200 function hook_user_categories() {
danielebarchiesi@0 201 return array(array(
danielebarchiesi@0 202 'name' => 'account',
danielebarchiesi@0 203 'title' => t('Account settings'),
danielebarchiesi@0 204 'weight' => 1,
danielebarchiesi@0 205 ));
danielebarchiesi@0 206 }
danielebarchiesi@0 207
danielebarchiesi@0 208 /**
danielebarchiesi@0 209 * A user account is about to be created or updated.
danielebarchiesi@0 210 *
danielebarchiesi@0 211 * This hook is primarily intended for modules that want to store properties in
danielebarchiesi@0 212 * the serialized {users}.data column, which is automatically loaded whenever a
danielebarchiesi@0 213 * user account object is loaded, modules may add to $edit['data'] in order
danielebarchiesi@0 214 * to have their data serialized on save.
danielebarchiesi@0 215 *
danielebarchiesi@0 216 * @param $edit
danielebarchiesi@0 217 * The array of form values submitted by the user. Assign values to this
danielebarchiesi@0 218 * array to save changes in the database.
danielebarchiesi@0 219 * @param $account
danielebarchiesi@0 220 * The user object on which the operation is performed. Values assigned in
danielebarchiesi@0 221 * this object will not be saved in the database.
danielebarchiesi@0 222 * @param $category
danielebarchiesi@0 223 * The active category of user information being edited.
danielebarchiesi@0 224 *
danielebarchiesi@0 225 * @see hook_user_insert()
danielebarchiesi@0 226 * @see hook_user_update()
danielebarchiesi@0 227 */
danielebarchiesi@0 228 function hook_user_presave(&$edit, $account, $category) {
danielebarchiesi@0 229 // Make sure that our form value 'mymodule_foo' is stored as
danielebarchiesi@0 230 // 'mymodule_bar' in the 'data' (serialized) column.
danielebarchiesi@0 231 if (isset($edit['mymodule_foo'])) {
danielebarchiesi@0 232 $edit['data']['mymodule_bar'] = $edit['mymodule_foo'];
danielebarchiesi@0 233 }
danielebarchiesi@0 234 }
danielebarchiesi@0 235
danielebarchiesi@0 236 /**
danielebarchiesi@0 237 * A user account was created.
danielebarchiesi@0 238 *
danielebarchiesi@0 239 * The module should save its custom additions to the user object into the
danielebarchiesi@0 240 * database.
danielebarchiesi@0 241 *
danielebarchiesi@0 242 * @param $edit
danielebarchiesi@0 243 * The array of form values submitted by the user.
danielebarchiesi@0 244 * @param $account
danielebarchiesi@0 245 * The user object on which the operation is being performed.
danielebarchiesi@0 246 * @param $category
danielebarchiesi@0 247 * The active category of user information being edited.
danielebarchiesi@0 248 *
danielebarchiesi@0 249 * @see hook_user_presave()
danielebarchiesi@0 250 * @see hook_user_update()
danielebarchiesi@0 251 */
danielebarchiesi@0 252 function hook_user_insert(&$edit, $account, $category) {
danielebarchiesi@0 253 db_insert('mytable')
danielebarchiesi@0 254 ->fields(array(
danielebarchiesi@0 255 'myfield' => $edit['myfield'],
danielebarchiesi@0 256 'uid' => $account->uid,
danielebarchiesi@0 257 ))
danielebarchiesi@0 258 ->execute();
danielebarchiesi@0 259 }
danielebarchiesi@0 260
danielebarchiesi@0 261 /**
danielebarchiesi@0 262 * A user account was updated.
danielebarchiesi@0 263 *
danielebarchiesi@0 264 * Modules may use this hook to update their user data in a custom storage
danielebarchiesi@0 265 * after a user account has been updated.
danielebarchiesi@0 266 *
danielebarchiesi@0 267 * @param $edit
danielebarchiesi@0 268 * The array of form values submitted by the user.
danielebarchiesi@0 269 * @param $account
danielebarchiesi@0 270 * The user object on which the operation is performed.
danielebarchiesi@0 271 * @param $category
danielebarchiesi@0 272 * The active category of user information being edited.
danielebarchiesi@0 273 *
danielebarchiesi@0 274 * @see hook_user_presave()
danielebarchiesi@0 275 * @see hook_user_insert()
danielebarchiesi@0 276 */
danielebarchiesi@0 277 function hook_user_update(&$edit, $account, $category) {
danielebarchiesi@0 278 db_insert('user_changes')
danielebarchiesi@0 279 ->fields(array(
danielebarchiesi@0 280 'uid' => $account->uid,
danielebarchiesi@0 281 'changed' => time(),
danielebarchiesi@0 282 ))
danielebarchiesi@0 283 ->execute();
danielebarchiesi@0 284 }
danielebarchiesi@0 285
danielebarchiesi@0 286 /**
danielebarchiesi@0 287 * The user just logged in.
danielebarchiesi@0 288 *
danielebarchiesi@0 289 * @param $edit
danielebarchiesi@0 290 * The array of form values submitted by the user.
danielebarchiesi@0 291 * @param $account
danielebarchiesi@0 292 * The user object on which the operation was just performed.
danielebarchiesi@0 293 */
danielebarchiesi@0 294 function hook_user_login(&$edit, $account) {
danielebarchiesi@0 295 // If the user has a NULL time zone, notify them to set a time zone.
danielebarchiesi@0 296 if (!$account->timezone && variable_get('configurable_timezones', 1) && variable_get('empty_timezone_message', 0)) {
danielebarchiesi@0 297 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')))));
danielebarchiesi@0 298 }
danielebarchiesi@0 299 }
danielebarchiesi@0 300
danielebarchiesi@0 301 /**
danielebarchiesi@0 302 * The user just logged out.
danielebarchiesi@0 303 *
danielebarchiesi@0 304 * Note that when this hook is invoked, the changes have not yet been written to
danielebarchiesi@0 305 * the database, because a database transaction is still in progress. The
danielebarchiesi@0 306 * transaction is not finalized until the save operation is entirely completed
danielebarchiesi@0 307 * and user_save() goes out of scope. You should not rely on data in the
danielebarchiesi@0 308 * database at this time as it is not updated yet. You should also note that any
danielebarchiesi@0 309 * write/update database queries executed from this hook are also not committed
danielebarchiesi@0 310 * immediately. Check user_save() and db_transaction() for more info.
danielebarchiesi@0 311 *
danielebarchiesi@0 312 * @param $account
danielebarchiesi@0 313 * The user object on which the operation was just performed.
danielebarchiesi@0 314 */
danielebarchiesi@0 315 function hook_user_logout($account) {
danielebarchiesi@0 316 db_insert('logouts')
danielebarchiesi@0 317 ->fields(array(
danielebarchiesi@0 318 'uid' => $account->uid,
danielebarchiesi@0 319 'time' => time(),
danielebarchiesi@0 320 ))
danielebarchiesi@0 321 ->execute();
danielebarchiesi@0 322 }
danielebarchiesi@0 323
danielebarchiesi@0 324 /**
danielebarchiesi@0 325 * The user's account information is being displayed.
danielebarchiesi@0 326 *
danielebarchiesi@0 327 * The module should format its custom additions for display and add them to the
danielebarchiesi@0 328 * $account->content array.
danielebarchiesi@0 329 *
danielebarchiesi@0 330 * Note that when this hook is invoked, the changes have not yet been written to
danielebarchiesi@0 331 * the database, because a database transaction is still in progress. The
danielebarchiesi@0 332 * transaction is not finalized until the save operation is entirely completed
danielebarchiesi@0 333 * and user_save() goes out of scope. You should not rely on data in the
danielebarchiesi@0 334 * database at this time as it is not updated yet. You should also note that any
danielebarchiesi@0 335 * write/update database queries executed from this hook are also not committed
danielebarchiesi@0 336 * immediately. Check user_save() and db_transaction() for more info.
danielebarchiesi@0 337 *
danielebarchiesi@0 338 * @param $account
danielebarchiesi@0 339 * The user object on which the operation is being performed.
danielebarchiesi@0 340 * @param $view_mode
danielebarchiesi@0 341 * View mode, e.g. 'full'.
danielebarchiesi@0 342 * @param $langcode
danielebarchiesi@0 343 * The language code used for rendering.
danielebarchiesi@0 344 *
danielebarchiesi@0 345 * @see hook_user_view_alter()
danielebarchiesi@0 346 * @see hook_entity_view()
danielebarchiesi@0 347 */
danielebarchiesi@0 348 function hook_user_view($account, $view_mode, $langcode) {
danielebarchiesi@0 349 if (user_access('create blog content', $account)) {
danielebarchiesi@0 350 $account->content['summary']['blog'] = array(
danielebarchiesi@0 351 '#type' => 'user_profile_item',
danielebarchiesi@0 352 '#title' => t('Blog'),
danielebarchiesi@0 353 '#markup' => l(t('View recent blog entries'), "blog/$account->uid", array('attributes' => array('title' => t("Read !username's latest blog entries.", array('!username' => format_username($account)))))),
danielebarchiesi@0 354 '#attributes' => array('class' => array('blog')),
danielebarchiesi@0 355 );
danielebarchiesi@0 356 }
danielebarchiesi@0 357 }
danielebarchiesi@0 358
danielebarchiesi@0 359 /**
danielebarchiesi@0 360 * The user was built; the module may modify the structured content.
danielebarchiesi@0 361 *
danielebarchiesi@0 362 * This hook is called after the content has been assembled in a structured array
danielebarchiesi@0 363 * and may be used for doing processing which requires that the complete user
danielebarchiesi@0 364 * content structure has been built.
danielebarchiesi@0 365 *
danielebarchiesi@0 366 * If the module wishes to act on the rendered HTML of the user rather than the
danielebarchiesi@0 367 * structured content array, it may use this hook to add a #post_render callback.
danielebarchiesi@0 368 * Alternatively, it could also implement hook_preprocess_user_profile(). See
danielebarchiesi@0 369 * drupal_render() and theme() documentation respectively for details.
danielebarchiesi@0 370 *
danielebarchiesi@0 371 * @param $build
danielebarchiesi@0 372 * A renderable array representing the user.
danielebarchiesi@0 373 *
danielebarchiesi@0 374 * @see user_view()
danielebarchiesi@0 375 * @see hook_entity_view_alter()
danielebarchiesi@0 376 */
danielebarchiesi@0 377 function hook_user_view_alter(&$build) {
danielebarchiesi@0 378 // Check for the existence of a field added by another module.
danielebarchiesi@0 379 if (isset($build['an_additional_field'])) {
danielebarchiesi@0 380 // Change its weight.
danielebarchiesi@0 381 $build['an_additional_field']['#weight'] = -10;
danielebarchiesi@0 382 }
danielebarchiesi@0 383
danielebarchiesi@0 384 // Add a #post_render callback to act on the rendered HTML of the user.
danielebarchiesi@0 385 $build['#post_render'][] = 'my_module_user_post_render';
danielebarchiesi@0 386 }
danielebarchiesi@0 387
danielebarchiesi@0 388 /**
danielebarchiesi@0 389 * Inform other modules that a user role is about to be saved.
danielebarchiesi@0 390 *
danielebarchiesi@0 391 * Modules implementing this hook can act on the user role object before
danielebarchiesi@0 392 * it has been saved to the database.
danielebarchiesi@0 393 *
danielebarchiesi@0 394 * @param $role
danielebarchiesi@0 395 * A user role object.
danielebarchiesi@0 396 *
danielebarchiesi@0 397 * @see hook_user_role_insert()
danielebarchiesi@0 398 * @see hook_user_role_update()
danielebarchiesi@0 399 */
danielebarchiesi@0 400 function hook_user_role_presave($role) {
danielebarchiesi@0 401 // Set a UUID for the user role if it doesn't already exist
danielebarchiesi@0 402 if (empty($role->uuid)) {
danielebarchiesi@0 403 $role->uuid = uuid_uuid();
danielebarchiesi@0 404 }
danielebarchiesi@0 405 }
danielebarchiesi@0 406
danielebarchiesi@0 407 /**
danielebarchiesi@0 408 * Inform other modules that a user role has been added.
danielebarchiesi@0 409 *
danielebarchiesi@0 410 * Modules implementing this hook can act on the user role object when saved to
danielebarchiesi@0 411 * the database. It's recommended that you implement this hook if your module
danielebarchiesi@0 412 * adds additional data to user roles object. The module should save its custom
danielebarchiesi@0 413 * additions to the database.
danielebarchiesi@0 414 *
danielebarchiesi@0 415 * @param $role
danielebarchiesi@0 416 * A user role object.
danielebarchiesi@0 417 */
danielebarchiesi@0 418 function hook_user_role_insert($role) {
danielebarchiesi@0 419 // Save extra fields provided by the module to user roles.
danielebarchiesi@0 420 db_insert('my_module_table')
danielebarchiesi@0 421 ->fields(array(
danielebarchiesi@0 422 'rid' => $role->rid,
danielebarchiesi@0 423 'role_description' => $role->description,
danielebarchiesi@0 424 ))
danielebarchiesi@0 425 ->execute();
danielebarchiesi@0 426 }
danielebarchiesi@0 427
danielebarchiesi@0 428 /**
danielebarchiesi@0 429 * Inform other modules that a user role has been updated.
danielebarchiesi@0 430 *
danielebarchiesi@0 431 * Modules implementing this hook can act on the user role object when updated.
danielebarchiesi@0 432 * It's recommended that you implement this hook if your module adds additional
danielebarchiesi@0 433 * data to user roles object. The module should save its custom additions to
danielebarchiesi@0 434 * the database.
danielebarchiesi@0 435 *
danielebarchiesi@0 436 * @param $role
danielebarchiesi@0 437 * A user role object.
danielebarchiesi@0 438 */
danielebarchiesi@0 439 function hook_user_role_update($role) {
danielebarchiesi@0 440 // Save extra fields provided by the module to user roles.
danielebarchiesi@0 441 db_merge('my_module_table')
danielebarchiesi@0 442 ->key(array('rid' => $role->rid))
danielebarchiesi@0 443 ->fields(array(
danielebarchiesi@0 444 'role_description' => $role->description
danielebarchiesi@0 445 ))
danielebarchiesi@0 446 ->execute();
danielebarchiesi@0 447 }
danielebarchiesi@0 448
danielebarchiesi@0 449 /**
danielebarchiesi@0 450 * Inform other modules that a user role has been deleted.
danielebarchiesi@0 451 *
danielebarchiesi@0 452 * This hook allows you act when a user role has been deleted.
danielebarchiesi@0 453 * If your module stores references to roles, it's recommended that you
danielebarchiesi@0 454 * implement this hook and delete existing instances of the deleted role
danielebarchiesi@0 455 * in your module database tables.
danielebarchiesi@0 456 *
danielebarchiesi@0 457 * @param $role
danielebarchiesi@0 458 * The $role object being deleted.
danielebarchiesi@0 459 */
danielebarchiesi@0 460 function hook_user_role_delete($role) {
danielebarchiesi@0 461 // Delete existing instances of the deleted role.
danielebarchiesi@0 462 db_delete('my_module_table')
danielebarchiesi@0 463 ->condition('rid', $role->rid)
danielebarchiesi@0 464 ->execute();
danielebarchiesi@0 465 }
danielebarchiesi@0 466
danielebarchiesi@0 467 /**
danielebarchiesi@0 468 * @} End of "addtogroup hooks".
danielebarchiesi@0 469 */