Verified Commit 691c493a authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3322894 by Akhil Babu, Juanjol, micnap, larowlan, quietone, catch,...

Issue #3322894 by Akhil Babu, Juanjol, micnap, larowlan, quietone, catch, alexpott:  Comment form gives deprecated warnings on PHP 8.1 when comment is empty

(cherry picked from commit 1ceffc16)
parent 0f1cc5ae
Loading
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -313,7 +313,7 @@ public function buildEntity(array $form, FormStateInterface $form_state) {
    // Validate the comment's subject. If not specified, extract from comment
    // body.
    if (trim($comment->getSubject()) == '') {
      if ($comment->hasField('comment_body')) {
      if ($comment->hasField('comment_body') && !$comment->comment_body->isEmpty()) {
        // The body may be in any format, so:
        // 1) Filter it into HTML
        // 2) Strip out all HTML tags
@@ -323,7 +323,7 @@ public function buildEntity(array $form, FormStateInterface $form_state) {
      }
      // Edge cases where the comment body is populated only by HTML tags will
      // require a default subject.
      if ($comment->getSubject() == '') {
      if (trim($comment->getSubject()) == '') {
        $comment->setSubject($this->t('(No subject)'));
      }
    }
+11 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\comment\Entity\Comment;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\field\Entity\FieldConfig;
use Drupal\user\RoleInterface;
use Drupal\filter\Entity\FilterFormat;

@@ -238,6 +239,16 @@ public function testAutoFilledSubject() {
    $body_text2 = 'AQuickBrownFoxJumpedOverTheLazyDog';
    $comment2 = $this->postComment(NULL, $body_text2, '', TRUE);
    $this->assertEquals('AQuickBrownFoxJumpedOverTheL…', $comment2->getSubject());

    // Make the body field non required.
    $comment_body_field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
    $comment_body_field->setRequired(FALSE)->save();
    // Try to post a comment without any value in body and subject fields.
    $this->drupalGet('node/' . $this->node->id());
    // Ensure that there are no PHP errors or warnings when automatically
    // generating the subject. This occurs when the comment body is empty.
    $comment2 = $this->postComment(NULL, '', '', TRUE);
    $this->assertEquals('(No subject)', $comment2->getSubject());
  }

  /**