Commit 574bccb3 authored by catch's avatar catch
Browse files

fix: #3566261 [regression] custom field twig for second comment fields has...

fix: #3566261 [regression] custom field twig for second comment fields has null value in comment after 11.3.0 upgrade

By: fl-49
By: ghost of drupal past
By: nicxvan
By: will_frank
By: godotislate
(cherry picked from commit c834c186)
parent fb42722d
Loading
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -213,8 +213,11 @@ public function preprocessBlock(&$variables): void {
   *   An associative array containing:
   *   - element: An associative array containing render arrays for the list of
   *     comments, and the comment form. Array keys: comments, comment_form.
   *
   * @todo Rename to preprocess_field__comment() once
   *   https://www.drupal.org/node/3566850 is resolved.
   */
  #[Hook('preprocess_field__comment')]
  #[Hook('preprocess_field')]
  public function preprocessField(&$variables): void {
    $element = $variables['element'];
    // We need to check for the field type even though we are using the comment
+77 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\comment\Kernel;

use Drupal\Core\Render\Markup;
use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use PHPUnit\Framework\Attributes\TestWith;

/**
 * Test comment field preprocess hook.
 */
#[Group('comment')]
#[Group('field')]
#[RunTestsInSeparateProcesses]
class CommentFieldPreprocessTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['comment', 'field', 'system'];

  /**
   * Tests that CommentThemeHooks::preprocessField() injects variables.
   *
   * If the Hook attribute CommentThemeHooks::preprocessField() does not target
   * the base hook "preprocess_field", and instead targets
   * "preprocess_field__comment", then preprocessField() is not invoked when
   * the active theme suggestion for a comment field is something other than
   * "field__comment". For example, this can happen if the theme has a template
   * using the field name, and the active theme suggestion becomes
   * field__{field_name}.
   */
  #[TestWith(['comment'])]
  #[TestWith(['comment_other'])]
  public function testCommentFieldPreprocess(string $fieldName): void {
    // Set active theme to test_theme, so its field--other-comment.html.twig
    // template gets picked up.
    \Drupal::service('theme_installer')->install(['test_theme']);
    /** @var \Drupal\Core\Theme\ThemeInitializationInterface $theme_initializer */
    $theme_initializer = \Drupal::service('theme.initialization');
    /** @var \Drupal\Core\Theme\ThemeManagerInterface $theme_manager */
    $theme_manager = \Drupal::service('theme.manager');
    $theme_manager->setActiveTheme($theme_initializer->getActiveThemeByName('test_theme'));

    // Create a mock field render array for a comment field.
    $build = [
      '#theme' => "field__{$fieldName}",
      '#entity_type' => 'test',
      '#bundle' => 'test',
      '#field_name' => $fieldName,
      '#label_display' => 'hidden',
      '#field_type' => 'comment',
      '#title' => 'test',
      '#is_multiple' => FALSE,
      0 => [
        '#comment_type' => 'test',
        '#comment_display_mode' => 'default',
        'comments' => [
          '#markup' => '<p>These are the comments.</p>',
        ],
        'comment_form' => [
          '#markup' => Markup::create('<form>This is the comment form.</form>'),
        ],
      ],
    ];
    $rendered = (string) \Drupal::service('renderer')->renderInIsolation($build);
    // The "comments" and "comment_form" variables are injected by
    // CommentThemeHooks::preprocessField() and should be correctly rendered by
    // test_theme template using those variables.
    $this->assertSame("<p>These are the comments.</p><form>This is the comment form.</form>\n", $rendered);
  }

}
+1 −0
Original line number Diff line number Diff line
{% include 'field--comment.html.twig' %}
+8 −0
Original line number Diff line number Diff line
{#
/**
 * @file
 * Minimal template to ensure comments and comment_form variables are propagated
 * Drupal\comment\Hook\CommentThemeHooks::preprocessField().
 */
#}
{{ comments }}{{ comment_form }}