diff --git a/core/modules/comment/comment.admin.inc b/core/modules/comment/comment.admin.inc index 4a3220f..ef8a90b 100644 --- a/core/modules/comment/comment.admin.inc +++ b/core/modules/comment/comment.admin.inc @@ -110,13 +110,14 @@ function comment_admin_overview($form, &$form_state, $arg) { // Remove the first node title from the node_titles array and attach to // the comment. $comment->node_title = array_shift($node_titles); + $comment_body = field_get_items('comment', $comment, 'comment_body'); $options[$comment->cid] = array( 'subject' => array( 'data' => array( '#type' => 'link', '#title' => $comment->subject, '#href' => 'comment/' . $comment->cid, - '#options' => array('attributes' => array('title' => truncate_utf8($comment->comment_body[LANGUAGE_NOT_SPECIFIED][0]['value'], 128)), 'fragment' => 'comment-' . $comment->cid), + '#options' => array('attributes' => array('title' => truncate_utf8($comment_body[0]['value'], 128)), 'fragment' => 'comment-' . $comment->cid), ), ), 'author' => theme('username', array('account' => $comment)), diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 44b4c04..27ff7c3 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1690,7 +1690,8 @@ function comment_preview(Comment $comment) { $node = node_load($comment->nid); if (!form_get_errors()) { - $comment->format = $comment->comment_body[LANGUAGE_NOT_SPECIFIED][0]['format']; + $comment_body = field_get_items('comment', $comment, 'comment_body'); + $comment->format = $comment_body[0]['format']; // Attach the user and time information. if (!empty($comment->name)) { $account = user_load_by_name($comment->name); diff --git a/core/modules/comment/lib/Drupal/comment/CommentFormController.php b/core/modules/comment/lib/Drupal/comment/CommentFormController.php index 7dae92d..cead0cd 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentFormController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentFormController.php @@ -20,7 +20,6 @@ class CommentFormController extends EntityFormController { */ public function form(array $form, array &$form_state, EntityInterface $comment) { global $user; - $language_content = language(LANGUAGE_TYPE_CONTENT); $node = node_load($comment->nid); $form_state['comment']['node'] = $node; @@ -174,16 +173,16 @@ class CommentFormController extends EntityFormController { } $form['node_type'] = array('#type' => 'value', '#value' => 'comment_node_' . $node->type); - // Make the comment inherit the node language unless specifically set. - $comment_langcode = $comment->langcode; - if ($comment_langcode == LANGUAGE_NOT_SPECIFIED) { - $comment_langcode = $language_content->langcode; + // Make the comment inherit the current content language unless specifically + // set. + if ($comment->isNew()) { + $language_content = language(LANGUAGE_TYPE_CONTENT); + $comment->langcode = $language_content->langcode; } - // Uses the language of the content as comment language. $form['langcode'] = array( '#type' => 'value', - '#value' => $comment_langcode, + '#value' => $comment->langcode, ); // Attach fields. @@ -294,7 +293,9 @@ class CommentFormController extends EntityFormController { // 1) Filter it into HTML // 2) Strip out all HTML tags // 3) Convert entities back to plain-text. - $comment_body = $comment->comment_body[LANGUAGE_NOT_SPECIFIED][0]; + $field = field_info_field('comment_body'); + $langcode = field_is_translatable('comment', $field) ? $this->getFormLangcode($form_state) : LANGUAGE_NOT_SPECIFIED; + $comment_body = $comment->comment_body[$langcode][0]; if (isset($comment_body['format'])) { $comment_text = check_markup($comment_body['value'], $comment_body['format']); } diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleCommentLanguageTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php similarity index 67% rename from core/modules/locale/lib/Drupal/locale/Tests/LocaleCommentLanguageTest.php rename to core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php index 6776e31..155846f 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocaleCommentLanguageTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentLanguageTest.php @@ -2,17 +2,17 @@ /** * @file - * Definition of Drupal\locale\Tests\LocaleCommentLanguageTest. + * Definition of Drupal\locale\Tests\CommentLanguageTest. */ -namespace Drupal\locale\Tests; +namespace Drupal\comment\Tests; use Drupal\simpletest\WebTestBase; /** * Functional tests for comment language. */ -class LocaleCommentLanguageTest extends WebTestBase { +class CommentLanguageTest extends WebTestBase { /** * Modules to enable. @@ -23,7 +23,7 @@ class LocaleCommentLanguageTest extends WebTestBase { * * @var array */ - public static $modules = array('locale', 'language_test'); + public static $modules = array('language', 'language_test', 'comment_test'); protected $profile = 'standard'; @@ -31,7 +31,7 @@ class LocaleCommentLanguageTest extends WebTestBase { return array( 'name' => 'Comment language', 'description' => 'Tests for comment language.', - 'group' => 'Locale', + 'group' => 'Comment', ); } @@ -39,7 +39,7 @@ class LocaleCommentLanguageTest extends WebTestBase { parent::setUp(); // Create and login user. - $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages', 'administer content types', 'create article content')); + $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages', 'administer content types', 'administer comments', 'create article content')); $this->drupalLogin($admin_user); // Add language. @@ -68,6 +68,12 @@ class LocaleCommentLanguageTest extends WebTestBase { // French no matter what path prefix the URLs have. $edit = array('preferred_langcode' => 'fr'); $this->drupalPost("user/{$admin_user->uid}/edit", $edit, t('Save')); + + // Make comment body translatable. + $field = field_info_field('comment_body'); + $field['translatable'] = TRUE; + field_update_field($field); + $this->assertTrue(field_is_translatable('comment', $field), 'Comment body is translatable.'); } /** @@ -99,19 +105,36 @@ class LocaleCommentLanguageTest extends WebTestBase { foreach (language_list() as $langcode => $language) { // Post a comment with content language $langcode. $prefix = empty($prefixes[$langcode]) ? '' : $prefixes[$langcode] . '/'; - $edit = array("comment_body[$langcode_not_specified][0][value]" => $this->randomName()); - $this->drupalPost("{$prefix}node/{$node->nid}", $edit, t('Save')); + $comment_values[$node_langcode][$langcode] = $this->randomName(); + $edit = array( + 'subject' => $this->randomName(), + "comment_body[$langcode][0][value]" => $comment_values[$node_langcode][$langcode], + ); + $this->drupalPost("{$prefix}node/{$node->nid}", $edit, t('Preview')); + $this->drupalPost(NULL, $edit, t('Save')); // Check that comment language matches the current content language. - $comment = db_select('comment', 'c') - ->fields('c') + $cid = db_select('comment', 'c') + ->fields('c', array('cid')) ->condition('nid', $node->nid) ->orderBy('cid', 'DESC') + ->range(0, 1) ->execute() - ->fetchObject(); + ->fetchField(); + $comment = comment_load($cid); $args = array('%node_language' => $node_langcode, '%comment_language' => $comment->langcode, '%langcode' => $langcode); - $this->assertEqual($comment->langcode, $langcode, t('The comment posted with content language %langcode and belonging to the node with language %node_language has language %comment_language', $args)); + $this->assertEqual($comment->langcode, $langcode, format_string('The comment posted with content language %langcode and belonging to the node with language %node_language has language %comment_language', $args)); + $this->assertEqual($comment->comment_body[$langcode][0]['value'], $comment_values[$node_langcode][$langcode], 'Comment body correctly stored.'); + } + } + + // Check that comment bodies appear in the administration UI. + $this->drupalGet('admin/content/comment'); + foreach ($comment_values as $node_values) { + foreach ($node_values as $value) { + $this->assertRaw($value); } } } + } diff --git a/core/modules/comment/tests/modules/comment_test/comment_test.info b/core/modules/comment/tests/modules/comment_test/comment_test.info new file mode 100644 index 0000000..a7ad565 --- /dev/null +++ b/core/modules/comment/tests/modules/comment_test/comment_test.info @@ -0,0 +1,8 @@ +name = Comment test +description = Support module for Comment module testing. +package = Testing +version = VERSION +core = 8.x +hidden = TRUE + +dependencies[] = comment diff --git a/core/modules/comment/tests/modules/comment_test/comment_test.module b/core/modules/comment/tests/modules/comment_test/comment_test.module new file mode 100644 index 0000000..8e903db --- /dev/null +++ b/core/modules/comment/tests/modules/comment_test/comment_test.module @@ -0,0 +1,17 @@ +bundle(); + $id = $entity->id(); $bundle = $entity->bundle(); $langcode = field_valid_language($langcode, FALSE);