pathauto_i18n.module 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * @file
  4. * Provides common functions and callbacks for pathauto_i18n submodules.
  5. *
  6. * Does not contain any hooks or provide functionality on its own.
  7. */
  8. /**
  9. * Insert entity to pathauto_i18n table and generate alias if necessary.
  10. */
  11. function pathauto_i18n_process_entity_object($entity, $entity_type, $pathauto_i18n_status, $op) {
  12. // Get entity properties.
  13. list($entity_id, , $bundle) = entity_extract_ids($entity_type, $entity);
  14. // Insert pathauto_i18n settings.
  15. if (!empty($entity_id) && !empty($bundle) && !empty($entity_type)) {
  16. pathauto_i18n_insert_settings($entity_id, $entity_type, $bundle, $pathauto_i18n_status);
  17. // Build aliases in it necessary.
  18. if ($pathauto_i18n_status) {
  19. pathauto_i18n_update_alias($entity, $entity_type, $bundle, $op);
  20. }
  21. }
  22. }
  23. /**
  24. * Update the URL aliases for all available languages.
  25. *
  26. * @see pathauto_field_attach_form()
  27. */
  28. function pathauto_i18n_update_alias($entity, $entity_type, $bundle, $op) {
  29. // Generate aliases.
  30. module_load_include('inc', 'pathauto');
  31. $languages = language_list();
  32. $langcode = pathauto_entity_language($entity_type, $entity);
  33. $uri = entity_uri($entity_type, $entity);
  34. foreach ($languages as $language) {
  35. // Skipping a creating alias for node language, it created automatically.
  36. if (empty($entity->language) || $langcode != $language->language) {
  37. pathauto_create_alias(
  38. $entity_type,
  39. $op,
  40. $uri['path'],
  41. array($entity_type => $entity),
  42. $bundle,
  43. $language->language
  44. );
  45. }
  46. }
  47. }
  48. /**
  49. * Returns the configured default value for a bundle.
  50. */
  51. function pathauto_i18n_get_bundle_default($entity_type, $bundle, $default = 1) {
  52. return variable_get('pathauto_i18n_default_' . $entity_type . '_' . $bundle, $default);
  53. }
  54. /**
  55. * Sets a default value for a bundle.
  56. */
  57. function pathauto_i18n_set_bundle_default($entity_type, $bundle, $value) {
  58. variable_set('pathauto_i18n_default_' . $entity_type . '_' . $bundle, $value);
  59. }
  60. /**
  61. * Attach Pathauto i18n field configuration to form.
  62. *
  63. * @param array $form
  64. * Form to attach.
  65. *
  66. * @param object $entity
  67. * Entity object.
  68. *
  69. * @param string $entity_type
  70. * Entity type.
  71. *
  72. * @see pathauto_field_attach_form()
  73. */
  74. function pathauto_i18n_configuration_form(&$form, $entity, $entity_type, $bundle) {
  75. $access = user_access('create url aliases') || user_access('administer url aliases');
  76. if ($entity && $access) {
  77. $default = pathauto_i18n_get_bundle_default($entity_type, $bundle);
  78. $form['path']['pathauto_i18n_status'] = array(
  79. '#type' => 'checkbox',
  80. '#title' => t('Generate automatic URL alias for all languages'),
  81. '#description' => t('Allows you to generate aliases for all available languages.', array('@entity_type' => $entity_type)),
  82. '#default_value' => isset($entity->path['pathauto_i18n_status']) ? $entity->path['pathauto_i18n_status'] : $default,
  83. '#weight' => -0.99,
  84. );
  85. $form['path']['pathauto_i18n_undefined_language_tip'] = array(
  86. '#type' => 'item',
  87. '#markup' => t('URL alias for "Language neutral" <strong>won\'t be created</strong>, because you use automatic alias.') . '</strong>',
  88. '#weight' => -0.98,
  89. '#states' => array(
  90. 'visible' => array(
  91. 'select[name="language"]' => array('value' => LANGUAGE_NONE),
  92. 'input[name="path[pathauto]"]' => array('checked' => TRUE),
  93. 'input[name="path[pathauto_i18n_status]"]' => array('checked' => TRUE),
  94. ),
  95. ),
  96. );
  97. $form['path']['pathauto_i18n_undefined_language_custom_tip'] = array(
  98. '#type' => 'item',
  99. '#markup' => t('URL alias for "Language neutral" <strong>will be created</strong>, because you use custom alias.'),
  100. '#weight' => -0.98,
  101. '#states' => array(
  102. 'visible' => array(
  103. 'select[name="language"]' => array('value' => LANGUAGE_NONE),
  104. 'input[name="path[pathauto]"]' => array('checked' => FALSE),
  105. 'input[name="path[pathauto_i18n_status]"]' => array('checked' => TRUE),
  106. ),
  107. ),
  108. );
  109. }
  110. }
  111. /**
  112. * Insert settings for entity.
  113. */
  114. function pathauto_i18n_insert_settings($entity_id, $entity_type, $bundle, $path_status) {
  115. db_merge('pathauto_i18n')
  116. ->key(array('entity_id' => $entity_id, 'entity_type' => $entity_type))
  117. ->fields(
  118. array(
  119. 'entity_id' => $entity_id,
  120. 'entity_type' => $entity_type,
  121. 'bundle' => $bundle,
  122. 'path_status' => $path_status,
  123. )
  124. )
  125. ->execute();
  126. }
  127. /**
  128. * Load settings for entity.
  129. */
  130. function pathauto_i18n_load_settings($ids, $entity_type) {
  131. return db_select('pathauto_i18n', 'p')
  132. ->fields('p', array(
  133. 'entity_id',
  134. 'path_status',
  135. ))
  136. ->condition('p.entity_id', $ids, 'IN')
  137. ->condition('p.entity_type', $entity_type)
  138. ->execute();
  139. }
  140. /**
  141. * Load settings for entity.
  142. */
  143. function pathauto_i18n_load_settings_single($id, $entity_type) {
  144. return db_select('pathauto_i18n', 'p')
  145. ->fields('p', array(
  146. 'entity_id',
  147. 'path_status',
  148. ))
  149. ->condition('p.entity_id', $id)
  150. ->condition('p.entity_type', $entity_type)
  151. ->execute()
  152. ->fetchAssoc();
  153. }
  154. /**
  155. * Delete settings for certain entity.
  156. */
  157. function pathauto_i18n_delete_settings($entity_id, $entity_type) {
  158. db_delete('pathauto_i18n')
  159. ->condition('entity_id', $entity_id)
  160. ->condition('entity_type', $entity_type)
  161. ->execute();
  162. }
  163. /**
  164. * Implements hook_form_FORM_ID_alter().
  165. *
  166. * Make pathauto_ignore_words global and add fields for languages.
  167. */
  168. function pathauto_i18n_form_pathauto_settings_form_alter(&$form, &$form_state, $form_id) {
  169. module_load_include('inc', 'pathauto');
  170. $languages = language_list();
  171. $form['pathauto_ignore_words']['#title'] = t('Strings to Remove - @language', array('@language' => t('Global')));
  172. foreach ($languages as $language) {
  173. $key = 'pathauto_ignore_words_' . $language->language . '_language';
  174. $form[$key] = array(
  175. '#type' => 'textarea',
  176. '#title' => t('Strings to Remove - @language', array('@language' => $language->name)),
  177. '#default_value' => variable_get($key, ''),
  178. '#wysiwyg' => FALSE,
  179. );
  180. }
  181. }
  182. /**
  183. * Clean up a string segment for certain language.
  184. */
  185. function pathauto_i18n_cleanstring($language, $string) {
  186. $ignore_words = variable_get('pathauto_ignore_words_' . $language . '_language', '');
  187. $ignore_words_regex = preg_replace(array('/^[,\s]+|[,\s]+$/', '/[,\s]+/'), array('', '\b|\b'), $ignore_words);
  188. if ($ignore_words_regex) {
  189. $ignore_words_regex = '\b' . $ignore_words_regex . '\b';
  190. if (function_exists('mb_eregi_replace')) {
  191. $ignore_words_callback = 'mb_eregi_replace';
  192. }
  193. else {
  194. $ignore_words_callback = 'preg_replace';
  195. $ignore_words_regex = '/' . $ignore_words_regex . '/i';
  196. }
  197. if (!empty($ignore_words_regex) && !empty($ignore_words_callback)) {
  198. $words_removed = $ignore_words_callback($ignore_words_regex, '', $string);
  199. if (drupal_strlen(trim($words_removed)) > 0) {
  200. $string = $words_removed;
  201. }
  202. }
  203. }
  204. return $string;
  205. }
  206. /**
  207. * Implements hook_tokens_alter().
  208. */
  209. function pathauto_i18n_tokens_alter(&$replacements, array $context) {
  210. // @todo need tests.
  211. if (!empty($context['options']['pathauto'])) {
  212. foreach ($context['tokens'] as $name => $original) {
  213. if (!empty($replacements[$original]) && !empty($context['options']['language']->language) && $context['options']['language']->language != LANGUAGE_NONE) {
  214. $replacements[$original] = pathauto_i18n_cleanstring($context['options']['language']->language, $replacements[$original]);
  215. }
  216. }
  217. }
  218. }
  219. /**
  220. * Init the property.
  221. */
  222. function pathauto_i18n_init_property(&$entity, $type, $bundle) {
  223. $default = pathauto_i18n_get_bundle_default($type, $bundle);
  224. switch ($type) {
  225. case 'user':
  226. $entity->pathauto_i18n_status = isset($entity->pathauto_i18n_status) ? $entity->pathauto_i18n_status : $default;
  227. break;
  228. default:
  229. $entity->path['pathauto_i18n_status'] = isset($entity->path['pathauto_i18n_status']) ? $entity->path['pathauto_i18n_status'] : $default;
  230. break;
  231. }
  232. }