annotate core/modules/comment/comment.views.inc @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Provide views data for comment.module.
Chris@0 6 */
Chris@0 7
Chris@0 8 use Drupal\Core\Entity\ContentEntityInterface;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Implements hook_views_data_alter().
Chris@0 12 */
Chris@0 13 function comment_views_data_alter(&$data) {
Chris@0 14 // New comments are only supported for node table because it requires the
Chris@0 15 // history table.
Chris@0 16 $data['node']['new_comments'] = [
Chris@0 17 'title' => t('New comments'),
Chris@0 18 'help' => t('The number of new comments on the node.'),
Chris@0 19 'field' => [
Chris@0 20 'id' => 'node_new_comments',
Chris@0 21 'no group by' => TRUE,
Chris@0 22 ],
Chris@0 23 ];
Chris@0 24
Chris@0 25 // Provide a integration for each entity type except comment.
Chris@0 26 foreach (\Drupal::entityManager()->getDefinitions() as $entity_type_id => $entity_type) {
Chris@0 27 if ($entity_type_id == 'comment' || !$entity_type->entityClassImplements(ContentEntityInterface::class) || !$entity_type->getBaseTable()) {
Chris@0 28 continue;
Chris@0 29 }
Chris@0 30 $fields = \Drupal::service('comment.manager')->getFields($entity_type_id);
Chris@0 31 $base_table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();
Chris@0 32 $args = ['@entity_type' => $entity_type_id];
Chris@0 33
Chris@0 34 if ($fields) {
Chris@0 35 $data[$base_table]['comments_link'] = [
Chris@0 36 'field' => [
Chris@0 37 'title' => t('Add comment link'),
Chris@0 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),
Chris@0 39 'id' => 'comment_entity_link',
Chris@0 40 ],
Chris@0 41 ];
Chris@0 42
Chris@0 43 // Multilingual properties are stored in data table.
Chris@0 44 if (!($table = $entity_type->getDataTable())) {
Chris@0 45 $table = $entity_type->getBaseTable();
Chris@0 46 }
Chris@0 47 $data[$table]['uid_touch'] = [
Chris@0 48 'title' => t('User posted or commented'),
Chris@0 49 'help' => t('Display nodes only if a user posted the @entity_type or commented on the @entity_type.', $args),
Chris@0 50 'argument' => [
Chris@0 51 'field' => 'uid',
Chris@0 52 'name table' => 'users_field_data',
Chris@0 53 'name field' => 'name',
Chris@0 54 'id' => 'argument_comment_user_uid',
Chris@0 55 'no group by' => TRUE,
Chris@0 56 'entity_type' => $entity_type_id,
Chris@0 57 'entity_id' => $entity_type->getKey('id'),
Chris@0 58 ],
Chris@0 59 'filter' => [
Chris@0 60 'field' => 'uid',
Chris@0 61 'name table' => 'users_field_data',
Chris@0 62 'name field' => 'name',
Chris@0 63 'id' => 'comment_user_uid',
Chris@0 64 'entity_type' => $entity_type_id,
Chris@0 65 'entity_id' => $entity_type->getKey('id'),
Chris@0 66 ],
Chris@0 67 ];
Chris@0 68
Chris@0 69 foreach ($fields as $field_name => $field) {
Chris@0 70 $data[$base_table][$field_name . '_cid'] = [
Chris@0 71 'title' => t('Comments of the @entity_type using field: @field_name', $args + ['@field_name' => $field_name]),
Chris@0 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),
Chris@0 73 'relationship' => [
Chris@0 74 'group' => t('Comment'),
Chris@0 75 'label' => t('Comments'),
Chris@0 76 'base' => 'comment_field_data',
Chris@0 77 'base field' => 'entity_id',
Chris@0 78 'relationship field' => $entity_type->getKey('id'),
Chris@0 79 'id' => 'standard',
Chris@0 80 'extra' => [
Chris@0 81 [
Chris@0 82 'field' => 'entity_type',
Chris@0 83 'value' => $entity_type_id,
Chris@0 84 ],
Chris@0 85 [
Chris@0 86 'field' => 'field_name',
Chris@0 87 'value' => $field_name,
Chris@0 88 ],
Chris@0 89 ],
Chris@0 90 ],
Chris@0 91 ];
Chris@0 92 }
Chris@0 93 }
Chris@0 94 }
Chris@0 95 }