rules_i18n.module 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * @file
  4. * Rules i18n integration.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function rules_i18n_rules_ui_menu_alter(&$items, $base_path, $base_count) {
  10. $items[$base_path . '/manage/%rules_config/edit'] = array(
  11. 'title' => 'Edit',
  12. 'type' => MENU_DEFAULT_LOCAL_TASK,
  13. 'weight' => -100,
  14. );
  15. // For reaction-rules i18n generates the menu items, for the others we provide
  16. // further i18n menu items for all other base paths.
  17. if ($base_path != 'admin/config/workflow/rules/reaction') {
  18. $items[$base_path . '/manage/%rules_config/translate'] = array(
  19. 'title' => 'Translate',
  20. 'page callback' => 'i18n_page_translate_localize',
  21. 'page arguments' => array('rules_config', $base_count + 1),
  22. 'access callback' => 'i18n_object_translate_access',
  23. 'access arguments' => array('rules_config', $base_count + 1),
  24. 'type' => MENU_LOCAL_TASK,
  25. 'file' => 'i18n.pages.inc',
  26. 'file path' => drupal_get_path('module', 'i18n'),
  27. 'weight' => 10,
  28. );
  29. $items[$base_path . '/manage/%rules_config/translate/%i18n_language'] = array(
  30. 'title' => 'Translate',
  31. 'page callback' => 'i18n_page_translate_localize',
  32. 'page arguments' => array('rules_config', $base_count + 1, $base_count + 3),
  33. 'access callback' => 'i18n_object_translate_access',
  34. 'access arguments' => array('rules_config', $base_count),
  35. 'type' => MENU_CALLBACK,
  36. 'file' => 'i18n.pages.inc',
  37. 'file path' => drupal_get_path('module', 'i18n'),
  38. 'weight' => 10,
  39. );
  40. }
  41. }
  42. /**
  43. * Implements hook_entity_info_alter().
  44. */
  45. function rules_i18n_entity_info_alter(&$info) {
  46. // Enable i18n support via the entity API.
  47. $info['rules_config']['i18n controller class'] = 'RulesI18nStringController';
  48. }
  49. /**
  50. * Implements hook_rules_config_insert().
  51. */
  52. function rules_i18n_rules_config_insert($rules_config) {
  53. // Do nothing when rebuilding defaults to avoid multiple cache rebuilds.
  54. // @see rules_i18n_rules_config_defaults_rebuild()
  55. if (!empty($rules_config->is_rebuild)) {
  56. return;
  57. }
  58. i18n_string_object_update('rules_config', $rules_config);
  59. }
  60. /**
  61. * Implements hook_rules_config_update().
  62. */
  63. function rules_i18n_rules_config_update($rules_config, $original = NULL) {
  64. // Do nothing when rebuilding defaults to avoid multiple cache rebuilds.
  65. // @see rules_i18n_rules_config_defaults_rebuild()
  66. if (!empty($rules_config->is_rebuild)) {
  67. return;
  68. }
  69. $original = $original ? $original : $rules_config->original;
  70. // Account for name changes.
  71. if ($original->name != $rules_config->name) {
  72. i18n_string_update_context("rules:rules_config:{$original->name}:*", "rules:rules_config:{$rules_config->name}:*");
  73. }
  74. // We need to remove the strings of any disappeared properties, i.e. strings
  75. // from translatable parameters of deleted actions.
  76. // i18n_object() uses a static cache per config, so bypass it to wrap the
  77. // original entity.
  78. $object_key = i18n_object_key('rules_config', $original);
  79. $old_i18n_object = new RulesI18nStringObjectWrapper('rules_config', $object_key, $original);
  80. $old_strings = $old_i18n_object->get_strings(array('empty' => TRUE));
  81. // Note: For the strings to have updated values, the updated entity needs to
  82. // be handled last due to i18n's cache.
  83. $strings = i18n_object('rules_config', $rules_config)->get_strings(array('empty' => TRUE));
  84. foreach (array_diff_key($old_strings, $strings) as $name => $string) {
  85. $string->remove(array('empty' => TRUE));
  86. }
  87. // Now update the remaining strings.
  88. foreach ($strings as $string) {
  89. $string->update(array('empty' => TRUE, 'update' => TRUE));
  90. }
  91. }
  92. /**
  93. * Implements hook_rules_config_delete().
  94. */
  95. function rules_i18n_rules_config_delete($rules_config) {
  96. // Only react on real delete, not revert.
  97. if (!$rules_config->hasStatus(ENTITY_IN_CODE)) {
  98. i18n_string_object_remove('rules_config', $rules_config);
  99. }
  100. }
  101. /**
  102. * Implements hook_rules_config_defaults_rebuild().
  103. */
  104. function rules_i18n_rules_config_defaults_rebuild($rules_configs, $originals) {
  105. // Once all defaults have been rebuilt, update all i18n strings at once. That
  106. // way we build the rules cache once the rebuild is complete and avoid
  107. // rebuilding caches for each updated rule.
  108. foreach ($rules_configs as $name => $rule_config) {
  109. if (empty($originals[$name])) {
  110. rules_i18n_rules_config_insert($rule_config);
  111. }
  112. else {
  113. rules_i18n_rules_config_update($rule_config, $originals[$name]);
  114. }
  115. }
  116. }