1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Contains the flag_entity class.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Base entity flag handler.
|
10
|
*/
|
11
|
class flag_entity extends flag_flag {
|
12
|
/**
|
13
|
* Adds additional options that are common for all entity types.
|
14
|
*/
|
15
|
function options() {
|
16
|
$options = parent::options();
|
17
|
$options += array(
|
18
|
// Output the flag in the entity links.
|
19
|
// This is empty for now and will get overriden for different
|
20
|
// entities.
|
21
|
// @see hook_entity_view().
|
22
|
'show_in_links' => array(),
|
23
|
// Output the flag as individual pseudofields.
|
24
|
'show_as_field' => FALSE,
|
25
|
// Add a checkbox for the flag in the entity form.
|
26
|
// @see hook_field_attach_form().
|
27
|
'show_on_form' => FALSE,
|
28
|
'access_author' => '',
|
29
|
'show_contextual_link' => FALSE,
|
30
|
);
|
31
|
return $options;
|
32
|
}
|
33
|
|
34
|
/**
|
35
|
* Options form extras for the generic entity flag.
|
36
|
*/
|
37
|
function options_form(&$form) {
|
38
|
$bundles = array();
|
39
|
$entity_info = entity_get_info($this->entity_type);
|
40
|
foreach ($entity_info['bundles'] as $bundle_key => $bundle) {
|
41
|
$bundles[$bundle_key] = check_plain($bundle['label']);
|
42
|
}
|
43
|
$form['access']['types'] = array(
|
44
|
'#type' => 'checkboxes',
|
45
|
'#title' => t('Bundles'),
|
46
|
'#options' => $bundles,
|
47
|
'#description' => t('Select the bundles that this flag may be used on. Leave blank to allow on all bundles for the entity type.'),
|
48
|
'#default_value' => $this->types,
|
49
|
);
|
50
|
|
51
|
// Add checkboxes to show flag link on each entity view mode.
|
52
|
$options = array();
|
53
|
$defaults = array();
|
54
|
$entity_view_modes = $entity_info['view modes'];
|
55
|
foreach ($entity_view_modes as $name => $view_mode) {
|
56
|
$options[$name] = t('Display on @name view mode', array('@name' => $view_mode['label']));
|
57
|
$defaults[$name] = !empty($this->show_in_links[$name]) ? $name : 0;
|
58
|
}
|
59
|
// Select the first display option by default if this is a new flag.
|
60
|
if (empty($this->fid)) {
|
61
|
$first_view_mode_keys = array_keys($entity_view_modes);
|
62
|
$first_view_mode = reset($first_view_mode_keys);
|
63
|
$defaults[$first_view_mode] = $first_view_mode;
|
64
|
}
|
65
|
|
66
|
$form['display']['show_in_links'] = array(
|
67
|
'#type' => 'checkboxes',
|
68
|
'#title' => t('Display in entity links'),
|
69
|
'#description' => t('Show the flag link with the other links on the entity.'),
|
70
|
'#options' => $options,
|
71
|
'#default_value' => $defaults,
|
72
|
);
|
73
|
|
74
|
$form['display']['show_as_field'] = array(
|
75
|
'#type' => 'checkbox',
|
76
|
'#title' => t('Display link as field'),
|
77
|
'#description' => t('Show the flag link as a pseudofield, which can be ordered among other entity elements in the "Manage display" settings for the entity type.'),
|
78
|
'#default_value' => isset($this->show_as_field) ? $this->show_as_field : TRUE,
|
79
|
);
|
80
|
if (empty($entity_info['fieldable'])) {
|
81
|
$form['display']['show_as_field']['#disabled'] = TRUE;
|
82
|
$form['display']['show_as_field']['#description'] = t("This entity type is not fieldable.");
|
83
|
}
|
84
|
|
85
|
$form['display']['show_on_form'] = array(
|
86
|
'#type' => 'checkbox',
|
87
|
'#title' => t('Display checkbox on entity edit form'),
|
88
|
'#default_value' => $this->show_on_form,
|
89
|
'#weight' => 5,
|
90
|
);
|
91
|
|
92
|
// We use FieldAPI to put the flag checkbox on the entity form, so therefore
|
93
|
// require the entity to be fielable. Since this is a potential DX
|
94
|
// headscratcher for a developer wondering where this option has gone,
|
95
|
// we disable it and explain why.
|
96
|
if (empty($entity_info['fieldable'])) {
|
97
|
$form['display']['show_on_form']['#disabled'] = TRUE;
|
98
|
$form['display']['show_on_form']['#description'] = t('This is only possible on entities which are fieldable.');
|
99
|
}
|
100
|
$form['display']['show_contextual_link'] = array(
|
101
|
'#type' => 'checkbox',
|
102
|
'#title' => t('Display in contextual links'),
|
103
|
'#default_value' => $this->show_contextual_link,
|
104
|
'#description' => t('Note that not all entity types support contextual links.'),
|
105
|
'#access' => module_exists('contextual'),
|
106
|
'#weight' => 10,
|
107
|
);
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Loads the entity object.
|
112
|
*/
|
113
|
function _load_entity($entity_id) {
|
114
|
if (is_numeric($entity_id)) {
|
115
|
$entity = entity_load($this->entity_type, array($entity_id));
|
116
|
return reset($entity);
|
117
|
}
|
118
|
return NULL;
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* Checks whether the flag applies for the current entity bundle.
|
123
|
*/
|
124
|
function applies_to_entity($entity) {
|
125
|
$entity_info = entity_get_info($this->entity_type);
|
126
|
// The following conditions are applied:
|
127
|
// - if the types array is empty, the flag applies to all bundles and thus
|
128
|
// to this entity.
|
129
|
// - if the entity has no bundles, the flag applies to the entity.
|
130
|
// - if the entity's bundle is in the list of types.
|
131
|
if (empty($this->types) || empty($entity_info['entity keys']['bundle']) || in_array($entity->{$entity_info['entity keys']['bundle']}, $this->types)) {
|
132
|
return TRUE;
|
133
|
}
|
134
|
return FALSE;
|
135
|
}
|
136
|
|
137
|
/**
|
138
|
* Provides permissions for this flag.
|
139
|
*
|
140
|
* @return
|
141
|
* An array of permissions for hook_permission().
|
142
|
*/
|
143
|
function get_permissions() {
|
144
|
// For entity flags, use the human label of the entity.
|
145
|
$entity_info = entity_get_info($this->entity_type);
|
146
|
$entity_label = $entity_info['label'];
|
147
|
return array(
|
148
|
"flag $this->name" => array(
|
149
|
'title' => t('Flag %entity entities as %flag_title', array(
|
150
|
'%flag_title' => $this->title,
|
151
|
'%entity' => $entity_label,
|
152
|
)),
|
153
|
),
|
154
|
"unflag $this->name" => array(
|
155
|
'title' => t('Unflag %entity entities as %flag_title', array(
|
156
|
'%flag_title' => $this->title,
|
157
|
'%entity' => $entity_label,
|
158
|
)),
|
159
|
),
|
160
|
);
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* Invoke a Rules event in reaction to a flagging or unflagging.
|
165
|
*
|
166
|
* @param $action
|
167
|
* Either 'flag' or 'unflag'.
|
168
|
* @param $flagging
|
169
|
* The flagging entity that is either newly created or about to be deleted.
|
170
|
* @param $entity_id
|
171
|
* The entity ID of entity being flagged or unflagged.
|
172
|
* @param $account
|
173
|
* The account performing the action.
|
174
|
*/
|
175
|
protected function invoke_rules_event($action, $flagging, $entity_id, $account) {
|
176
|
switch ($action) {
|
177
|
case 'flag':
|
178
|
$event_name = 'flag_flagged_' . $this->name;
|
179
|
break;
|
180
|
case 'unflag':
|
181
|
$event_name = 'flag_unflagged_' . $this->name;
|
182
|
break;
|
183
|
}
|
184
|
|
185
|
$variables = array(
|
186
|
'flag' => $this,
|
187
|
'flagged_' . $this->entity_type => $entity_id,
|
188
|
'flagging_user' => $account,
|
189
|
'flagging' => $flagging,
|
190
|
);
|
191
|
rules_invoke_event_by_args($event_name, $variables);
|
192
|
}
|
193
|
|
194
|
/**
|
195
|
* Returns the entity id, if it already exists.
|
196
|
*/
|
197
|
function get_entity_id($entity) {
|
198
|
$entity_info = entity_get_info($this->entity_type);
|
199
|
if ($entity && isset($entity->{$entity_info['entity keys']['id']})) {
|
200
|
return $entity->{$entity_info['entity keys']['id']};
|
201
|
}
|
202
|
}
|
203
|
|
204
|
/**
|
205
|
* Determine whether the flag should show a flag link in entity links.
|
206
|
*/
|
207
|
function shows_in_entity_links($view_mode) {
|
208
|
// Check for settings for the given view mode.
|
209
|
if (isset($this->show_in_links[$view_mode])) {
|
210
|
return (bool) $this->show_in_links[$view_mode];
|
211
|
}
|
212
|
return FALSE;
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* Returns token types for the current entity type.
|
217
|
*/
|
218
|
function get_labels_token_types() {
|
219
|
// The token type name might be different to the entity type name. If so,
|
220
|
// an own flag entity handler can be used for overriding this.
|
221
|
$entity_info = entity_get_info($this->entity_type);
|
222
|
if (isset($entity_info['token type'])) {
|
223
|
return array_merge(array($entity_info['token type']), parent::get_labels_token_types());
|
224
|
}
|
225
|
else {
|
226
|
return array_merge(array($this->entity_type), parent::get_labels_token_types());
|
227
|
}
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Replaces tokens.
|
232
|
*/
|
233
|
function replace_tokens($label, $contexts, $options, $entity_id) {
|
234
|
if ($entity_id && ($entity = $this->fetch_entity($entity_id))) {
|
235
|
$contexts[$this->entity_type] = $entity;
|
236
|
}
|
237
|
return parent::replace_tokens($label, $contexts, $options, $entity_id);
|
238
|
}
|
239
|
|
240
|
/**
|
241
|
* Returns a 'flag action' object.
|
242
|
*/
|
243
|
function get_flag_action($entity_id) {
|
244
|
$flag_action = parent::get_flag_action($entity_id);
|
245
|
$entity = $this->fetch_entity($entity_id);
|
246
|
$flag_action->content_title = entity_label($this->entity_type, $entity);
|
247
|
$flag_action->content_url = $this->_flag_url($this->entity_type . '/' . $this->get_entity_id($entity));
|
248
|
return $flag_action;
|
249
|
}
|
250
|
|
251
|
/**
|
252
|
* Returns objects the action may possible need.
|
253
|
*/
|
254
|
function get_relevant_action_objects($entity_id) {
|
255
|
return array(
|
256
|
$this->entity_type => $this->fetch_entity($entity_id),
|
257
|
);
|
258
|
}
|
259
|
|
260
|
/**
|
261
|
* Returns information for the Views integration.
|
262
|
*/
|
263
|
function get_views_info() {
|
264
|
$entity_info = entity_get_info($this->entity_type);
|
265
|
|
266
|
if (!isset($entity_info['base table'])) {
|
267
|
return NULL;
|
268
|
}
|
269
|
|
270
|
return array(
|
271
|
'views table' => $entity_info['base table'],
|
272
|
'join field' => $entity_info['entity keys']['id'],
|
273
|
'title field' => isset($entity_info['entity keys']['label']) ? $entity_info['entity keys']['label'] : '',
|
274
|
'title' => t('@entity_label flag', array('@entity_label' => $entity_info['label'])),
|
275
|
'help' => t('Limit results to only those entity flagged by a certain flag; Or display information about the flag set on a entity.'),
|
276
|
'counter title' => t('@entity_label flag counter', array('@entity_label' => $entity_info['label'])),
|
277
|
'counter help' => t('Include this to gain access to the flag counter field.'),
|
278
|
);
|
279
|
}
|
280
|
}
|