pathauto_i18n_user.module 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @file
  4. * Provides tools for creating multilanguage aliases for users.
  5. */
  6. /**
  7. * Implements hook_form_BASE_FORM_ID_alter().
  8. */
  9. function pathauto_i18n_user_form_pathauto_patterns_form_alter(&$form, &$form_state) {
  10. $languages = language_list();
  11. $default_pattern = $form['user']['pathauto_user_pattern'];
  12. // Remove parents handlers.
  13. unset($default_pattern['#parents']);
  14. $form['user']['token_help']['#weight'] = 1;
  15. foreach ($languages as $language) {
  16. $pattern_name = 'pathauto_user_user_' . $language->language . '_pattern';
  17. $form['user'][$pattern_name] = $default_pattern;
  18. $form['user'][$pattern_name]['#title'] = t('Pattern for all @language user paths', array('@language' => $language->name));
  19. $form['user'][$pattern_name]['#default_value'] = variable_get($pattern_name, '');
  20. }
  21. }
  22. /**
  23. * Implements hook_form_alter().
  24. */
  25. function pathauto_i18n_user_form_alter(&$form, &$form_state, $form_id) {
  26. if ($form_id == 'user_register_form' || $form_id == 'user_profile_form') {
  27. pathauto_i18n_configuration_form($form, empty($form_state['user']) ? new StdClass() : $form_state['user'], 'user', 'user');
  28. }
  29. }
  30. /**
  31. * Implements hook_user_insert().
  32. */
  33. function pathauto_i18n_user_user_insert(&$edit, $account, $category) {
  34. pathauto_i18n_init_property($account, 'user', 'user');
  35. pathauto_i18n_process_entity_object($account, 'user', $account->pathauto_i18n_status, 'insert');
  36. }
  37. /**
  38. * Implements hook_user_update().
  39. */
  40. function pathauto_i18n_user_user_update(&$edit, $account, $category) {
  41. pathauto_i18n_init_property($account, 'user', 'user');
  42. pathauto_i18n_process_entity_object($account, 'user', $account->pathauto_i18n_status, 'update');
  43. }
  44. /**
  45. * Implements hook_user_delete().
  46. */
  47. function pathauto_i18n_user_user_delete($account) {
  48. pathauto_i18n_delete_settings($account->uid, 'user');
  49. }
  50. /**
  51. * Implements hook_user_load().
  52. */
  53. function pathauto_i18n_user_user_load($users) {
  54. // Attach pathauto i18n settings to user object.
  55. foreach ($users as $user) {
  56. $uids[] = $user->uid;
  57. }
  58. if (!empty($uids)) {
  59. $result = pathauto_i18n_load_settings($uids, 'user');
  60. foreach ($result as $record) {
  61. $users[$record->entity_id]->path['pathauto_i18n_status'] = $record->path_status;
  62. }
  63. }
  64. }
  65. /**
  66. * Implements hook_pathauto_alias_alter().
  67. */
  68. function pathauto_i18n_user_pathauto_alias_alter(&$alias, &$context) {
  69. $operations = array('insert', 'update', 'bulkupdate');
  70. // Skip insert of alias for all languages.
  71. if (!empty($context['module']) && $context['module'] == 'user' && in_array($context['op'], $operations)) {
  72. $entity = $context['data']['user'];
  73. // Run bulk update.
  74. $settings = pathauto_i18n_load_settings_single($entity->uid, 'user');
  75. if ($context['op'] == 'bulkupdate' && !empty($settings['path_status'])) {
  76. $entity->pathauto_i18n_status = $settings['path_status'];
  77. pathauto_i18n_process_entity_object($entity, 'user', $entity->pathauto_i18n_status, 'update');
  78. }
  79. if (isset($entity->pathauto_i18n_status) && $entity->pathauto_i18n_status && $context['language'] == LANGUAGE_NONE) {
  80. $alias = '';
  81. }
  82. }
  83. }
  84. /**
  85. * Implements hook_action_info().
  86. */
  87. function pathauto_i18n_user_action_info() {
  88. return array(
  89. 'pathauto_i18n_user_generate_alias' => array(
  90. 'type' => 'user',
  91. 'label' => t('Enable generation of aliases for all languages'),
  92. 'configurable' => FALSE,
  93. 'behavior' => array('changes_property'),
  94. 'triggers' => array(
  95. 'any',
  96. ),
  97. ),
  98. 'pathauto_i18n_user_remove_alias' => array(
  99. 'type' => 'user',
  100. 'label' => t('Disable generation of aliases for all languages'),
  101. 'configurable' => FALSE,
  102. 'behavior' => array('changes_property'),
  103. 'triggers' => array(
  104. 'any',
  105. ),
  106. ),
  107. );
  108. }
  109. /**
  110. * Sets the status of a pathauto_i18n_status to 1.
  111. *
  112. * @param object $user
  113. * A user object.
  114. *
  115. * @param array $context
  116. * (optional) Array of additional information about what triggered the action.
  117. * Not used for this action.
  118. *
  119. * @ingroup actions
  120. */
  121. function pathauto_i18n_user_generate_alias($user, $context = array()) {
  122. $user->pathauto_i18n_status = 1;
  123. }
  124. /**
  125. * Sets the status of a pathauto_i18n_status to 0.
  126. *
  127. * @param object $user
  128. * A user object.
  129. *
  130. * @param array $context
  131. * (optional) Array of additional information about what triggered the action.
  132. * Not used for this action.
  133. *
  134. * @ingroup actions
  135. */
  136. function pathauto_i18n_user_remove_alias($user, $context = array()) {
  137. $user->pathauto_i18n_status = 0;
  138. }