comment_test.module

Same filename and directory in other branches
  1. 9 core/modules/comment/tests/modules/comment_test/comment_test.module
  2. 8.9.x core/modules/comment/tests/modules/comment_test/comment_test.module
  3. 11.x core/modules/comment/tests/modules/comment_test/comment_test.module

Implements comment-related hooks to test API interactions.

File

core/modules/comment/tests/modules/comment_test/comment_test.module

View source
<?php


/**
 * @file
 * Implements comment-related hooks to test API interactions.
 */

use Drupal\comment\CommentInterface;
use Drupal\Core\Url;

/**
 * Implements hook_entity_type_alter().
 */
function comment_test_entity_type_alter(array &$entity_types) {
  /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
  if (\Drupal::languageManager()->isMultilingual()) {
    // Enable language handling for comment fields.
    $translation = $entity_types['comment']->get('translation');
    $translation['comment_test'] = TRUE;
    $entity_types['comment']->set('translation', $translation);
  }
}

/**
 * Implements hook_comment_links_alter().
 */
function comment_test_comment_links_alter(array &$links, CommentInterface &$entity, array &$context) {
  // Allow tests to enable or disable this alter hook.
  if (!\Drupal::state()->get('comment_test_links_alter_enabled', FALSE)) {
    return;
  }
  $links['comment_test'] = [
    '#theme' => 'links__comment__comment_test',
    '#attributes' => [
      'class' => [
        'links',
        'inline',
      ],
    ],
    '#links' => [
      'comment-report' => [
        'title' => t('Report'),
        'url' => Url::fromRoute('comment_test.report', [
          'comment' => $entity->id(),
        ], [
          'query' => [
            'token' => \Drupal::getContainer()->get('csrf_token')
              ->get("comment/{$entity->id()}/report"),
          ],
        ]),
      ],
    ],
  ];
}

Functions

Title Deprecated Summary
comment_test_comment_links_alter Implements hook_comment_links_alter().
comment_test_entity_type_alter Implements hook_entity_type_alter().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.