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