You are here

function flat_comments_comment_presave in Flatcomments 8

Implementation of hook_comment_presave().

File

./flat_comments.module, line 70
Contains flat_comments.module.

Code

function flat_comments_comment_presave(CommentInterface $comment) {
  $entity = \Drupal::entityTypeManager()
    ->getStorage($comment
    ->getCommentedEntityTypeId())
    ->load($comment
    ->getCommentedEntityId());
  $field_definition = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions($entity
    ->getEntityTypeId(), $entity
    ->bundle());
  $field_definition = $field_definition[$comment
    ->getFieldName()];

  // Only affect new comments and comments set to be displayed flat.
  if (!$comment
    ->id() && $field_definition
    ->getSetting('default_mode') === 0) {

    // Set parent id to NULL to prevent threads.
    $comment
      ->get('pid')
      ->setValue(NULL);

    // Re-calculate the Thread.
    $thread = flat_comments_calculate_thread($comment);
    $comment
      ->setThread($thread);
  }
}