-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocial_comments.module
290 lines (251 loc) · 8 KB
/
social_comments.module
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
* @file
* Social comments module.
*/
require_once 'includes/social_comments.google.inc';
require_once 'includes/social_comments.facebook.inc';
require_once 'includes/social_comments.twitter.inc';
define('SOCIAL_COMMENTS_URL_MAX_LENGTH', 2048);
/**
* Implements hook_menu().
*/
function social_comments_menu() {
$items = array();
$items['admin/config/system/social-comments'] = array(
'title' => 'Social comments',
'description' => 'Configure social comments',
'page callback' => 'drupal_get_form',
'page arguments' => array('social_comments_settings_form'),
'access arguments' => array('administer site configuration'),
'file' => 'includes/social_comments.admin.inc',
);
return $items;
}
/**
* Implements hook_libraries_info().
*/
function social_comments_libraries_info() {
$libraries['acTwitterConversation'] = array(
'name' => 'acTwitterConversation',
'vendor url' => 'http://adriancrepaz.com/twitter_conversions_api',
'download url' => 'https://github.com/adriancrepaz/acTwitterConversation',
'files' => array(
'php' => array('acTwitterConversation.php'),
),
'version' => '1.0',
);
return $libraries;
}
/**
* Implements hook_theme().
*/
function social_comments_theme() {
$themes = array();
$themes['social_comments_items'] = array(
'variables' => array(
'comments' => array(),
'bundle' => NULL,
'entity_type' => NULL,
'type' => NULL,
),
'template' => 'social-comments-items',
'path' => drupal_get_path('module', 'social_comments') . '/templates',
);
return $themes;
}
/**
* Prepares variables for social-comments-items template.
*/
function template_preprocess_social_comments_items(&$variables) {
$comments = &$variables['comments'];
if (is_array($comments)) {
foreach ($comments as &$comment) {
$comment['date'] = format_date($comment['timestamp'], 'medium');
$comment['userphoto'] = theme('image', array('path' => $comment['userphoto']));
}
}
$variables['theme_hook_suggestions'][] = 'social_comments_items__' . $variables['entity_type'];
$variables['theme_hook_suggestions'][] = 'social_comments_items__' . $variables['bundle'];
$variables['theme_hook_suggestions'][] = 'social_comments_items__' . $variables['type'];
$variables['theme_hook_suggestions'][] = 'social_comments_items__' . $variables['type'] . '__' . $variables['bundle'];
}
/**
* Implements hook_field_info().
*/
function social_comments_field_info() {
return array(
'social_comments_field' => array(
'label' => t('Social comments'),
'description' => t('Store an URL string.'),
'default_widget' => 'social_comments',
'default_formatter' => 'social_comments_google',
),
);
}
/**
* Implements hook_field_is_empty().
*/
function social_comments_field_is_empty($item, $field) {
return empty($item['url']);
}
/**
* Implements hook_field_insert().
*/
function social_comments_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
foreach ($items as $delta => $value) {
trim($items[$delta]['url']);
}
}
/**
* Implements hook_field_update().
*/
function social_comments_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
foreach ($items as $delta => $value) {
trim($items[$delta]['url']);
}
}
/**
* Implements hook_field_widget_info().
*/
function social_comments_field_widget_info() {
return array(
'social_comments_field' => array(
'label' => 'Social field',
'field types' => array('social_comments_field'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
);
}
/**
* Implements hook_field_widget_form().
*/
function social_comments_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element += array(
'#type' => $instance['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
);
return $element;
}
/**
* Implements hook_element_info().
*/
function social_comments_element_info() {
$elements = array();
$elements['social_comments_field'] = array(
'#input' => TRUE,
'#process' => array('social_comments_field_process'),
'#theme_wrappers' => array('form_element'),
);
return $elements;
}
/**
* Processes the element before displaying the field.
*/
function social_comments_field_process($element, $form_state, $complete_form) {
$element['url'] = array(
'#type' => 'textfield',
'#maxlength' => SOCIAL_COMMENTS_URL_MAX_LENGTH,
'#title' => t('URL'),
'#default_value' => isset($element['#value']['url']) ? $element['#value']['url'] : NULL,
);
return $element;
}
/**
* Implements hook_field_formatter_info().
*/
function social_comments_field_formatter_info() {
$formatters = array();
$formatters['social_comments_google'] = array(
'label' => t('Social comments (Google)'),
'field types' => array('social_comments_field'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'settings' => array(
'count' => 0,
),
);
// @todo: Rewrite facebook implementation.
$formatters['social_comments_facebook'] = array(
'label' => t('Social comments (Facebook)'),
'field types' => array('social_comments_field'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'settings' => array(
'count' => 0,
),
);
// @todo: Write own implementation.
// $library = libraries_detect('acTwitterConversation');
// if ($library['installed']) {
// $formatters['social_comments_twitter'] = array(
// 'label' => t('Social comments (Twitter)'),
// 'field types' => array('social_comments_field'),
// 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
// 'settings' => array(
// 'count' => 0,
// ),
// );
// }
return $formatters;
}
/**
* Implements hook_field_formatter_settings_form().
*/
function social_comments_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'social_comments_google' || $display['type'] == 'social_comments_facebook' || $display['type'] == 'social_comments_twitter') {
$element['count'] = array(
'#type' => 'textfield',
'#title' => t('Count'),
'#default_value' => $settings['count'],
'#description' => t('Count of items to show. Input 0 to display all.'),
);
}
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function social_comments_field_formatter_settings_summary($field, $instance, $view_mode) {
$summary = array();
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
if ($display['type'] == 'social_comments_google' || $display['type'] == 'social_comments_facebook' || $display['type'] == 'social_comments_twitter') {
$count = $settings['count'];
if (!empty($count)) {
$summary[] = t('Display: @count items', array('@count' => $count));
}
else {
$summary[] = t('Display all items');
}
}
return implode('</ br>', $summary);
}
/**
* Implements hook_field_formatter_view().
*/
function social_comments_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$elements = array();
$settings = $display['settings'];
if ($display['type'] == 'social_comments_google' || $display['type'] == 'social_comments_facebook' || $display['type'] == 'social_comments_twitter') {
$type = str_replace('social_comments_', '', $display['type']);
$formatter = 'Social' . drupal_ucfirst($type) . 'Formatter';
foreach ($items as $delta => $item) {
$count = $settings['count'];
$bundle = $instance['bundle'];
$comments = new $formatter();
$comments = $comments->getData($item['url'], $count);
if ($comments) {
$elements[$delta] = array(
'#theme' => 'social_comments_items',
'#comments' => $comments,
'#bundle' => $bundle,
'#entity_type' => $entity_type,
'#type' => $type,
);
}
}
}
return $elements;
}