summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJose Reyero2011-05-20 18:44:50 (GMT)
committer Jose Reyero2011-05-20 18:44:50 (GMT)
commita7cd9e05b32e729947e79e81e43c7a2aa56c547e (patch)
tree6d3a134f9703e657c4777a258e79520ae56cd93a
parentf607f0aaf265db2f649e302108e813ea34d2fc6e (diff)
Issue #1157484 by das-peter | skilip: Fixed Both delete translation sets when deleting a menu and when deleting menu items.
-rw-r--r--i18n_menu/i18n_menu.admin.inc4
-rw-r--r--i18n_menu/i18n_menu.module96
-rw-r--r--i18n_menu/i18n_menu.test115
-rw-r--r--i18n_taxonomy/i18n_taxonomy.module4
-rw-r--r--i18n_translation/i18n_translation.inc14
5 files changed, 216 insertions, 17 deletions
diff --git a/i18n_menu/i18n_menu.admin.inc b/i18n_menu/i18n_menu.admin.inc
index 3d2fa42..a417349 100644
--- a/i18n_menu/i18n_menu.admin.inc
+++ b/i18n_menu/i18n_menu.admin.inc
@@ -30,6 +30,8 @@ function i18n_menu_translation_overview($menu) {
function i18n_menu_translation_form($form, $form_state, $menu = NULL, $item = NULL, $translation_set = NULL) {
$form['translation_set'] = array('#type' => 'value', '#value' => $translation_set);
$translations = $translation_set ? $translation_set->get_translations() : array();
+ // What to do with title? Let's make it a hidden field for now, some tests relay on it
+ $form['title'] = array('#type' => 'hidden', '#default_value' => $translation_set ? $translation_set->title : '');
if ($item && ($lang = i18n_object_langcode($item))) {
$translations[$lang] = $item;
}
@@ -112,7 +114,7 @@ function i18n_menu_translation_form_submit($form, &$form_state) {
list($menu_name, $mlid) = explode(':', $item_name);
$translation_set->add_item(menu_link_load($mlid), $lang);
}
- $translation_set->title = $translation_set->get_title();
+ $translation_set->title = !empty($form_state['values']['title']) ? $form_state['values']['title'] : '';
$translation_set->save(TRUE);
drupal_set_message(t('The item translation has been saved.'));
break;
diff --git a/i18n_menu/i18n_menu.module b/i18n_menu/i18n_menu.module
index 958e248..c36d990 100644
--- a/i18n_menu/i18n_menu.module
+++ b/i18n_menu/i18n_menu.module
@@ -259,6 +259,9 @@ function i18n_menu_menu_insert($menu) {
* Implements hook_menu_update()
*/
function i18n_menu_menu_update($menu) {
+ // Stores the fields of menu links which need an update.
+ $update = array();
+
if (!isset($menu['i18n_mode'])) {
$menu['i18n_mode'] = I18N_MODE_NONE;
}
@@ -275,13 +278,35 @@ function i18n_menu_menu_update($menu) {
elseif ($menu['i18n_mode'] & I18N_MODE_LANGUAGE) {
$update['language'] = $menu['language'];
}
+
+ // Non translatable menu.
+ if (!i18n_menu_mode($menu['menu_name'], I18N_MODE_TRANSLATE)) {
+ $tsids = db_select('menu_links')
+ ->fields('menu_links', array('i18n_tsid'))
+ ->groupBy('i18n_tsid')
+ ->condition('menu_name', $menu['menu_name'])
+ ->condition('customized', 1)
+ ->condition('i18n_tsid', 0, '<>')
+ ->execute()
+ ->fetchCol(0);
+ if (!empty($tsids)) {
+ foreach($tsids as $tsid) {
+ if ($translation_set = i18n_translation_set_load($tsid)) {
+ $translation_set->delete();
+ }
+ }
+ }
+ $update['i18n_tsid'] = 0;
+ }
+
if (!empty($update)) {
db_update('menu_links')
- ->fields(array('language' => $menu['language']))
+ ->fields($update)
->condition('menu_name', $menu['menu_name'])
->condition('customized', 1)
->execute();
}
+
// Update strings, always add translation if no language
if (!i18n_object_langcode($menu)) {
i18n_string_object_update('menu', $menu);
@@ -289,7 +314,7 @@ function i18n_menu_menu_update($menu) {
}
/**
- * Implements hook_menu_insert()
+ * Implements hook_menu_delete()
*/
function i18n_menu_menu_delete($menu) {
i18n_string_object_remove('menu', $menu);
@@ -306,33 +331,76 @@ function i18n_menu_menu_link_insert($link) {
* Implements hook_menu_update()
*/
function i18n_menu_menu_link_update($link) {
+ // Stores the fields to update.
+ $fields = array();
+
+ // Multilingual menu links, translatable, it may be part of a translation set.
+ if (i18n_menu_mode($link['menu_name'], I18N_MODE_TRANSLATE) && isset($link['language'])) {
+ if (i18n_object_langcode($link)) {
+ if (!empty($link['translation_set'])) {
+ // Translation set comes as parameter, we may be creating a translation, add link to the set
+ $translation_set = $link['translation_set'];
+ $translation_set->add_item($link);
+ $translation_set->save(TRUE);
+ }
+ }
+ elseif ($link['language'] === LANGUAGE_NONE && !empty($link['original_item']['i18n_tsid'])) {
+ if ($translation_set = i18n_translation_set_load($link['original_item']['i18n_tsid'])) {
+ $translation_set->remove_item(NULL, $link['original_item']['language']);
+ // If there are no links left in this translation set, delete the set.
+ // Otherwise update the set.
+ if ($translation_set->get_translations()) {
+ $translation_set->update(TRUE);
+ }
+ else {
+ $translation_set->delete();
+ }
+ }
+ $fields['i18n_tsid'] = 0;
+ }
+ }
+
if (isset($link['language'])) {
+ $fields['language'] = $link['language'];
+ }
+
+ if (!empty($fields)) {
db_update('menu_links')
- ->fields(array('language' => $link['language']))
+ ->fields($fields)
->condition('mlid', $link['mlid'])
->execute();
}
+ // Update translatable strings if any
if (isset($link['options']['attributes']['title']) && !empty($link['customized']) && i18n_menu_mode($link['menu_name'], I18N_MODE_LOCALIZE) && !i18n_object_langcode($link)) {
i18n_string_update(array('menu', 'item', $link['mlid']), array(
'title' => $link['link_title'],
'description' => $link['options']['attributes']['title'],
));
}
-
- // Multilingual item, translatable, it may be part of a translation set
- if (i18n_menu_mode($link['menu_name'], I18N_MODE_TRANSLATE) && i18n_object_langcode($link) && !empty($link['translation_set'])) {
- // If the term translation set id is not set, we will create a new translation set.
- $translation_set = $link['translation_set'];
- // Add the current term to the translation set.
- $translation_set->add_item($link);
- $translation_set->save(TRUE);
- }
}
/**
- * Implements hook_menu_insert()
+ * Implements hook_menu_delete()
*/
function i18n_menu_menu_link_delete($link) {
+ // If a translation set exists for this link, remove this link from the set.
+ if (!empty($link['i18n_tsid'])) {
+ if ($translation_set = i18n_translation_set_load($link['i18n_tsid'])) {
+ $translation_set->get_translations();
+
+ unset($translation_set->translations[$link['language']]);
+
+ // If there are no links left in this translation set, delete the set.
+ // Otherwise update the set.
+ if (empty($translation_set->translations)) {
+ $translation_set->delete();
+ }
+ else {
+ $translation_set->save(TRUE);
+ }
+ }
+ }
+
i18n_string_object_remove('menu_link', $link);
}
@@ -620,7 +688,7 @@ function i18n_menu_form_menu_edit_item_alter(&$form, &$form_state) {
// create a form element for storing the translation set of the source term.
if (isset($_GET['translation']) && isset($_GET['target']) && ($source_item = menu_link_load($_GET['translation']))) {
if (!empty($source_item['i18n_tsid'])) {
- $translation_set = i18n_translation_set_load($i18n_tsid);
+ $translation_set = i18n_translation_set_load($source_item['i18n_tsid']);
}
else {
// Create object and stick the source information in the translation set
diff --git a/i18n_menu/i18n_menu.test b/i18n_menu/i18n_menu.test
index c2aecd0..493d304 100644
--- a/i18n_menu/i18n_menu.test
+++ b/i18n_menu/i18n_menu.test
@@ -79,6 +79,103 @@ class i18nMenuTestCase extends Drupali18nTestCase {
}
/**
+ * Tests if the translation set management works.
+ */
+ function testMenuTranslationSets() {
+ $menu = $this->createMenu(array('i18n_mode' => I18N_MODE_MULTIPLE));
+
+ $neutral_item = $this->createMenuLink(array('menu_name' => $menu['menu_name']));
+ $default_item = $this->createMenuLink(array('menu_name' => $menu['menu_name'], 'language' => $this->default_language));
+ $secondary_item = $this->createMenuLink(array('menu_name' => $menu['menu_name'], 'language' => $this->secondary_language));
+
+ $translationset_edit = array(
+ 'translations[' . $this->default_language . ']' => $default_item['menu_name'] . ':' . $default_item['mlid'],
+ 'translations[' . $this->secondary_language . ']' => $secondary_item['menu_name'] . ':' . $secondary_item['mlid'],
+ );
+ $translation_set = $this->createMenuLinkTranslationSet($menu['menu_name'], $translationset_edit);
+
+ // Check if the overview works
+ $this->drupalGet('admin/structure/menu/manage/' . $menu['menu_name'] . '/list/sets');
+ $link = $this->xpath('//*/a[contains(@href,"admin/structure/menu/manage/' . $menu['menu_name'] . '/list/sets/edit/' . $translation_set->tsid . '")]');
+ $this->assertTrue(!empty($link),'Created translation-set found.');
+
+ // Check if the edit mode works
+ $this->drupalGet('admin/structure/menu/manage/' . $menu['menu_name'] . '/list/sets/edit/' . $translation_set->tsid);
+ $this->assertFieldByXPath(
+ "//*[@id='edit-translations-" . $this->default_language . "']/option[@selected]/@value",
+ $menu['menu_name'] . ':' . $default_item['mlid'],
+ 'Expected option selection for language ' . $this->default_language . ' found.'
+ );
+ $this->assertFieldByXPath(
+ "//*[@id='edit-translations-" . $this->secondary_language . "']/option[@selected]/@value",
+ $menu['menu_name'] . ':' . $secondary_item['mlid'],
+ 'Expected option selection for language ' . $this->secondary_language . ' found.'
+ );
+ }
+
+ /**
+ * Tests if on a switch from translatable to non translatable the translation
+ * sets and links are cleaned up.
+ */
+ function testMenuTranslateLocalizeSwitchToNonTranslatable() {
+ // Test filtering for menu blocks.
+ $menu = $this->createMenu(array('i18n_mode' => I18N_MODE_MULTIPLE));
+
+ // Check current menu mode
+ $this->drupalGet('admin/structure/menu/manage/' . $menu['menu_name'] . '/edit');
+ $this->assertFieldByName('i18n_mode', I18N_MODE_MULTIPLE, 'Menu i18n mode set to I18N_MODE_MULTIPLE');
+
+ // Setup menu links for testing.
+ $neutral_item = $this->createMenuLink(array('menu_name' => $menu['menu_name']));
+ $default_item = $this->createMenuLink(array('menu_name' => $menu['menu_name'], 'language' => $this->default_language));
+ $secondary_item = $this->createMenuLink(array('menu_name' => $menu['menu_name'], 'language' => $this->secondary_language));
+
+ $translationset_edit = array(
+ 'translations[' . $this->default_language . ']' => $default_item['menu_name'] . ':' . $default_item['mlid'],
+ 'translations[' . $this->secondary_language . ']' => $secondary_item['menu_name'] . ':' . $secondary_item['mlid'],
+ );
+ $translation_set = $this->createMenuLinkTranslationSet($menu['menu_name'], $translationset_edit);
+ $tsid = $translation_set->tsid;
+
+ // Test language mode switch
+ $edit = array(
+ 'i18n_mode' => I18N_MODE_LANGUAGE,
+ 'language' => $this->secondary_language,
+ );
+ $this->drupalPost('admin/structure/menu/manage/' . $menu['menu_name'] . '/edit', $edit, t('Save'));
+ $this->drupalGet('admin/structure/menu/manage/' . $menu['menu_name'] . '/edit');
+ $this->assertFieldByName('i18n_mode', I18N_MODE_LANGUAGE, 'Menu i18n mode changed to I18N_MODE_LANGUAGE');
+
+ $this->assertTrue(!empty(i18n_translation_set_load($tsid)->tsid), 'Translation set kept.');
+
+ $menu_link_languages = db_select('menu_links')
+ ->fields('menu_links', array('language'))
+ ->condition('menu_name', $menu['menu_name'])
+ ->groupBy('language')
+ ->execute()
+ ->fetchCol();
+ $this->assertTrue((count($menu_link_languages) == 1 && reset($menu_link_languages) === $this->secondary_language), 'Menu link language changed to menu language.');
+
+ // Test cleanup.
+ $edit = array(
+ 'i18n_mode' => I18N_MODE_NONE,
+ );
+ $this->drupalPost('admin/structure/menu/manage/' . $menu['menu_name'] . '/edit', $edit, t('Save'));
+ $this->drupalGet('admin/structure/menu/manage/' . $menu['menu_name'] . '/edit');
+ $this->assertFieldByName('i18n_mode', I18N_MODE_NONE, 'Menu i18n mode changed to I18N_MODE_NONE');
+ $translation_sets = entity_load('i18n_translation', FALSE, array('tsid' => $tsid), TRUE);
+ $this->assertTrue(empty($translation_sets), 'Translation set deleted.');
+
+ $menu_link_languages = db_select('menu_links')
+ ->fields('menu_links', array('language'))
+ ->condition('menu_name', $menu['menu_name'])
+ ->groupBy('language')
+ ->execute()
+ ->fetchCol();
+ $this->assertTrue(((count($menu_link_languages) == 1) && reset($menu_link_languages) === LANGUAGE_NONE), 'Menu link language switched to LANGUAGE_NONE.');
+ }
+
+ /**
* Helper function to create a menu.
*/
function createMenu($edit = array()) {
@@ -104,4 +201,22 @@ class i18nMenuTestCase extends Drupali18nTestCase {
return menu_link_load(menu_link_save($item));
}
+
+ /**
+ * Helper function to create a translation set.
+ */
+ function createMenuLinkTranslationSet($menu_name, $edit = array()) {
+ $edit += array(
+ 'title' => $this->randomName(16),
+ );
+ $this->drupalPost('admin/structure/menu/manage/' . $menu_name . '/list/sets/add', $edit, t('Save'));
+
+ // Load translation set entity.
+ $entity = entity_load('i18n_translation', FALSE, array('title' => $edit['title']), TRUE);
+ if (empty($entity)) {
+ $this->fail('Could not create a translation set.', 'i18n_translation');
+ return FALSE;
+ }
+ return reset($entity);
+ }
}
diff --git a/i18n_taxonomy/i18n_taxonomy.module b/i18n_taxonomy/i18n_taxonomy.module
index 01973a7..1443dfb 100644
--- a/i18n_taxonomy/i18n_taxonomy.module
+++ b/i18n_taxonomy/i18n_taxonomy.module
@@ -568,7 +568,7 @@ function i18n_taxonomy_taxonomy_term_update($term) {
if (i18n_taxonomy_vocabulary_mode($term->vid, I18N_MODE_TRANSLATE)) {
if ($term->language !== LANGUAGE_NONE) {
$vocabulary = taxonomy_vocabulary_load($term->vid);
-
+
// If the term translation set id is not set, we will create a new translation set.
$translation_set = isset($term->translation_set) ? $term->translation_set : i18n_translation_set_create('taxonomy_term', $vocabulary->machine_name);
$translation_set->get_translations();
@@ -639,7 +639,7 @@ function i18n_taxonomy_taxonomy_vocabulary_update($vocabulary) {
}
/**
- * Implements hook_taxonomy_vocabulary_insert()
+ * Implements hook_taxonomy_vocabulary_delete()
*/
function i18n_taxonomy_taxonomy_vocabulary_delete($vocabulary) {
i18n_string_object_remove('taxonomy_vocabulary', $vocabulary);
diff --git a/i18n_translation/i18n_translation.inc b/i18n_translation/i18n_translation.inc
index a1a601e..8ca85cf 100644
--- a/i18n_translation/i18n_translation.inc
+++ b/i18n_translation/i18n_translation.inc
@@ -9,6 +9,8 @@ class i18n_translation_set {
public $bundle = '';
public $status = 0;
public $master_id = 0;
+ // It may optionally have a title
+ public $title = '';
// Translations indexed by language
public $translations = NULL;
@@ -252,6 +254,7 @@ class i18n_translation_set {
$item = $this->check_item($item);
$langcode = $langcode ? $langcode : i18n_object_langcode($item);
if ($item && $langcode) {
+ $this->get_translations();
$this->translations[$langcode] = $item;
return TRUE;
}
@@ -260,6 +263,17 @@ class i18n_translation_set {
}
}
/**
+ * Remove item / language from translation set
+ */
+ public function remove_item($item = NULL, $langcode = NULL) {
+ $this->get_translations();
+ $langcode = $langcode ? $langcode : i18n_object_langcode($item);
+ if ($langcode && isset($this->translations[$langcode])) {
+ unset($this->translations[$langcode]);
+ }
+ }
+
+ /**
* Check item has translation set property
*/
public function check_item($item) {