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.');
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';
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.");
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.');
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.');
39 $data['comment_field_data']['created_fulldata'] = [
40 'title' => $this->t('Created date'),
41 'help' => $this->t('Date in the form of CCYYMMDD.'),
44 'id' => 'date_fulldate',
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.'),
53 'id' => 'date_year_month',
57 $data['comment_field_data']['created_year'] = [
58 'title' => $this->t('Created year'),
59 'help' => $this->t('Date in the form of YYYY.'),
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).'),
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).'),
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).'),
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.');
96 $data['comment_field_data']['changed_fulldata'] = [
97 'title' => $this->t('Changed date'),
98 'help' => $this->t('Date in the form of CCYYMMDD.'),
100 'field' => 'changed',
101 'id' => 'date_fulldate',
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.'),
109 'field' => 'changed',
110 'id' => 'date_year_month',
114 $data['comment_field_data']['changed_year'] = [
115 'title' => $this->t('Changed year'),
116 'help' => $this->t('Date in the form of YYYY.'),
118 'field' => 'changed',
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).'),
127 'field' => 'changed',
128 'id' => 'date_month',
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).'),
136 'field' => 'changed',
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).'),
145 'field' => 'changed',
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';
155 $data['comment']['approve_comment'] = [
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',
163 $data['comment']['replyto_comment'] = [
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',
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',
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',
181 unset($data['comment_field_data']['thread']['filter']);
182 unset($data['comment_field_data']['thread']['argument']);
184 $entities_types = \Drupal::entityManager()->getDefinitions();
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()) {
191 if ($fields = \Drupal::service('comment.manager')->getFields($type)) {
192 $data['comment_field_data'][$type] = [
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',
200 'label' => $entity_type->getLabel(),
203 'field' => 'entity_type',
205 'table' => 'comment_field_data'
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');
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');
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');
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()) {
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()] = [
242 'left_field' => $entity_type->getKey('id'),
243 'field' => 'entity_id',
246 'field' => 'entity_type',
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.'),
258 'id' => 'comment_last_timestamp',
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.'),
272 'id' => 'comment_ces_last_comment_name',
273 'no group by' => TRUE,
276 'id' => 'comment_ces_last_comment_name',
277 'no group by' => TRUE,
281 $data['comment_entity_statistics']['comment_count'] = [
282 'title' => $this->t('Comment count'),
283 'help' => $this->t('The number of comments an entity has.'),
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.'),
302 'id' => 'comment_ces_last_updated',
303 'no group by' => TRUE,
306 'id' => 'comment_ces_last_updated',
307 'no group by' => TRUE,
310 'id' => 'comment_ces_last_updated',
314 $data['comment_entity_statistics']['cid'] = [
315 'title' => $this->t('Last comment CID'),
316 'help' => $this->t('Display the last comment of an entity'),
318 'title' => $this->t('Last comment'),
319 'help' => $this->t('The last comment of an entity.'),
320 'group' => $this->t('Comment'),
322 'base field' => 'cid',
324 'label' => $this->t('Last Comment'),
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.'),
332 'title' => $this->t('Last comment author'),
334 'base field' => 'uid',
336 'label' => $this->t('Last comment author'),
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.'),
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.'),