diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index 40bfbf31bd0cb448f256e0cf48ec797ff4fb6166..11fca800ab0d2c2b0941a9c12a2e1054cd883f30 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -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() == '') { diff --git a/core/modules/comment/src/Form/CommentAdminOverview.php b/core/modules/comment/src/Form/CommentAdminOverview.php index 16c86c1f22a5fd0ced6a890c6c956de2055e2bda..fedc921f8bd50758cf736a054ca764847517bc20 100644 --- a/core/modules/comment/src/Form/CommentAdminOverview.php +++ b/core/modules/comment/src/Form/CommentAdminOverview.php @@ -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( diff --git a/core/modules/comment/src/Tests/CommentNonNodeTest.php b/core/modules/comment/src/Tests/CommentNonNodeTest.php index a7f7918de27f57a8f3193c0eb72f2db928916928..ee08aa2c97739fcc4d41f2877a5dd961c7bfb5bc 100644 --- a/core/modules/comment/src/Tests/CommentNonNodeTest.php +++ b/core/modules/comment/src/Tests/CommentNonNodeTest.php @@ -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')); } /** diff --git a/core/modules/rdf/rdf.module b/core/modules/rdf/rdf.module index d6728202f7fad55afb7a3e0f32ad25cc99b2c87a..1beb1a1053f3366837ac909f86e450ee1ebe02d0 100644 --- a/core/modules/rdf/rdf.module +++ b/core/modules/rdf/rdf.module @@ -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'],