Commit e8898ed3 authored by catch's avatar catch

Issue #1534674 by plach, slowflyer: Fixed Comment field language is completely broken.

parent 90b9f678
......@@ -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)),
......
......@@ -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);
......
......@@ -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 @@ public function form(array $form, array &$form_state, EntityInterface $comment)
}
$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 @@ public function submit(array $form, array &$form_state) {
// 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']);
}
......
......@@ -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 @@ public static function getInfo() {
return array(
'name' => 'Comment language',
'description' => 'Tests for comment language.',
'group' => 'Locale',
'group' => 'Comment',
);
}
......@@ -39,7 +39,7 @@ function setUp() {
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 @@ function setUp() {
// 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 @@ function testCommentLanguage() {
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);
}
}
}
}
name = Comment test
description = Support module for Comment module testing.
package = Testing
version = VERSION
core = 8.x
hidden = TRUE
dependencies[] = comment
<?php
/**
* @file
* Dummy module implementing comment related hooks to test API interaction with
* the Comment module.
*/
/**
* Implements hook_entity_info_alter().
*/
function comment_test_entity_info_alter(&$info) {
if (language_multilingual()) {
// Enable language handling for comment fields.
$info['comment']['translation']['comment_test'] = TRUE;
}
}
......@@ -301,7 +301,7 @@ function field_valid_language($langcode, $default = TRUE) {
*/
function field_language($entity_type, $entity, $field_name = NULL, $langcode = NULL) {
$display_langcodes = &drupal_static(__FUNCTION__, array());
$id = $entity->bundle();
$id = $entity->id();
$bundle = $entity->bundle();
$langcode = field_valid_language($langcode, FALSE);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment