PageRenderTime 29ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/comment/src/CommentViewsData.php

http://github.com/drupal/drupal
PHP | 397 lines | 339 code | 43 blank | 15 comment | 11 complexity | 192b4d60490994dcb46e90200f79e233 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\comment;
  3. use Drupal\Core\Entity\ContentEntityInterface;
  4. use Drupal\views\EntityViewsData;
  5. /**
  6. * Provides views data for the comment entity type.
  7. */
  8. class CommentViewsData extends EntityViewsData {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. public function getViewsData() {
  13. $data = parent::getViewsData();
  14. $data['comment_field_data']['table']['base']['help'] = $this->t('Comments are responses to content.');
  15. $data['comment_field_data']['table']['base']['access query tag'] = 'comment_access';
  16. $data['comment_field_data']['table']['wizard_id'] = 'comment';
  17. $data['comment_field_data']['subject']['title'] = $this->t('Title');
  18. $data['comment_field_data']['subject']['help'] = $this->t('The title of the comment.');
  19. $data['comment_field_data']['subject']['field']['default_formatter'] = 'comment_permalink';
  20. $data['comment_field_data']['name']['title'] = $this->t('Author');
  21. $data['comment_field_data']['name']['help'] = $this->t("The name of the comment's author. Can be rendered as a link to the author's homepage.");
  22. $data['comment_field_data']['name']['field']['default_formatter'] = 'comment_username';
  23. $data['comment_field_data']['homepage']['title'] = $this->t("Author's website");
  24. $data['comment_field_data']['homepage']['help'] = $this->t("The website address of the comment's author. Can be rendered as a link. Will be empty if the author is a registered user.");
  25. $data['comment_field_data']['mail']['help'] = $this->t('Email of user that posted the comment. Will be empty if the author is a registered user.');
  26. $data['comment_field_data']['created']['title'] = $this->t('Post date');
  27. $data['comment_field_data']['created']['help'] = $this->t('Date and time of when the comment was created.');
  28. $data['comment_field_data']['created_fulldata'] = [
  29. 'title' => $this->t('Created date'),
  30. 'help' => $this->t('Date in the form of CCYYMMDD.'),
  31. 'argument' => [
  32. 'field' => 'created',
  33. 'id' => 'date_fulldate',
  34. ],
  35. ];
  36. $data['comment_field_data']['created_year_month'] = [
  37. 'title' => $this->t('Created year + month'),
  38. 'help' => $this->t('Date in the form of YYYYMM.'),
  39. 'argument' => [
  40. 'field' => 'created',
  41. 'id' => 'date_year_month',
  42. ],
  43. ];
  44. $data['comment_field_data']['created_year'] = [
  45. 'title' => $this->t('Created year'),
  46. 'help' => $this->t('Date in the form of YYYY.'),
  47. 'argument' => [
  48. 'field' => 'created',
  49. 'id' => 'date_year',
  50. ],
  51. ];
  52. $data['comment_field_data']['created_month'] = [
  53. 'title' => $this->t('Created month'),
  54. 'help' => $this->t('Date in the form of MM (01 - 12).'),
  55. 'argument' => [
  56. 'field' => 'created',
  57. 'id' => 'date_month',
  58. ],
  59. ];
  60. $data['comment_field_data']['created_day'] = [
  61. 'title' => $this->t('Created day'),
  62. 'help' => $this->t('Date in the form of DD (01 - 31).'),
  63. 'argument' => [
  64. 'field' => 'created',
  65. 'id' => 'date_day',
  66. ],
  67. ];
  68. $data['comment_field_data']['created_week'] = [
  69. 'title' => $this->t('Created week'),
  70. 'help' => $this->t('Date in the form of WW (01 - 53).'),
  71. 'argument' => [
  72. 'field' => 'created',
  73. 'id' => 'date_week',
  74. ],
  75. ];
  76. $data['comment_field_data']['changed']['title'] = $this->t('Updated date');
  77. $data['comment_field_data']['changed']['help'] = $this->t('Date and time of when the comment was last updated.');
  78. $data['comment_field_data']['changed_fulldata'] = [
  79. 'title' => $this->t('Changed date'),
  80. 'help' => $this->t('Date in the form of CCYYMMDD.'),
  81. 'argument' => [
  82. 'field' => 'changed',
  83. 'id' => 'date_fulldate',
  84. ],
  85. ];
  86. $data['comment_field_data']['changed_year_month'] = [
  87. 'title' => $this->t('Changed year + month'),
  88. 'help' => $this->t('Date in the form of YYYYMM.'),
  89. 'argument' => [
  90. 'field' => 'changed',
  91. 'id' => 'date_year_month',
  92. ],
  93. ];
  94. $data['comment_field_data']['changed_year'] = [
  95. 'title' => $this->t('Changed year'),
  96. 'help' => $this->t('Date in the form of YYYY.'),
  97. 'argument' => [
  98. 'field' => 'changed',
  99. 'id' => 'date_year',
  100. ],
  101. ];
  102. $data['comment_field_data']['changed_month'] = [
  103. 'title' => $this->t('Changed month'),
  104. 'help' => $this->t('Date in the form of MM (01 - 12).'),
  105. 'argument' => [
  106. 'field' => 'changed',
  107. 'id' => 'date_month',
  108. ],
  109. ];
  110. $data['comment_field_data']['changed_day'] = [
  111. 'title' => $this->t('Changed day'),
  112. 'help' => $this->t('Date in the form of DD (01 - 31).'),
  113. 'argument' => [
  114. 'field' => 'changed',
  115. 'id' => 'date_day',
  116. ],
  117. ];
  118. $data['comment_field_data']['changed_week'] = [
  119. 'title' => $this->t('Changed week'),
  120. 'help' => $this->t('Date in the form of WW (01 - 53).'),
  121. 'argument' => [
  122. 'field' => 'changed',
  123. 'id' => 'date_week',
  124. ],
  125. ];
  126. $data['comment_field_data']['status']['title'] = $this->t('Approved status');
  127. $data['comment_field_data']['status']['help'] = $this->t('Whether the comment is approved (or still in the moderation queue).');
  128. $data['comment_field_data']['status']['filter']['label'] = $this->t('Approved comment status');
  129. $data['comment_field_data']['status']['filter']['type'] = 'yes-no';
  130. $data['comment']['approve_comment'] = [
  131. 'field' => [
  132. 'title' => $this->t('Link to approve comment'),
  133. 'help' => $this->t('Provide a simple link to approve the comment.'),
  134. 'id' => 'comment_link_approve',
  135. ],
  136. ];
  137. $data['comment']['replyto_comment'] = [
  138. 'field' => [
  139. 'title' => $this->t('Link to reply-to comment'),
  140. 'help' => $this->t('Provide a simple link to reply to the comment.'),
  141. 'id' => 'comment_link_reply',
  142. ],
  143. ];
  144. $data['comment_field_data']['entity_id']['field']['id'] = 'commented_entity';
  145. unset($data['comment_field_data']['entity_id']['relationship']);
  146. $data['comment']['comment_bulk_form'] = [
  147. 'title' => $this->t('Comment operations bulk form'),
  148. 'help' => $this->t('Add a form element that lets you run operations on multiple comments.'),
  149. 'field' => [
  150. 'id' => 'comment_bulk_form',
  151. ],
  152. ];
  153. $data['comment_field_data']['thread']['field'] = [
  154. 'title' => $this->t('Depth'),
  155. 'help' => $this->t('Display the depth of the comment if it is threaded.'),
  156. 'id' => 'comment_depth',
  157. ];
  158. $data['comment_field_data']['thread']['sort'] = [
  159. 'title' => $this->t('Thread'),
  160. 'help' => $this->t('Sort by the threaded order. This will keep child comments together with their parents.'),
  161. 'id' => 'comment_thread',
  162. ];
  163. unset($data['comment_field_data']['thread']['filter']);
  164. unset($data['comment_field_data']['thread']['argument']);
  165. $entities_types = \Drupal::entityTypeManager()->getDefinitions();
  166. // Provide a relationship for each entity type except comment.
  167. foreach ($entities_types as $type => $entity_type) {
  168. if ($type == 'comment' || !$entity_type->entityClassImplements(ContentEntityInterface::class) || !$entity_type->getBaseTable()) {
  169. continue;
  170. }
  171. if ($fields = \Drupal::service('comment.manager')->getFields($type)) {
  172. $data['comment_field_data'][$type] = [
  173. 'relationship' => [
  174. 'title' => $entity_type->getLabel(),
  175. 'help' => $this->t('The @entity_type to which the comment is a reply to.', ['@entity_type' => $entity_type->getLabel()]),
  176. 'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(),
  177. 'base field' => $entity_type->getKey('id'),
  178. 'relationship field' => 'entity_id',
  179. 'id' => 'standard',
  180. 'label' => $entity_type->getLabel(),
  181. 'extra' => [
  182. [
  183. 'field' => 'entity_type',
  184. 'value' => $type,
  185. 'table' => 'comment_field_data',
  186. ],
  187. ],
  188. ],
  189. ];
  190. }
  191. }
  192. $data['comment_field_data']['uid']['title'] = $this->t('Author uid');
  193. $data['comment_field_data']['uid']['help'] = $this->t('If you need more fields than the uid add the comment: author relationship');
  194. $data['comment_field_data']['uid']['relationship']['title'] = $this->t('Author');
  195. $data['comment_field_data']['uid']['relationship']['help'] = $this->t("The User ID of the comment's author.");
  196. $data['comment_field_data']['uid']['relationship']['label'] = $this->t('author');
  197. $data['comment_field_data']['pid']['title'] = $this->t('Parent CID');
  198. $data['comment_field_data']['pid']['relationship']['title'] = $this->t('Parent comment');
  199. $data['comment_field_data']['pid']['relationship']['help'] = $this->t('The parent comment');
  200. $data['comment_field_data']['pid']['relationship']['label'] = $this->t('parent');
  201. // Define the base group of this table. Fields that don't have a group defined
  202. // will go into this field by default.
  203. $data['comment_entity_statistics']['table']['group'] = $this->t('Comment Statistics');
  204. // Provide a relationship for each entity type except comment.
  205. foreach ($entities_types as $type => $entity_type) {
  206. if ($type == 'comment' || !$entity_type->entityClassImplements(ContentEntityInterface::class) || !$entity_type->getBaseTable()) {
  207. continue;
  208. }
  209. // This relationship does not use the 'field id' column, if the entity has
  210. // multiple comment-fields, then this might introduce duplicates, in which
  211. // case the site-builder should enable aggregation and SUM the comment_count
  212. // field. We cannot create a relationship from the base table to
  213. // {comment_entity_statistics} for each field as multiple joins between
  214. // the same two tables is not supported.
  215. if (\Drupal::service('comment.manager')->getFields($type)) {
  216. $data['comment_entity_statistics']['table']['join'][$entity_type->getDataTable() ?: $entity_type->getBaseTable()] = [
  217. 'type' => 'INNER',
  218. 'left_field' => $entity_type->getKey('id'),
  219. 'field' => 'entity_id',
  220. 'extra' => [
  221. [
  222. 'field' => 'entity_type',
  223. 'value' => $type,
  224. ],
  225. ],
  226. ];
  227. }
  228. }
  229. $data['comment_entity_statistics']['last_comment_timestamp'] = [
  230. 'title' => $this->t('Last comment time'),
  231. 'help' => $this->t('Date and time of when the last comment was posted.'),
  232. 'field' => [
  233. 'id' => 'comment_last_timestamp',
  234. ],
  235. 'sort' => [
  236. 'id' => 'date',
  237. ],
  238. 'filter' => [
  239. 'id' => 'date',
  240. ],
  241. ];
  242. $data['comment_entity_statistics']['last_comment_name'] = [
  243. 'title' => $this->t("Last comment author"),
  244. 'help' => $this->t('The name of the author of the last posted comment.'),
  245. 'field' => [
  246. 'id' => 'comment_ces_last_comment_name',
  247. 'no group by' => TRUE,
  248. ],
  249. 'sort' => [
  250. 'id' => 'comment_ces_last_comment_name',
  251. 'no group by' => TRUE,
  252. ],
  253. ];
  254. $data['comment_entity_statistics']['comment_count'] = [
  255. 'title' => $this->t('Comment count'),
  256. 'help' => $this->t('The number of comments an entity has.'),
  257. 'field' => [
  258. 'id' => 'numeric',
  259. ],
  260. 'filter' => [
  261. 'id' => 'numeric',
  262. ],
  263. 'sort' => [
  264. 'id' => 'standard',
  265. ],
  266. 'argument' => [
  267. 'id' => 'standard',
  268. ],
  269. ];
  270. $data['comment_entity_statistics']['last_updated'] = [
  271. 'title' => $this->t('Updated/commented date'),
  272. 'help' => $this->t('The most recent of last comment posted or entity updated time.'),
  273. 'field' => [
  274. 'id' => 'comment_ces_last_updated',
  275. 'no group by' => TRUE,
  276. ],
  277. 'sort' => [
  278. 'id' => 'comment_ces_last_updated',
  279. 'no group by' => TRUE,
  280. ],
  281. 'filter' => [
  282. 'id' => 'comment_ces_last_updated',
  283. ],
  284. ];
  285. $data['comment_entity_statistics']['cid'] = [
  286. 'title' => $this->t('Last comment CID'),
  287. 'help' => $this->t('Display the last comment of an entity'),
  288. 'relationship' => [
  289. 'title' => $this->t('Last comment'),
  290. 'help' => $this->t('The last comment of an entity.'),
  291. 'group' => $this->t('Comment'),
  292. 'base' => 'comment',
  293. 'base field' => 'cid',
  294. 'id' => 'standard',
  295. 'label' => $this->t('Last Comment'),
  296. ],
  297. ];
  298. $data['comment_entity_statistics']['last_comment_uid'] = [
  299. 'title' => $this->t('Last comment uid'),
  300. 'help' => $this->t('The User ID of the author of the last comment of an entity.'),
  301. 'relationship' => [
  302. 'title' => $this->t('Last comment author'),
  303. 'base' => 'users',
  304. 'base field' => 'uid',
  305. 'id' => 'standard',
  306. 'label' => $this->t('Last comment author'),
  307. ],
  308. 'filter' => [
  309. 'id' => 'numeric',
  310. ],
  311. 'argument' => [
  312. 'id' => 'numeric',
  313. ],
  314. 'field' => [
  315. 'id' => 'numeric',
  316. ],
  317. ];
  318. $data['comment_entity_statistics']['entity_type'] = [
  319. 'title' => $this->t('Entity type'),
  320. 'help' => $this->t('The entity type to which the comment is a reply to.'),
  321. 'field' => [
  322. 'id' => 'standard',
  323. ],
  324. 'filter' => [
  325. 'id' => 'string',
  326. ],
  327. 'argument' => [
  328. 'id' => 'string',
  329. ],
  330. 'sort' => [
  331. 'id' => 'standard',
  332. ],
  333. ];
  334. $data['comment_entity_statistics']['field_name'] = [
  335. 'title' => $this->t('Comment field name'),
  336. 'help' => $this->t('The field name from which the comment originated.'),
  337. 'field' => [
  338. 'id' => 'standard',
  339. ],
  340. 'filter' => [
  341. 'id' => 'string',
  342. ],
  343. 'argument' => [
  344. 'id' => 'string',
  345. ],
  346. 'sort' => [
  347. 'id' => 'standard',
  348. ],
  349. ];
  350. return $data;
  351. }
  352. }