pathauto_i18n_taxonomy.module 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * @file
  4. * Provides tools for creating multilanguage aliases for taxonomy terms.
  5. */
  6. /**
  7. * Implements hook_form_BASE_FORM_ID_alter().
  8. */
  9. function pathauto_i18n_taxonomy_form_taxonomy_form_term_alter(&$form, &$form_state) {
  10. // Exclude elements from deletion confirm form.
  11. if ($form['#theme'] != 'confirm_form') {
  12. pathauto_i18n_configuration_form($form, $form_state['term'], 'term', $form_state['term']->vocabulary_machine_name);
  13. }
  14. }
  15. /**
  16. * Implements hook_form_BASE_FORM_ID_alter().
  17. */
  18. function pathauto_i18n_taxonomy_form_pathauto_patterns_form_alter(&$form, &$form_state) {
  19. $languages = language_list();
  20. $default_pattern_name = 'pathauto_taxonomy_term_pattern';
  21. $default_pattern = $form['taxonomy_term'][$default_pattern_name];
  22. // Remove parents handlers.
  23. unset($default_pattern['#parents']);
  24. $form['taxonomy_term']['token_help']['#weight'] = 1;
  25. foreach (element_children($form['taxonomy_term']) as $term_pattern_name) {
  26. if ($term_pattern_name != $default_pattern_name && $term_pattern_name != 'token_help') {
  27. foreach ($languages as $language) {
  28. $vocabulary = pathauto_i18n_taxonomy_get_vocabulary_name($term_pattern_name);
  29. if ($vocabulary) {
  30. $pattern_name = 'pathauto_taxonomy_term_' . $vocabulary . '_' . $language->language . '_pattern';
  31. $form['taxonomy_term'][$pattern_name] = $default_pattern;
  32. $form['taxonomy_term'][$pattern_name]['#title'] = t('Pattern for all @language @vocabulary paths', array('@language' => $language->name, '@vocabulary' => $vocabulary));
  33. $form['taxonomy_term'][$pattern_name]['#default_value'] = variable_get($pattern_name, '');
  34. }
  35. }
  36. }
  37. }
  38. }
  39. /**
  40. * Get vocabulary name from pattern name.
  41. */
  42. function pathauto_i18n_taxonomy_get_vocabulary_name($pattern_name) {
  43. $scope = str_replace(array('pathauto_taxonomy_term_', '_pattern'), array('', ''), $pattern_name);
  44. return !empty($scope) ? $scope : FALSE;
  45. }
  46. /**
  47. * Implements hook_taxonomy_term_insert().
  48. */
  49. function pathauto_i18n_taxonomy_taxonomy_term_insert($term) {
  50. pathauto_i18n_init_property($term, 'taxonomy', $term->vocabulary_machine_name);
  51. pathauto_i18n_process_entity_object($term, 'taxonomy_term', $term->path['pathauto_i18n_status'], 'insert');
  52. }
  53. /**
  54. * Implements hook_taxonomy_term_update().
  55. */
  56. function pathauto_i18n_taxonomy_taxonomy_term_update($term) {
  57. pathauto_i18n_init_property($term, 'taxonomy', $term->vocabulary_machine_name);
  58. pathauto_i18n_process_entity_object($term, 'taxonomy_term', $term->path['pathauto_i18n_status'], 'update');
  59. }
  60. /**
  61. * Implements hook_taxonomy_term_delete().
  62. */
  63. function pathauto_i18n_taxonomy_taxonomy_term_delete($term) {
  64. pathauto_i18n_delete_settings($term->tid, 'taxonomy_term');
  65. }
  66. /**
  67. * Implements hook_taxonomy_term_load().
  68. */
  69. function pathauto_i18n_taxonomy_taxonomy_term_load($terms) {
  70. // Attach pathauto i18n settings to taxonomy_term object.
  71. foreach ($terms as $term) {
  72. $tids[] = $term->tid;
  73. }
  74. if (!empty($tids)) {
  75. $result = pathauto_i18n_load_settings($tids, 'taxonomy_term');
  76. $settings = array();
  77. foreach ($result as $record) {
  78. $settings[$record->entity_id] = $record->path_status;
  79. }
  80. foreach ($terms as $tid => $term) {
  81. if (array_key_exists($tid, $settings)) {
  82. $terms[$tid]->path['pathauto_i18n_status'] = $settings[$tid];
  83. }
  84. else {
  85. $terms[$tid]->path['pathauto_i18n_status'] = 0;
  86. }
  87. }
  88. }
  89. }
  90. /**
  91. * Implements hook_pathauto_alias_alter().
  92. */
  93. function pathauto_i18n_taxonomy_pathauto_alias_alter(&$alias, &$context) {
  94. $operations = array('insert', 'update', 'bulkupdate');
  95. // Skip insert of alias for all languages.
  96. if (!empty($context['module']) && ($context['module'] == 'term' || $context['module'] == 'taxonomy_term') && in_array($context['op'], $operations)) {
  97. $entity = FALSE;
  98. // On creating taxonomy term has term key on updating taxonomy_term.
  99. if (!empty($context['data']['term'])) {
  100. $entity = $context['data']['term'];
  101. }
  102. if (!empty($context['data']['taxonomy_term'])) {
  103. $entity = $context['data']['taxonomy_term'];
  104. }
  105. // Run bulk update.
  106. if ($context['op'] == 'bulkupdate') {
  107. pathauto_i18n_taxonomy_taxonomy_term_update($entity);
  108. }
  109. if ($entity && isset($entity->path['pathauto_i18n_status']) && $entity->path['pathauto_i18n_status'] && $context['language'] == LANGUAGE_NONE) {
  110. $alias = '';
  111. }
  112. }
  113. }
  114. /**
  115. * Implements hook_form_alter().
  116. */
  117. function pathauto_i18n_taxonomy_form_alter(&$form, &$form_state, $form_id) {
  118. if ($form_id == 'taxonomy_form_term') {
  119. // Add pathauto value if pathauto_i18n_status TRUE.
  120. // Remove alias value to prevent overwriting.
  121. $term = FALSE;
  122. if (isset($form['#term'])) {
  123. $term = (object) $form['#term'];
  124. }
  125. if ($term && isset($term->path['pathauto_i18n_status']) && $term->path['pathauto_i18n_status']) {
  126. $form['path']['pathauto']['#default_value'] = TRUE;
  127. $form['path']['alias']['#default_value'] = '';
  128. }
  129. }
  130. }
  131. /**
  132. * Implements hook_module_implements_alter().
  133. */
  134. function pathauto_i18n_taxonomy_module_implements_alter(&$implementations, $hook) {
  135. // Move pathauto_i18n_taxonomy to the end of the list.
  136. // By default there not available pathauto value.
  137. if ($hook == 'form_alter') {
  138. $group = $implementations['pathauto_i18n_taxonomy'];
  139. unset($implementations['pathauto_i18n_taxonomy']);
  140. $implementations['pathauto_i18n_taxonomy'] = $group;
  141. }
  142. }
  143. /**
  144. * Implements hook_action_info().
  145. */
  146. function pathauto_i18n_taxonomy_action_info() {
  147. return array(
  148. 'pathauto_i18n_taxonomy_generate_alias' => array(
  149. 'type' => 'taxonomy_term',
  150. 'label' => t('Enable generation of aliases for all languages'),
  151. 'configurable' => FALSE,
  152. 'behavior' => array('changes_property'),
  153. 'triggers' => array(
  154. 'any',
  155. ),
  156. ),
  157. 'pathauto_i18n_taxonomy_remove_alias' => array(
  158. 'type' => 'taxonomy_term',
  159. 'label' => t('Disable generation of aliases for all languages'),
  160. 'configurable' => FALSE,
  161. 'behavior' => array('changes_property'),
  162. 'triggers' => array(
  163. 'any',
  164. ),
  165. ),
  166. );
  167. }
  168. /**
  169. * Sets the status of a pathauto_i18n_status to 1.
  170. *
  171. * @param object $term
  172. * A taxonomy term object.
  173. *
  174. * @param array $context
  175. * (optional) Array of additional information about what triggered the action.
  176. * Not used for this action.
  177. *
  178. * @ingroup actions
  179. */
  180. function pathauto_i18n_taxonomy_generate_alias($term, $context = array()) {
  181. $term->path['pathauto_i18n_status'] = 1;
  182. }
  183. /**
  184. * Sets the status of a pathauto_i18n_status to 0.
  185. *
  186. * @param object $term
  187. * A term object.
  188. *
  189. * @param array $context
  190. * (optional) Array of additional information about what triggered the action.
  191. * Not used for this action.
  192. *
  193. * @ingroup actions
  194. */
  195. function pathauto_i18n_taxonomy_remove_alias($term, $context = array()) {
  196. $term->path['pathauto_i18n_status'] = 0;
  197. }