user.api.php

File

modules/user/user.api.php
View source
  1. <?php
  2. /**
  3. * This file is meant to demonstrate how to use hooks in the user module, as a fellow module developer.
  4. */
  5. /**
  6. * Sample hook other modules implement to register a user attribute.
  7. *
  8. * Simply return an array as illustrated below to register your
  9. * module's content types.
  10. *
  11. * IMPORTNAT: The attribute's machine name must be a valid variable name.
  12. * No spaces or symbols.
  13. *
  14. * Values are saved to the user_attributes table automatically as
  15. * name__fieldname
  16. * In the example below, the value would be saved under visa_status__visa
  17. *
  18. *
  19. */
  20. function hook_user_register_user_attributes() {
  21. $arr['visa_status'] = array(
  22. 'title' => 'Visa Status',
  23. 'description' => 'Determines the visa status of the user.',
  24. 'settings' => array(
  25. 'large_profile' => TRUE,
  26. 'large_profile_editable' => TRUE,
  27. 'mini_profile' => TRUE,
  28. 'mini_profile_editable' => FALSE,
  29. 'user_type' => 'student', // Can be "student", "faculty", or "all"
  30. ),
  31. );
  32. $arr['fun_field'] = array(
  33. 'title' => 'Enter any text you like:',
  34. 'settings' => array(
  35. 'user_type' => 'student',
  36. 'large_profile_editable' => TRUE,
  37. ),
  38. );
  39. // If we are in a popup (dialog)...
  40. if (@$_GET['window_mode'] == 'popup') {
  41. // We want to make sure we redirect to our handler URL, which will close the dialog.
  42. $arr['visa_status']['settings']['#redirect'] = array(
  43. 'path' => 'content-dialog-handle-after-save',
  44. 'query' => '',
  45. );
  46. $arr['fun_field']['settings']['#redirect'] = array(
  47. 'path' => 'content-dialog-handle-after-save',
  48. 'query' => '',
  49. );
  50. /**
  51. * // To do all attributes at once...
  52. foreach ($arr as $attr => $details) {
  53. $arr[$attr]['settings']['#redirect'] = array(
  54. 'path' => 'content-dialog-handle-after-save',
  55. 'query' => '',
  56. );
  57. }
  58. */
  59. }
  60. $fields = array();
  61. $fields['visa'] = array(
  62. 'type' => 'select',
  63. 'label' => 'Status:',
  64. 'options' => array(
  65. 'open' => t('Open'),
  66. 'closed' => t('Closed'),
  67. ),
  68. 'required' => TRUE,
  69. 'hide_please_select' => TRUE,
  70. 'weight' => 20,
  71. );
  72. $arr['visa_status']['fields'] = $fields;
  73. // ** required! **
  74. // Display settings
  75. $arr['visa_status']['display']['visa'] = array(
  76. 'label' => 'Visa Status:',
  77. 'value' => 'Selected: <i>@value</i>', // We can make use of HTML and replacement patterns.
  78. // @value means whatever was selected / typed in. @key means they key from an options field.
  79. // If 'value' is ommitted, we display @value by default.
  80. );
  81. //////////////////////////////////////////////////
  82. $fields = array();
  83. $fields['fun_field_val'] = array(
  84. 'type' => 'textfield',
  85. 'label' => 'Fun Field:',
  86. );
  87. $arr['fun_field']['display']['fun_field_val'] = array(
  88. 'label' => 'Fun Field:',
  89. );
  90. $arr['fun_field']['fields'] = $fields;
  91. return $arr;
  92. }
  93. /**
  94. * Allows other modules to alter the "profile items".
  95. *
  96. * @see user_alter_student_profile_items for a real-life demo of this hook.
  97. */
  98. function hook_alter_student_profile_items($bool_mini, &$extra_profile_items, $bool_balance = TRUE, $alt_section = "") {
  99. // Code goes here. See user_alter_student_profile_items for a real-life demo of this hook.
  100. }
  101. /**
  102. * This function lets us alter the value of a user attribute, before it is displayed to the user.
  103. *
  104. * Notice that value is passed by reference.
  105. */
  106. function hook_alter_user_attribute_display($attribute_id, &$value) {
  107. if ($attribute_id == "my_module__name_value") {
  108. $value = strtoupper($value);
  109. }
  110. }

Functions

Namesort descending Description
hook_alter_student_profile_items Allows other modules to alter the "profile items".
hook_alter_user_attribute_display This function lets us alter the value of a user attribute, before it is displayed to the user.
hook_user_register_user_attributes Sample hook other modules implement to register a user attribute.