diff -u b/controller/tmgmt.controller.job_item.inc b/controller/tmgmt.controller.job_item.inc --- b/controller/tmgmt.controller.job_item.inc +++ b/controller/tmgmt.controller.job_item.inc @@ -17,7 +17,7 @@ */ public function create(array $values = array()) { $entity = parent::create($values); - $this->doCountWords($entity); + $this->countWords($entity); return $entity; } @@ -26,7 +26,7 @@ * * @param TMGMTJobItem $item */ - public function doCountWords(TMGMTJobItem &$item) { + public function countWords(TMGMTJobItem &$item) { // Set translatable data from the current entity to calculate words. if (empty($item->data)) { //$entity = entity_load_single($item->item_type, $item->item_id); @@ -60,7 +60,7 @@ */ public function save($entity, DatabaseTransaction $transaction = NULL) { $entity->changed = REQUEST_TIME; - $this->doCountWords($entity); + $this->countWords($entity); return parent::save($entity, $transaction); } diff -u b/entity/tmgmt.entity.job.inc b/entity/tmgmt.entity.job.inc --- b/entity/tmgmt.entity.job.inc +++ b/entity/tmgmt.entity.job.inc @@ -197,6 +197,17 @@ } /** + * Add a given TMGMTJobItem to this job. + * + * @param TMGMTJobItem $job + * The job item to add. + */ + function addJobItem(TMGMTJobItem &$item) { + $item->tjid = $this->tjid; + $item->save(); + } + + /** * Add a log message for this job. * * @param $message @@ -782,8 +793,8 @@ $keys = array_keys($suggestions); foreach ($keys as $i) { $suggestion = $suggestions[$i]; - if (is_array($suggestion) && isset($suggestion['tmgmt_job']) && ($suggestion['tmgmt_job'] instanceof TMGMTJobItem)) { - $jobItem = $suggestion['tmgmt_job']; + if (is_array($suggestion) && isset($suggestion['job_item']) && ($suggestion['job_item'] instanceof TMGMTJobItem)) { + $jobItem = $suggestion['job_item']; // Items with no words to translate should not be presented. if ($jobItem->getWordCount() <= 0) { unset($suggestions[$i]); diff -u b/sources/entity/tmgmt_entity.module b/sources/entity/tmgmt_entity.module --- b/sources/entity/tmgmt_entity.module +++ b/sources/entity/tmgmt_entity.module @@ -221,14 +221,11 @@ $entity_types = array_filter(variable_get('entity_translation_entity_types', array())); // Create a link to the referenced entity to show in the table. - $entity_link = l( t('@title (@type)', array( - '@title' => check_plain($entity->title), - '@type' => check_plain($entity->type), - )), 'node/' . $entity->vid, array('attributes' => array('target' => '_blank')) ); + $entity_uri = entity_uri($item->item_type, $entity); // Loop over all fields, check if they are NOT translatable. Only if a - // field is not translatable we may have a referenced node/entity. - // If so, check for a valid type. + // field is not translatable we may suggest a referenced entity. If so, + // check for a supported field type (image and file currently here). foreach ($field_instances as $instance) { $field = field_info_field($instance['field_name']); if (isset($field['translatable']) && !$field['translatable']) { @@ -236,32 +233,32 @@ switch ($field_type) { case 'file': case 'image': - // Images and files must be a translatable entity type. + // 'File' (and images) must be translatable entity types. // Other files we not suggest here. Get all field items from the // current entity and suggest them as translatable. - if (isset($entity_types['file'])) { - $field_name = $field['field_name']; - if ($field_items = field_get_items($item->item_type, $entity, $field_name)) { - // Add all files as a suggestion. - foreach ($field_items as $field_item) { - $file_entity = entity_load_single('file', $field_item['fid']); + $field_name = $field['field_name']; + if (isset($entity_types['file']) && ($field_items = field_get_items($item->item_type, $entity, $field_name))) { + // Add all files as a suggestion. + foreach ($field_items as $field_item) { + $file_entity = entity_load_single('file', $field_item['fid']); - // Check if there is already a translation available for this - // file. If so, just continue with the next file. - $handler = entity_translation_get_handler('file', $file_entity); - if ($handler instanceof EntityTranslationHandlerInterface) { - $translations = $handler->getTranslations(); - if (isset($translations->data[$job->target_language])) { - continue; - } + // Check if there is already a translation available for this + // file. If so, just continue with the next file. + $handler = entity_translation_get_handler('file', $file_entity); + if ($handler instanceof EntityTranslationHandlerInterface) { + $translations = $handler->getTranslations(); + if (isset($translations->data[$job->target_language])) { + continue; } - - // Add the translation as a suggestion. - $suggestions[] = array( - 'tmgmt_job' => tmgmt_job_item_create('entity', 'file', $file_entity->fid), - 'referenced' => t('Suggested from !entity', array('!entity' => $entity_link)), - ); } + + // Add the translation as a suggestion. + $suggestions[] = array( + 'job_item' => tmgmt_job_item_create('entity', 'file', $file_entity->fid), + 'suggested' => t('Suggested @type entity', array('@type' => $field_type)), + 'entity_uri' => $entity_uri, + 'from_job' => $job->tjid, + ); } } break; reverted: --- b/sources/i18n_string/tmgmt_i18n_string.test +++ a/sources/i18n_string/tmgmt_i18n_string.test @@ -20,7 +20,7 @@ * Overrides SimplenewsTestCase::setUp() */ function setUp() { + parent::setUp(array('tmgmt_i18n_string', 'i18n_menu', 'i18n_taxonomy')); - parent::setUp(array('tmgmt_i18n_string', 'i18n_menu', 'i18n_taxonomy', 'i18n_string')); $this->translator = $this->createTranslator(); } @@ -42,7 +42,7 @@ $job->settings = array(); $job->save(); + $item1 = $job->addItem('i18n_string', 'i18n_string', $string_object_name); - $item1 = $job->addItem('i18n_string', 'node', $string_object_name); $job->requestTranslation(); foreach ($job->getItems() as $item) { reverted: --- b/tests/tmgmt.base.test +++ a/tests/tmgmt.base.test @@ -160,10 +160,6 @@ // Assert that the translator was assigned a tid. $this->assertTrue($job->tjid > 0); - - // Create two nodes to translate. - $this->drupalCreateNode(); - $this->drupalCreateNode(); return $job; } reverted: --- b/tests/tmgmt.crud.test +++ a/tests/tmgmt.crud.test @@ -74,8 +74,8 @@ $this->assertEqual($job->reference, $loaded_job->reference); // Test the job items. + $item1 = $job->addItem('test_source', 'type', 5); + $item2 = $job->addItem('test_source', 'type', 4); - $item1 = $job->addItem('test_source', 'node', 1); - $item2 = $job->addItem('test_source', 'node', 2); // Load and compare the items. $items = $job->getItems(); @@ -101,8 +101,8 @@ $job = $this->createJob(); $job->translator = $translator->name; $job->save(); + $item1 = $job->addItem('test_source', 'type', 5); + $item2 = $job->addItem('test_source', 'type', 4); - $item1 = $job->addItem('test_source', 'node', 1); - $item2 = $job->addItem('test_source', 'node', 2); $result = $item1->addRemoteMapping($data_key, 'id11', array('remote_identifier_2' => 'id12', 'remote_identifier_3' => 'id13')); $this->assertEqual($result, SAVED_NEW); @@ -167,8 +167,8 @@ $job = $this->createJob(); // Add some test items. + $item1 = $job->addItem('test_source', 'type', 5); + $item2 = $job->addItem('test_source', 'type', 4); - $item1 = $job->addItem('test_source', 'node', 1); - $item2 = $job->addItem('test_source', 'node', 2); // Test single load callback. $item = tmgmt_job_item_load($item1->tjiid); @@ -226,7 +226,7 @@ $this->assertEqual(0, $job->getWordCount()); // Add a test items. + $job_item1 = tmgmt_job_item_create('plugin', 'type', 4, array('tjid' => $job->tjid)); - $job_item1 = tmgmt_job_item_create('entity', 'node', 1, array('tjid' => $job->tjid)); $job_item1->save(); // No pending, translated and confirmed data items. @@ -345,7 +345,7 @@ $this->assertEqual(5, $job->getCountReviewed()); // Add several job items + $job_item2 = tmgmt_job_item_create('plugin', 'type', 5, array('tjid' => $job->tjid)); - $job_item2 = tmgmt_job_item_create('entity', 'node', 2, array('tjid' => $job->tjid)); for ($index = 1; $index <= 4; $index++) { $job_item2->data['data_item' . $index] = $data1; } @@ -378,67 +378,4 @@ $this->assertEqual(31, $job->getCountAccepted()); } - /** - * Test suggested entities from a translation job. - */ - function testSuggestions() { - $job = $this->createJob(); - - // Create a content type with fields which have translatable fields. - $type = $this->drupalCreateContentType(); - - $field1 = field_create_field(array( - 'field_name' => 'field1', - 'type' => 'file', - 'entity_types' => array('node'), - )); - $field2 = field_create_field(array( - 'field_name' => 'field2', - 'type' => 'file', - 'entity_types' => array('node'), - )); - - field_create_instance(array( - 'field_name' => $field1['field_name'], - 'entity_type' => 'node', - 'bundle' => $type->type, - 'label' => 'Field 1', - 'widget' => array('type' => 'file'), - 'settings' => array(), - )); - field_create_instance(array( - 'field_name' => $field2['field_name'], - 'entity_type' => 'node', - 'bundle' => $type->type, - 'label' => 'Field 2', - 'widget' => array('type' => 'file'), - 'settings' => array(), - )); - $this->drupalCreateNode(array( - 'type' => $type->type, - 'field1' => array( - 'alt_text' => array( - LANGUAGE_NONE => array('value' => 'Alt text'), - ), - 'title_text' => array( - LANGUAGE_NONE => array('value' => 'Title text'), - ), - ), - 'field2' => array( - 'alt_text' => array( - LANGUAGE_NONE => array('value' => 'Alt text'), - ), - 'title_text' => array( - LANGUAGE_NONE => array('value' => 'Title text'), - ), - ), - )); - - // Add all three nodes to the job. - $item1 = $job->addItem('entity', 'node', 1); - $item2 = $job->addItem('entity', 'node', 2); - $item3 = $job->addItem('entity', 'file', 3); - - } - } diff -u b/tmgmt.api.php b/tmgmt.api.php --- b/tmgmt.api.php +++ b/tmgmt.api.php @@ -45,15 +45,18 @@ * * @return array * An array with all additional translation suggestions. - * - tmgmt_job: A TMGMTJobItem instance. - * - referenced: A string which indicates where this suggestion comes from. - * This string may include a link to let the user view the page. + * - job_item: A TMGMTJobItem instance. + * - suggested: A string which indicates where this suggestion comes from. + * - reference_link: Link to the entity this suggestions comes from. + * - from_job: The main TMGMTJob-ID which suggests this translation. */ -function tmgmt_entity_tmgmt_source_suggestions(TMGMTJob $job) { +function hook_tmgmt_source_suggestions(TMGMTJob $job) { return array( array( - 'tmgmt_job' => tmgmt_job_item_create('entity', 'node', 0), - 'referenced' => t('Referenced from'), + 'job_item' => tmgmt_job_item_create('entity', 'node', 0), + 'suggested' => t('Suggested @type entity', array('@type' => 'node')), + 'entity_uri' => entity_uri('node', 0), + 'from_job' => $job->tjid, ) ); } reverted: --- b/translators/file/tmgmt_file.test +++ a/translators/file/tmgmt_file.test @@ -43,8 +43,8 @@ $job = $this->createJob(); $job->translator = $translator->name; + $job->addItem('test_source', 'test', '1'); + $job->addItem('test_source', 'test', '2'); - $job->addItem('test_source', 'node', 1); - $job->addItem('test_source', 'node', 2); $job->requestTranslation(); $messages = $job->getMessages(); @@ -82,8 +82,8 @@ $job = $this->createJob(); $job->translator = $translator->name; + $job->addItem('test_source', 'test', '1'); + $job->addItem('test_source', 'test', '2'); - $job->addItem('test_source', 'node', 1); - $job->addItem('test_source', 'node', 2); $job->requestTranslation(); $messages = $job->getMessages(); @@ -180,8 +180,8 @@ $job = $this->createJob(); $job->translator = $translator->name; + $job->addItem('test_source', 'test', '1'); + $job->addItem('test_source', 'test', '2'); - $job->addItem('test_source', 'node', 1); - $job->addItem('test_source', 'node', 2); $job->requestTranslation(); $messages = $job->getMessages(); reverted: --- b/translators/tmgmt_local/tmgmt_local.test +++ a/translators/tmgmt_local/tmgmt_local.test @@ -163,8 +163,8 @@ $job = $this->createJob(); $job->translator = $translator->name; $job->settings['job_comment'] = $job_comment = 'Dummy job comment'; + $job->addItem('test_source', 'test', '1'); + $job->addItem('test_source', 'test', '2'); - $job->addItem('test_source', 'node', 1); - $job->addItem('test_source', 'node', 2); // Create another local translator with the required capabilities. $other_translator_same = $this->drupalCreateUser($this->local_translator_permissions); @@ -260,8 +260,8 @@ $this->assertEqual($first_task_item->getCountTranslated(), 0); $this->assertEqual($first_task_item->getCountUntranslated(), 1); + $this->assertText(t('Translation for @source', array('@source' => 'test_source:test:1'))); + $this->assertText(t('Translation for @source', array('@source' => 'test_source:test:2'))); - $this->assertText(t('Translation for @source', array('@source' => 'test_source:node:1'))); - $this->assertText(t('Translation for @source', array('@source' => 'test_source:node:2'))); $this->assertText(t('Untranslated')); // Translate the first item. @@ -270,7 +270,7 @@ $this->assertText(t('Dummy')); // Job comment is present in the translate tool as well. $this->assertText($job_comment); + $this->assertText(t('Translation for @source', array('@source' => 'test_source:test:1'))); - $this->assertText(t('Translation for @source', array('@source' => 'test_source:node:1'))); // Try to complete a translation when translations are missing. $this->drupalPost(NULL, array(), t('Save as completed')); @@ -425,8 +425,8 @@ $this->loginAsTranslator(); $job = $this->createJob(); $job->translator = $translator->name; + $job->addItem('test_source', 'test', '1'); + $job->addItem('test_source', 'test', '2'); - $job->addItem('test_source', 'node', 1); - $job->addItem('test_source', 'node', 2); $this->assertFalse($job->requestTranslation(), 'Translation request was denied.'); diff -u b/ui/includes/tmgmt_ui.pages.inc b/ui/includes/tmgmt_ui.pages.inc --- b/ui/includes/tmgmt_ui.pages.inc +++ b/ui/includes/tmgmt_ui.pages.inc @@ -439,7 +439,7 @@ '#type' => 'submit', '#value' => t('Save suggestions'), '#submit' => array('tmgmt_ui_ajax_submit_save_suggestions'), - '#limit_validation_errors' => array(), + '#limit_validation_errors' => array(array('suggestions_table')), '#attributes' => array( 'class' => array('tmgmt-ui-job-suggestions-save') ), @@ -575,14 +575,15 @@ */ function tmgmt_ui_ajax_submit_save_suggestions($form, &$form_state) { // Save all selected suggestion jobs. - if (isset($form_state['input']['suggestions_table']) && is_array($form_state['input']['suggestions_table'])) { - foreach ($form_state['input']['suggestions_table'] as $id) { - if (isset($form_state['tmgmt_suggestions'][$id]['tmgmt_job']) + if (isset($form_state['values']['suggestions_table']) && is_array($form_state['values']['suggestions_table'])) { + foreach ($form_state['values']['suggestions_table'] as $id) { + $id = check_plain($id); + if (isset($form_state['tmgmt_suggestions'][$id]['job_item']) && isset($form_state['tmgmt_job']) - && ($form_state['tmgmt_suggestions'][$id]['tmgmt_job'] instanceof TMGMTJobItem) + && ($form_state['tmgmt_suggestions'][$id]['job_item'] instanceof TMGMTJobItem) && ($form_state['tmgmt_job'] instanceof TMGMTJob)) { - $item = &$form_state['tmgmt_suggestions'][$id]['tmgmt_job']; - $item = $form_state['tmgmt_job']->addItem($item->plugin, $item->item_type, $item->item_id); + $item = $form_state['tmgmt_suggestions'][$id]['job_item']; + $form_state['tmgmt_job']->addJobItem($item); // @todo: Maybe add the new item to the main 'job items table' and // refresh that too. Or at least print a message to show success. } @@ -617,7 +618,7 @@ $job = $form_state['tmgmt_job']; if ($job instanceof TMGMTJob) { // Cache all suggestions. - if (TRUE || !isset($form_state['tmgmt_suggestions']) || (count($form_state['tmgmt_suggestions']) <= 0)) { + if (!isset($form_state['tmgmt_suggestions']) || (count($form_state['tmgmt_suggestions']) <= 0)) { $form_state['tmgmt_suggestions'] = module_invoke_all('tmgmt_source_suggestions', $job); // @todo: Recursive call to all jobs to get sub-suggestions as well. // A break-rule has to be defined, based on a level or whatever. @@ -628,10 +629,11 @@ // Process all valid entries. foreach ($form_state['tmgmt_suggestions'] as $k => &$result) { - if (is_array($result) && isset($result['tmgmt_job']) && ($result['tmgmt_job'] instanceof TMGMTJobItem)) { - $entity = $result['tmgmt_job']; - $referenced = isset($result['referenced']) ? $result['referenced'] : NULL; - $options[$k] = _tmgmt_ui_add_suggestion_job($entity, $entity->label(), $referenced); + if (is_array($result) && isset($result['job_item']) && ($result['job_item'] instanceof TMGMTJobItem)) { + $entity = $result['job_item']; + $suggested = isset($result['suggested']) ? $result['suggested'] : NULL; + $uri = isset($result['entity_uri']) ? $result['entity_uri'] : NULL; + $options[$k] = _tmgmt_ui_add_suggestion_job($entity, $entity->label(), $suggested, $uri); } } @@ -652,20 +654,22 @@ * A translation jobitem created by a module which is a suggestion. * @param string $title = NULL * (Optional) title for the suggestion row. - * @param string $referenced = NULL - * (Optional) Text and/or link to the entity which suggests this job. + * @param string $suggested = NULL + * (Optional) Text which indicates what type of translation this is. + * @param array $uri = NULL + * (Optional) entity_uri array to link to the referenced entity. * * @return array * Options-Entry for a tableselect array. */ -function _tmgmt_ui_add_suggestion_job(TMGMTJobItem &$entity, $title = NULL, $referenced = NULL) { +function _tmgmt_ui_add_suggestion_job(TMGMTJobItem &$entity, $title = NULL, $suggested = '', $uri = NULL) { return array( 'title' => isset($title) ? $title : t('From job #@id', array('@id' => $entity->identifier())), 'words' => $entity->getWordCount(), 'referenced' => array( 'data' => array( '#type' => 'markup', - '#markup' => isset($referenced) ? $referenced : '', + '#markup' => isset($uri) ? l($suggested, $uri['path'], array('attributes' => array('target' => '_blank')) + $uri['options']) : $suggested, ) ), ); only in patch2: unchanged: --- a/tmgmt.info +++ b/tmgmt.info @@ -35,6 +35,7 @@ files[] = tests/tmgmt.crud.test files[] = tests/tmgmt.plugin.test files[] = tests/tmgmt.helper.test files[] = tests/tmgmt.upgrade.alpha1.test +files[] = tests/tmgmt.suggestions.test ; Views integration and handlers files[] = views/tmgmt.views.inc