flag.views.inc
7.78 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* @file
* Provides support for the Views module.
*/
/**
* Implements hook_views_plugins().
*/
function flag_views_plugins() {
return array(
'argument validator' => array(
'flag_flaggable_node' => array(
'title' => t('Flaggable node'),
'flag type' => 'node',
'handler' => 'flag_plugin_argument_validate_flaggability',
'path' => drupal_get_path('module', 'flag') . '/includes',
),
'flag_flaggable_user' => array(
'title' => t('Flaggable user'),
'flag type' => 'user',
'handler' => 'flag_plugin_argument_validate_flaggability',
'path' => drupal_get_path('module', 'flag') . '/includes',
),
// A comment validator won't be very useful. Moreover, having it will
// contribute to the argument object's $options polution, so let's skip
// it.
),
);
}
/**
* Implements hook_views_data().
*/
function flag_views_data() {
$data = array();
$data['flagging']['table']['group'] = t('Flags');
$data['flag_counts']['table']['group'] = t('Flags');
// Notify views from flag 2.x of our changes to views data.
// @see flag_update_7301().
$data['flag_content']['moved to'] = 'flagging';
// @see flag_update_7303().
$data['flag_content']['content_id']['moved to'] = array('flagging', 'entity_id');
$data['flagging']['uid'] = array(
'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' => array(
'base' => 'users',
'title' => t('User'),
'help' => t('Relate an item to the user that flagged it.'),
'handler' => 'views_handler_relationship',
'label' => t('Flag user'),
),
'filter' => array(
'handler' => 'views_handler_filter_user_name',
),
'argument' => array(
'handler' => 'views_handler_argument_numeric',
),
'field' => array(
'handler' => 'views_handler_field_user',
),
);
$data['flagging']['timestamp'] = array(
'title' => t('Flagged time'),
'help' => t('Display the time the content was flagged by a user.'),
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
'argument' => array(
'handler' => 'views_handler_argument_date',
),
);
// Argument for content ID, used for "Who's flagged this" views.
$data['flagging']['entity_id'] = array(
'title' => t('Content ID'),
'help' => t('The unique ID of the object that has been flagged.'),
'argument' => array(
'handler' => 'flag_handler_argument_entity_id',
),
);
// Specialized is null/is not null filter.
$data['flagging']['flagged'] = array(
'title' => t('Flagged'),
'real field' => 'uid',
'field' => array(
'handler' => 'flag_handler_field_flagged',
'label' => t('Flagged'),
'help' => t('A boolean field to show whether the flag is set or not.'),
),
'filter' => array(
'handler' => 'flag_handler_filter_flagged',
'label' => t('Flagged'),
'help' => t('Filter to ensure content has or has not been flagged.'),
),
'sort' => array(
'handler' => 'flag_handler_sort_flagged',
'label' => t('Flagged'),
'help' => t('Sort by whether entities have or have not been flagged.'),
),
);
// Flag content links.
$data['flagging']['ops'] = array(
'title' => t('Flag link'),
'help' => t('Display flag/unflag link.'),
'field' => array(
'handler' => 'flag_handler_field_ops',
),
);
$data['flag_counts']['count'] = array(
'title' => t('Flag counter'),
'help' => t('The number of times a piece of content is flagged by any user.'),
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'argument' => array(
'handler' => 'views_handler_argument_numeric',
),
);
$data['flag_counts']['last_updated'] = array(
'title' => t('Time last flagged'),
'help' => t('The time a piece of content was most recently flagged by any user.'),
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
'argument' => array(
'handler' => 'views_handler_argument_date',
),
);
return $data;
}
/**
* Implements hook_views_data_alter().
*/
function flag_views_data_alter(&$data) {
foreach (array_keys(flag_fetch_definition()) as $flag_type) {
$flag = flag_flag::factory_by_entity_type($flag_type);
$info = $flag->get_views_info();
if (!isset($info)) {
continue;
}
if (!empty($info['join field'])) {
// Add the flag relationship.
$data[$info['views table']]['flag_content_rel'] = array(
'group' => t('Flags'),
'title' => $info['title'],
'help' => $info['help'],
'relationship' => array(
'flag type' => $flag_type,
'handler' => 'flag_handler_relationship_content',
'label' => t('flag'),
'base' => 'flagging',
'base field' => 'entity_id',
'relationship field' => $info['join field'],
),
);
// Add the flag counter relationship.
$data[$info['views table']]['flag_count_rel'] = array(
'group' => t('Flags'),
'title' => $info['counter title'],
'help' => $info['counter help'],
'relationship' => array(
'flag type' => $flag_type,
'handler' => 'flag_handler_relationship_counts',
'label' => t('counter'),
'base' => 'flag_counts',
'base field' => 'entity_id',
'relationship field' => $info['join field'],
),
);
}
}
// Add a relationship for the user that flagged any type of content.
$data['users']['flag_user_content_rel'] = array(
'group' => t('Flags'),
'title' => t("User's flaggings"),
'help' => t('Relate users to the flaggings they have made on objects, using a particular flag.'),
'relationship' => array(
'base' => 'flagging',
'base field' => 'uid',
'relationship field' => 'uid',
'handler' => 'flag_handler_relationship_user_content',
'label' => t('user flagged content'),
),
);
}
/**
* Implements hook_views_query_substitutions().
*
* Allow replacement of current user's session id so we can cache these queries.
*/
function flag_views_query_substitutions() {
return array(
'***FLAG_CURRENT_USER_SID***' => flag_get_sid(),
);
}
/**
* A helper function that creates a radio list of available flags.
*
* This function is used to select the desired flag when setting up flag
* relationships and fields.
*/
function flag_views_flag_config_form($form_type, $entity_type, $current_flag) {
$flags = flag_get_flags($entity_type);
$options = array();
foreach ($flags as $flag) {
$options[$flag->name] = $flag->get_title();
}
$form = array(
'#type' => $form_type,
'#title' => t('Flag'),
'#options' => $options,
'#default_value' => $current_flag,
'#required' => TRUE,
);
return $form;
}
/**
* Helper function that gets the first defined flag and returns its name.
*/
function flag_views_flag_default($entity_type) {
$default_flag = &drupal_static(__FUNCTION__, array());
if (!array_key_exists($entity_type, $default_flag)) {
$flag = array_shift(flag_get_flags($entity_type));
$default_flag[$entity_type] = $flag ? $flag->name : NULL;
}
return $default_flag[$entity_type];
}