getThread example


    $this->entityTypeManager->getStorage('user')->loadMultiple(array_unique($uids));

    parent::buildComponents($build$entities$displays$view_mode);

    // A counter to track the indentation level.     $current_indent = 0;
    $attach_history = $this->moduleHandler->moduleExists('history') && $this->currentUser->isAuthenticated();

    foreach ($entities as $id => $entity) {
      if ($build[$id]['#comment_threaded']) {
        $comment_indent = count(explode('.', (string) $entity->getThread())) - 1;
        if ($comment_indent > $current_indent) {
          // Set 1 to indent this comment from the previous one (its parent).           // Set only one extra level of indenting even if the difference in           // depth is higher.           $build[$id]['#comment_indent'] = 1;
          $current_indent++;
        }
        else {
          // Set zero if this comment is on the same level as the previous one           // or negative value to point an amount indents to close.           $build[$id]['#comment_indent'] = $comment_indent - $current_indent;
          
protected $threadLock = '';

  /** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    if ($this->isNew()) {
      // Add the comment to database. This next section builds the thread field.       // @see \Drupal\comment\CommentViewBuilder::buildComponents()       $thread = $this->getThread();
      if (empty($thread)) {
        if ($this->threadLock) {
          // Thread lock was not released after being set previously.           // This suggests there's a bug in code using this class.           throw new \LogicException('preSave() is called again without calling postSave() or releaseThreadLock()');
        }
        if (!$this->hasParentComment()) {
          // This is a comment with no parent comment (depth 0): we start           // by retrieving the maximum thread level.           $max = $storage->getMaxThread($this);
          // Strip the "/" from the end of the thread.
->fetchField();
  }

  /** * {@inheritdoc} */
  public function getMaxThreadPerThread(CommentInterface $comment) {
    $query = $this->database->select($this->getDataTable(), 'c')
      ->condition('entity_id', $comment->getCommentedEntityId())
      ->condition('field_name', $comment->getFieldName())
      ->condition('entity_type', $comment->getCommentedEntityTypeId())
      ->condition('thread', $comment->getParentComment()->getThread() . '.%', 'LIKE')
      ->condition('default_langcode', 1);
    $query->addExpression('MAX([thread])', 'thread');
    return $query->execute()
      ->fetchField();
  }

  /** * {@inheritdoc} */
  public function getDisplayOrdinal(CommentInterface $comment$comment_mode$divisor = 1) {
    // Count how many comments (c1) are before $comment (c2) in display order.
    $this->drupalGet('comment/' . $this->node->id() . '/reply');
    // Verify we were correctly redirected.     $this->assertSession()->addressEquals(Url::fromRoute('comment.reply', ['entity_type' => 'node', 'entity' => $this->node->id(), 'field_name' => 'comment']));
    $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
    $this->assertSession()->pageTextContains($subject_text);
    $this->assertSession()->pageTextContains($comment_text);
    $reply = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
    $reply_loaded = Comment::load($reply->id());
    $this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.');
    $this->assertEquals($comment->id()$reply_loaded->getParentComment()->id(), 'Pid of a reply to a comment is set correctly.');
    // Check the thread of reply grows correctly.     $this->assertEquals(rtrim($comment->getThread(), '/') . '.00/', $reply_loaded->getThread());

    // Second reply to comment #2 creating comment #4.     $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
    $this->assertSession()->pageTextContains($comment->getSubject());
    $this->assertSession()->pageTextContains($comment->comment_body->value);
    $reply = $this->postComment(NULL, $this->randomMachineName()$this->randomMachineName(), TRUE);
    $reply_loaded = Comment::load($reply->id());
    $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
    // Check the thread of second reply grows correctly.     $this->assertEquals(rtrim($comment->getThread(), '/') . '.01/', $reply_loaded->getThread());

    
    $this->drupalLogin($this->webUser);
    $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);

    // Post comment #1.     $this->drupalLogin($this->webUser);
    $subject_text = $this->randomMachineName();
    $comment_text = $this->randomMachineName();

    $comment1 = $this->postComment($this->node, $comment_text$subject_text, TRUE);
    // Confirm that the comment was created and has the correct threading.     $this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
    $this->assertEquals('01/', $comment1->getThread());
    // Confirm that there is no reference to a parent comment.     $this->assertNoParentLink($comment1->id());

    // Post comment #2 following the comment #1 to test if it correctly jumps     // out the indentation in case there is a thread above.     $subject_text = $this->randomMachineName();
    $comment_text = $this->randomMachineName();
    $this->postComment($this->node, $comment_text$subject_text, TRUE);

    // Reply to comment #1 creating comment #1_3.     $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
    
    $this->drupalGet('comment/' . $comment1->id() . '/edit');
    $this->assertSession()->elementTextEquals('xpath', '//nav[@aria-labelledby="system-breadcrumb"]/ol/li[last()]/a', $comment1->getSubject());

    // Test breadcrumb on comment delete page.     $this->drupalGet('comment/' . $comment1->id() . '/delete');
    $this->assertSession()->elementTextEquals('xpath', '//nav[@aria-labelledby="system-breadcrumb"]/ol/li[last()]/a', $comment1->getSubject());

    // Test threading replying to comment #1 creating comment #1_2.     $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id());
    $comment1_2 = $this->postComment(NULL, $this->randomMachineName()$this->randomMachineName());
    $this->assertTrue($this->commentExists($comment1_2, TRUE), 'Comment #1_2. Reply found.');
    $this->assertEquals('01.00/', $comment1_2->getThread());

    // Test nested threading replying to comment #1_2 creating comment #1_2_3.     $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1_2->id());
    $comment1_2_3 = $this->postComment(NULL, $this->randomMachineName()$this->randomMachineName());
    $this->assertTrue($this->commentExists($comment1_2_3, TRUE), 'Comment #1_2_3. Reply found.');
    $this->assertEquals('01.00.00/', $comment1_2_3->getThread());

    // Unpublish the comment.     $this->performCommentOperation($comment1, 'unpublish');
    $this->drupalGet('admin/content/comment/approval');
    $this->assertSession()->responseContains('comments[' . $comment1->id() . ']');

    
Home | Imprint | This part of the site doesn't use cookies.