When we fill nothing in the subject field of comment, drupal will use the comment body as the value of subject. See the code in CommentForm::buildEntity

// Validate the comment's subject. If not specified, extract from comment
// body.
if (trim($comment->getSubject()) == '') {
  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() == '') {
    $comment->setSubject($this->t('(No subject)'));

I still need to show Subject if it's been filled, while leave the Subject as blank when the user fill nothing in. To programmatically change the default behavior, we may need to override the function after buildEntity take place, otherwise buildEntity will override it again to the comment body. Is it possible to do that?

2 Answers 2


You can use hook_ENTITY_TYPE_presave to do this, just call

 if (!$entity->getSubject()) {

if it isn’t filled.

Hope it makes sense.

  • This method set the subject field to be blank no matter it is filled or not. I still need Subject to be shown when it is filled with something.
    – xibinke
    Jan 21, 2019 at 2:28
  • I’m sorry if it was unclear try to look at update where I use getSubject to check if it has value
    – user72672
    Jan 21, 2019 at 7:15
  • Thanks a lot, it works. I'm still curious about the calling precedure. If the hook_ENTITY_TYPE_presave calls first, buildEnity will then set the value from comment_body again; if not, hook_ENTITY_TYPE_presave will always set subject to be blank. It seems conflict ... Where did I misunderstand?
    – xibinke
    Jan 21, 2019 at 9:31
  • hook_ENTITY_TYPE_presave is called just before the entity is saved. And it will only set the subject to be blank if has no value, instead of taking the value from body.
    – user72672
    Jan 21, 2019 at 9:34
  • Let me guess there's a callback from the save procedure, both hook_ENTITY_TYPE_presave and buildEntity will be called at the same time and the hook_ENTITY_TYPE_presave has a higher priority to set the Subject?
    – xibinke
    Jan 21, 2019 at 9:41

After codes pile up, the issue comes back again. Maybe some other function definition causes the above code of @Jdrupal lose its effect. I now use a reverse process of buildEntity in hook_ENTITY_TYPE_presave to achieve my purpose. Though not neat, it works:

$subject = strip_tags($entity->get('comment_body')->value);
if(strpos($entity->getSubject(),$subject) !== false)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.