comment.views.inc 3.4 KB

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