Drupal investigation

comment.views.inc 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. }