Commit 4eff3d1c authored by alexpott's avatar alexpott

Issue #2422353 by andypost: Comment module should check that comment body field exists

parent b84088e6
......@@ -297,12 +297,14 @@ public function buildEntity(array $form, FormStateInterface $form_state) {
// Validate the comment's subject. If not specified, extract from comment
// body.
if (trim($comment->getSubject()) == '') {
// The body may be in any format, so:
// 1) Filter it into HTML
// 2) Strip out all HTML tags
// 3) Convert entities back to plain-text.
$comment_text = $comment->comment_body->processed;
$comment->setSubject(Unicode::truncate(trim(Html::decodeEntities(strip_tags($comment_text))), 29, TRUE, TRUE));
if ($comment->hasField('comment_body')) {
// The body may be in any format, so:
// 1) Filter it into HTML
// 2) Strip out all HTML tags
// 3) Convert entities back to plain-text.
$comment_text = $comment->comment_body->processed;
$comment->setSubject(Unicode::truncate(trim(Html::decodeEntities(strip_tags($comment_text))), 29, TRUE, TRUE));
}
// Edge cases where the comment body is populated only by HTML tags will
// require a default subject.
if ($comment->getSubject() == '') {
......
......@@ -183,14 +183,12 @@ public function buildForm(array $form, FormStateInterface $form_state, $type = '
foreach ($comments as $comment) {
/** @var $commented_entity \Drupal\Core\Entity\EntityInterface */
$commented_entity = $commented_entities[$comment->getCommentedEntityTypeId()][$comment->getCommentedEntityId()];
$body = '';
if (!empty($comment->comment_body->value)) {
$body = $comment->comment_body->value;
}
$comment_permalink = $comment->permalink();
$attributes = $comment_permalink->getOption('attributes') ?: array();
$attributes += array('title' => Unicode::truncate($body, 128));
$comment_permalink->setOption('attributes', $attributes);
if ($comment->hasField('comment_body') && ($body = $comment->get('comment_body')->value)) {
$attributes = $comment_permalink->getOption('attributes') ?: array();
$attributes += array('title' => Unicode::truncate($body, 128));
$comment_permalink->setOption('attributes', $attributes);
}
$options[$comment->id()] = array(
'title' => array('data' => array('#title' => $comment->getSubject() ?: $comment->id())),
'subject' => array(
......
......@@ -100,13 +100,16 @@ protected function setUp() {
*
* @param \Drupal\Core\Entity\EntityInterface|null $entity
* Entity to post comment on or NULL to post to the previously loaded page.
* @param $comment
* @param string $comment
* Comment body.
* @param $subject
* @param string $subject
* Comment subject.
* @param $contact
* @param mixed $contact
* Set to NULL for no contact info, TRUE to ignore success checking, and
* array of values to set contact info.
*
* @return \Drupal\comment\CommentInterface
* The new comment entity.
*/
function postComment(EntityInterface $entity, $comment, $subject = '', $contact = NULL) {
$edit = array();
......@@ -436,9 +439,28 @@ function testCommentFunctionality() {
$this->assertFieldChecked('edit-field-foobar-0-status-2');
$this->assertNoField('edit-field-foobar-0-status-0');
// @todo Check proper url and form https://www.drupal.org/node/2458323
$this->drupalGet('comment/reply/entity_test/comment/' . $new_entity->id());
$this->assertNoFieldByName('subject[0][value]', '', 'Subject field found.');
$this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field found.');
// Test removal of comment_body field.
$limited_user = $this->drupalCreateUser(array(
'administer entity_test fields',
'post comments',
'administer comment fields',
'administer comment types',
));
$this->drupalLogin($limited_user);
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
$this->assertFieldByName('comment_body[0][value]', '', 'Comment body field found.');
$this->fieldUIDeleteField('admin/structure/comment/manage/comment', 'comment.comment.comment_body', 'Comment', 'Comment settings');
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
$this->assertNoFieldByName('comment_body[0][value]', '', 'Comment body field not found.');
// Set subject field to autogenerate it.
$edit = ['subject[0][value]' => ''];
$this->drupalPostForm(NULL, $edit, t('Save'));
}
/**
......
......@@ -495,8 +495,8 @@ function rdf_preprocess_comment(&$variables) {
$variables['rdf_metadata_attributes'][] = $parent_comment_attributes;
}
}
// Adds RDF metadata markup above comment body.
if (!empty($variables['rdf_metadata_attributes'])) {
// Adds RDF metadata markup above comment body if any.
if (!empty($variables['rdf_metadata_attributes']) && isset($variables['content']['comment_body'])) {
$rdf_metadata = array(
'#theme' => 'rdf_metadata',
'#metadata' => $variables['rdf_metadata_attributes'],
......
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