comment.views.inc 3.3 KB

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