forked from socketwench/flag-drupal8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag.views.inc
174 lines (158 loc) · 4.28 KB
/
flag.views.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* @file
* Contains views API hooks for Flag module.
*/
/**
* Implements hook_views_data().
*/
function flag_views_data() {
$data = [];
$data['flagging']['table']['group'] = t('Flag');
$data['flag_counts']['table']['group'] = t('Flaggings');
$data['flagging']['table']['join'] = [
'node' => [
'type' => 'LEFT',
'left_field' => 'nid',
'field' => 'entity_id',
],
];
$data['flagging']['uid'] = [
'title' => t('User uid'),
'help' => t('The user that flagged an item. If you need more fields than the uid add the "Flags: User" relationship.'),
'relationship' => [
'base' => 'users',
'title' => t('User'),
'help' => t('Relate an item to the user that flagged it.'),
'id' => 'standard',
'label' => t('Flag user'),
],
'filter' => [
'id' => 'numeric',
],
'argument' => [
'id' => 'numeric',
],
'field' => [
'id' => 'user',
],
];
$data['flagging']['created'] = [
'title' => t('Last Flagged Time'),
'help' => t('Display latest time the content was flagged by a user.'),
'field' => [
'id' => 'date',
],
'sort' => [
'id' => 'date',
],
'filter' => [
'id' => 'date',
],
'argument' => [
'id' => 'date',
],
];
$data['flagging']['entity_id'] = [
'title' => t('Flaggable ID'),
'help' => t('The unique ID of the object that has been flagged.'),
'sort' => [
'id' => 'standard',
],
'argument' => [
'id' => 'FlagViewsFlaggableArgument',
],
];
// Flag content links.
$data['flagging']['link_flag'] = [
'field' => [
'title' => t('Flag Links'),
'help' => t('Display flag/unflag link.'),
'id' => 'flag_link',
],
];
// Specialized is null/is not null filter.
$data['flagging']['flagged'] = [
'title' => t('Flagged'),
'real field' => 'uid',
'field' => [
'id' => 'flag_flagged',
'label' => t('Flagged'),
'help' => t('A boolean field to show whether the flag is set or not.'),
],
'filter' => [
'id' => 'flag_filter',
'label' => t('Flagged'),
'help' => t('Filter to ensure content has or has not been flagged.'),
],
'sort' => [
'id' => 'flag_sort',
'label' => t('Flagged'),
'help' => t('Sort by whether entities have or have not been flagged.'),
],
];
$data['flag_counts']['count'] = [
'title' => t('Flag counter'),
'help' => t('The number of times a piece of content is flagged by any user.'),
'field' => [
'id' => 'numeric',
// 'click sortable' => TRUE,
],
/*'sort' => array(
'id' => 'groupby_numeric',
),
'filter' => array(
'id' => 'numeric',
),
'argument' => array(
'id' => 'numeric',
),*/
];
$data['flag_counts']['last_updated'] = [
'title' => t('Time last flagged'),
'help' => t('The time a piece of content was most recently flagged by any user.'),
'field' => [
'id' => 'date',
// 'click sortable' => TRUE,
],
/*'sort' => array(
'id' => 'date',
),
'filter' => array(
'id' => 'date',
),
'argument' => array(
'id' => 'date',
),*/
];
return $data;
}
/**
* Implements hook_views_data_alter().
*/
function flag_views_data_alter(array &$data) {
$flags = \Drupal::service('flag')->getFlags();
foreach ($flags as $fid => $flag) {
$flag_type_plugin = $flag->getFlagTypePlugin();
$flag_type_def = $flag_type_plugin->getPluginDefinition();
$entity_type = $flag_type_def['entity_type'];
$info = \Drupal::entityManager()->getDefinition($entity_type);
$base_table = $info->getBaseTable();
if ($flag_type_def['entity_type'] == 'node') {
$base_table = $info->getDataTable();
}
$data[$base_table]['flag_content_rel'] = [
'title' => t('@entity_label flag', ['@entity_label' => $entity_type]),
'help' => t('Limit results to only those entity flagged by a certain flag; Or display information about the flag set on a entity.'),
'relationship' => [
'group' => t('Flag'),
'label' => t('Flags'),
'base' => 'flagging',
'base field' => 'entity_id',
'relationship field' => $info->getKey('id'),
'id' => 'flag_relationship',
'flaggable' => $entity_type,
],
];
}
}