5 * Provide views data for comment.module.
8 use Drupal\Core\Entity\ContentEntityInterface;
11 * Implements hook_views_data_alter().
13 function comment_views_data_alter(&$data) {
14 // New comments are only supported for node table because it requires the
16 $data['node']['new_comments'] = [
17 'title' => t('New comments'),
18 'help' => t('The number of new comments on the node.'),
20 'id' => 'node_new_comments',
21 'no group by' => TRUE,
25 // Provide a integration for each entity type except comment.
26 foreach (\Drupal::entityManager()->getDefinitions() as $entity_type_id => $entity_type) {
27 if ($entity_type_id == 'comment' || !$entity_type->entityClassImplements(ContentEntityInterface::class) || !$entity_type->getBaseTable()) {
30 $fields = \Drupal::service('comment.manager')->getFields($entity_type_id);
31 $base_table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();
32 $args = ['@entity_type' => $entity_type_id];
35 $data[$base_table]['comments_link'] = [
37 'title' => t('Add comment link'),
38 'help' => t('Display the standard add comment link used on regular @entity_type, which will only display if the viewing user has access to add a comment.', $args),
39 'id' => 'comment_entity_link',
43 // Multilingual properties are stored in data table.
44 if (!($table = $entity_type->getDataTable())) {
45 $table = $entity_type->getBaseTable();
47 $data[$table]['uid_touch'] = [
48 'title' => t('User posted or commented'),
49 'help' => t('Display nodes only if a user posted the @entity_type or commented on the @entity_type.', $args),
52 'name table' => 'users_field_data',
53 'name field' => 'name',
54 'id' => 'argument_comment_user_uid',
55 'no group by' => TRUE,
56 'entity_type' => $entity_type_id,
57 'entity_id' => $entity_type->getKey('id'),
61 'name table' => 'users_field_data',
62 'name field' => 'name',
63 'id' => 'comment_user_uid',
64 'entity_type' => $entity_type_id,
65 'entity_id' => $entity_type->getKey('id'),
69 foreach ($fields as $field_name => $field) {
70 $data[$base_table][$field_name . '_cid'] = [
71 'title' => t('Comments of the @entity_type using field: @field_name', $args + ['@field_name' => $field_name]),
72 'help' => t('Relate all comments on the @entity_type. This will create 1 duplicate record for every comment. Usually if you need this it is better to create a comment view.', $args),
74 'group' => t('Comment'),
75 'label' => t('Comments'),
76 'base' => 'comment_field_data',
77 'base field' => 'entity_id',
78 'relationship field' => $entity_type->getKey('id'),
82 'field' => 'entity_type',
83 'value' => $entity_type_id,
86 'field' => 'field_name',
87 'value' => $field_name,