3 namespace Drupal\comment;
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\views\EntityViewsData;
9 * Provides views data for the comment entity type.
11 class CommentViewsData extends EntityViewsData {
16 public function getViewsData() {
17 $data = parent::getViewsData();
19 $data['comment_field_data']['table']['base']['help'] = $this->t('Comments are responses to content.');
20 $data['comment_field_data']['table']['base']['access query tag'] = 'comment_access';
22 $data['comment_field_data']['table']['wizard_id'] = 'comment';
24 $data['comment_field_data']['subject']['title'] = $this->t('Title');
25 $data['comment_field_data']['subject']['help'] = $this->t('The title of the comment.');
26 $data['comment_field_data']['subject']['field']['default_formatter'] = 'comment_permalink';
28 $data['comment_field_data']['name']['title'] = $this->t('Author');
29 $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.");
30 $data['comment_field_data']['name']['field']['default_formatter'] = 'comment_username';
32 $data['comment_field_data']['homepage']['title'] = $this->t("Author's website");
33 $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.");
35 $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.');
37 $data['comment_field_data']['created']['title'] = $this->t('Post date');
38 $data['comment_field_data']['created']['help'] = $this->t('Date and time of when the comment was created.');
40 $data['comment_field_data']['created_fulldata'] = [
41 'title' => $this->t('Created date'),
42 'help' => $this->t('Date in the form of CCYYMMDD.'),
45 'id' => 'date_fulldate',
49 $data['comment_field_data']['created_year_month'] = [
50 'title' => $this->t('Created year + month'),
51 'help' => $this->t('Date in the form of YYYYMM.'),
54 'id' => 'date_year_month',
58 $data['comment_field_data']['created_year'] = [
59 'title' => $this->t('Created year'),
60 'help' => $this->t('Date in the form of YYYY.'),
67 $data['comment_field_data']['created_month'] = [
68 'title' => $this->t('Created month'),
69 'help' => $this->t('Date in the form of MM (01 - 12).'),
76 $data['comment_field_data']['created_day'] = [
77 'title' => $this->t('Created day'),
78 'help' => $this->t('Date in the form of DD (01 - 31).'),
85 $data['comment_field_data']['created_week'] = [
86 'title' => $this->t('Created week'),
87 'help' => $this->t('Date in the form of WW (01 - 53).'),
94 $data['comment_field_data']['changed']['title'] = $this->t('Updated date');
95 $data['comment_field_data']['changed']['help'] = $this->t('Date and time of when the comment was last updated.');
97 $data['comment_field_data']['changed_fulldata'] = [
98 'title' => $this->t('Changed date'),
99 'help' => $this->t('Date in the form of CCYYMMDD.'),
101 'field' => 'changed',
102 'id' => 'date_fulldate',
106 $data['comment_field_data']['changed_year_month'] = [
107 'title' => $this->t('Changed year + month'),
108 'help' => $this->t('Date in the form of YYYYMM.'),
110 'field' => 'changed',
111 'id' => 'date_year_month',
115 $data['comment_field_data']['changed_year'] = [
116 'title' => $this->t('Changed year'),
117 'help' => $this->t('Date in the form of YYYY.'),
119 'field' => 'changed',
124 $data['comment_field_data']['changed_month'] = [
125 'title' => $this->t('Changed month'),
126 'help' => $this->t('Date in the form of MM (01 - 12).'),
128 'field' => 'changed',
129 'id' => 'date_month',
133 $data['comment_field_data']['changed_day'] = [
134 'title' => $this->t('Changed day'),
135 'help' => $this->t('Date in the form of DD (01 - 31).'),
137 'field' => 'changed',
142 $data['comment_field_data']['changed_week'] = [
143 'title' => $this->t('Changed week'),
144 'help' => $this->t('Date in the form of WW (01 - 53).'),
146 'field' => 'changed',
151 $data['comment_field_data']['status']['title'] = $this->t('Approved status');
152 $data['comment_field_data']['status']['help'] = $this->t('Whether the comment is approved (or still in the moderation queue).');
153 $data['comment_field_data']['status']['filter']['label'] = $this->t('Approved comment status');
154 $data['comment_field_data']['status']['filter']['type'] = 'yes-no';
156 $data['comment']['approve_comment'] = [
158 'title' => $this->t('Link to approve comment'),
159 'help' => $this->t('Provide a simple link to approve the comment.'),
160 'id' => 'comment_link_approve',
164 $data['comment']['replyto_comment'] = [
166 'title' => $this->t('Link to reply-to comment'),
167 'help' => $this->t('Provide a simple link to reply to the comment.'),
168 'id' => 'comment_link_reply',
172 $data['comment_field_data']['entity_id']['field']['id'] = 'commented_entity';
173 unset($data['comment_field_data']['entity_id']['relationship']);
175 $data['comment']['comment_bulk_form'] = [
176 'title' => $this->t('Comment operations bulk form'),
177 'help' => $this->t('Add a form element that lets you run operations on multiple comments.'),
179 'id' => 'comment_bulk_form',
183 $data['comment_field_data']['thread']['field'] = [
184 'title' => $this->t('Depth'),
185 'help' => $this->t('Display the depth of the comment if it is threaded.'),
186 'id' => 'comment_depth',
188 $data['comment_field_data']['thread']['sort'] = [
189 'title' => $this->t('Thread'),
190 'help' => $this->t('Sort by the threaded order. This will keep child comments together with their parents.'),
191 'id' => 'comment_thread',
193 unset($data['comment_field_data']['thread']['filter']);
194 unset($data['comment_field_data']['thread']['argument']);
196 $entities_types = \Drupal::entityManager()->getDefinitions();
198 // Provide a relationship for each entity type except comment.
199 foreach ($entities_types as $type => $entity_type) {
200 if ($type == 'comment' || !$entity_type->entityClassImplements(ContentEntityInterface::class) || !$entity_type->getBaseTable()) {
203 if ($fields = \Drupal::service('comment.manager')->getFields($type)) {
204 $data['comment_field_data'][$type] = [
206 'title' => $entity_type->getLabel(),
207 'help' => $this->t('The @entity_type to which the comment is a reply to.', ['@entity_type' => $entity_type->getLabel()]),
208 'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(),
209 'base field' => $entity_type->getKey('id'),
210 'relationship field' => 'entity_id',
212 'label' => $entity_type->getLabel(),
215 'field' => 'entity_type',
217 'table' => 'comment_field_data',
225 $data['comment_field_data']['uid']['title'] = $this->t('Author uid');
226 $data['comment_field_data']['uid']['help'] = $this->t('If you need more fields than the uid add the comment: author relationship');
227 $data['comment_field_data']['uid']['relationship']['title'] = $this->t('Author');
228 $data['comment_field_data']['uid']['relationship']['help'] = $this->t("The User ID of the comment's author.");
229 $data['comment_field_data']['uid']['relationship']['label'] = $this->t('author');
231 $data['comment_field_data']['pid']['title'] = $this->t('Parent CID');
232 $data['comment_field_data']['pid']['relationship']['title'] = $this->t('Parent comment');
233 $data['comment_field_data']['pid']['relationship']['help'] = $this->t('The parent comment');
234 $data['comment_field_data']['pid']['relationship']['label'] = $this->t('parent');
236 // Define the base group of this table. Fields that don't have a group defined
237 // will go into this field by default.
238 $data['comment_entity_statistics']['table']['group'] = $this->t('Comment Statistics');
240 // Provide a relationship for each entity type except comment.
241 foreach ($entities_types as $type => $entity_type) {
242 if ($type == 'comment' || !$entity_type->entityClassImplements(ContentEntityInterface::class) || !$entity_type->getBaseTable()) {
245 // This relationship does not use the 'field id' column, if the entity has
246 // multiple comment-fields, then this might introduce duplicates, in which
247 // case the site-builder should enable aggregation and SUM the comment_count
248 // field. We cannot create a relationship from the base table to
249 // {comment_entity_statistics} for each field as multiple joins between
250 // the same two tables is not supported.
251 if (\Drupal::service('comment.manager')->getFields($type)) {
252 $data['comment_entity_statistics']['table']['join'][$entity_type->getDataTable() ?: $entity_type->getBaseTable()] = [
254 'left_field' => $entity_type->getKey('id'),
255 'field' => 'entity_id',
258 'field' => 'entity_type',
266 $data['comment_entity_statistics']['last_comment_timestamp'] = [
267 'title' => $this->t('Last comment time'),
268 'help' => $this->t('Date and time of when the last comment was posted.'),
270 'id' => 'comment_last_timestamp',
280 $data['comment_entity_statistics']['last_comment_name'] = [
281 'title' => $this->t("Last comment author"),
282 'help' => $this->t('The name of the author of the last posted comment.'),
284 'id' => 'comment_ces_last_comment_name',
285 'no group by' => TRUE,
288 'id' => 'comment_ces_last_comment_name',
289 'no group by' => TRUE,
293 $data['comment_entity_statistics']['comment_count'] = [
294 'title' => $this->t('Comment count'),
295 'help' => $this->t('The number of comments an entity has.'),
310 $data['comment_entity_statistics']['last_updated'] = [
311 'title' => $this->t('Updated/commented date'),
312 'help' => $this->t('The most recent of last comment posted or entity updated time.'),
314 'id' => 'comment_ces_last_updated',
315 'no group by' => TRUE,
318 'id' => 'comment_ces_last_updated',
319 'no group by' => TRUE,
322 'id' => 'comment_ces_last_updated',
326 $data['comment_entity_statistics']['cid'] = [
327 'title' => $this->t('Last comment CID'),
328 'help' => $this->t('Display the last comment of an entity'),
330 'title' => $this->t('Last comment'),
331 'help' => $this->t('The last comment of an entity.'),
332 'group' => $this->t('Comment'),
334 'base field' => 'cid',
336 'label' => $this->t('Last Comment'),
340 $data['comment_entity_statistics']['last_comment_uid'] = [
341 'title' => $this->t('Last comment uid'),
342 'help' => $this->t('The User ID of the author of the last comment of an entity.'),
344 'title' => $this->t('Last comment author'),
346 'base field' => 'uid',
348 'label' => $this->t('Last comment author'),
361 $data['comment_entity_statistics']['entity_type'] = [
362 'title' => $this->t('Entity type'),
363 'help' => $this->t('The entity type to which the comment is a reply to.'),
377 $data['comment_entity_statistics']['field_name'] = [
378 'title' => $this->t('Comment field name'),
379 'help' => $this->t('The field name from which the comment originated.'),