forked from artesis/ding_content
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathding_content.module
More file actions
472 lines (418 loc) · 13.8 KB
/
ding_content.module
File metadata and controls
472 lines (418 loc) · 13.8 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<?php
include_once('ding_content.features.inc');
/**
* @file
* Various integration with panels to create panel panes and more.
*/
/**
* Implements hook_menu().
*/
function ding_content_menu() {
$items = array();
$items['ding_content/media'] = array(
'title' => 'Media browser list',
'description' => 'Ajax Callback for getting media',
'page callback' => 'ding_content_media_browser_list',
'access callback' => 'media_access',
'access arguments' => array('view'),
'type' => MENU_CALLBACK,
'file' => 'ding_content.admin.inc',
);
$items['admin/config/ding/category_menu'] = array(
'title' => 'Category menu',
'description' => 'Configure category foldable menu settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_content_foldable_menu_admin'),
'access arguments' => array('administer category menu'),
'file' => 'ding_content.admin.inc',
);
$items['ding_content/relevant/nojs/%'] = array(
'page callback' => '_ding_content_relevant_content',
'page arguments' => array(2, 3),
'access arguments' => array('access content'),
'file' => 'ding_content.pages.inc',
'type' => MENU_CALLBACK,
);
$items['ding_content/relevant/ajax/%'] = array(
'delivery callback' => 'ajax_deliver',
) + $items['ding_content/relevant/nojs/%'];
return $items;
}
/**
* Implements hook_permission().
*/
function ding_content_permission() {
$perm = array();
$perm['administer category menu'] = array(
'title' => t('Category menu settings'),
'description' => t('Change the behavior of category menu'),
);
return $perm;
}
/**
* Implements hook_enable().
*
* Create taxonomy vocabolary if needed.
*/
function ding_content_enable() {
// Create wysiwyg profile if needed.
if (!wysiwyg_profile_load('ding_wysiwyg')) {
$wysiwyg = array(
'format' => 'ding_wysiwyg',
'editor' => 'ckeditor',
'settings' => array(
'default' => 1,
'user_choose' => 0,
'show_toggle' => 1,
'theme' => 'advanced',
'language' => 'da',
'buttons' => array(
'default' => array(
'Bold' => 1,
'Italic' => 1,
'Underline' => 1,
'JustifyLeft' => 1,
'JustifyCenter' => 1,
'JustifyRight' => 1,
'BulletedList' => 1,
'NumberedList' => 1,
'Undo' => 1,
'Redo' => 1,
'Link' => 1,
'Unlink' => 1,
'Anchor' => 1,
'TextColor' => 1,
'BGColor' => 1,
'Blockquote' => 1,
'Cut' => 1,
'Copy' => 1,
'Paste' => 1,
'PasteText' => 1,
'PasteFromWord' => 1,
'RemoveFormat' => 1,
'Format' => 1,
'Font' => 1,
'FontSize' => 1,
'Styles' => 1,
'Table' => 1,
'CreateDiv' => 1,
),
'drupal' => array(
'media' => 1,
),
),
'toolbar_loc' => 'top',
'toolbar_align' => 'left',
'path_loc' => 'bottom',
'resizing' => 1,
'verify_html' => 1,
'preformatted' => 0,
'convert_fonts_to_spans' => 1,
'remove_linebreaks' => 1,
'apply_source_formatting' => 0,
'paste_auto_cleanup_on_paste' => 0,
'block_formats' => 'p,address,pre,h2,h3,h4,h5,h6,div',
'css_setting' => 'theme',
'css_path' => '',
'css_classes' => '',
),
);
$wysiwyg['settings'] = serialize($wysiwyg['settings']);
drupal_write_record('wysiwyg', $wysiwyg);
wysiwyg_profile_cache_clear();
}
}
/**
* Implements hook_ctools_plugin_directory().
*/
function ding_content_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'ctools' && $plugin_type == 'content_types') {
return 'plugins/content_types';
}
}
/**
* Implements hook_media_browser_plugin_info().
*/
function ding_content_media_browser_plugin_info() {
$plugins = array();
$plugins['ding_content'] = array(
'#weight' => 0,
);
return $plugins;
}
/**
* Implementaion of hook_media_browser_plugin_view().
*/
function ding_content_media_browser_plugin_view($plugin_name, $params) {
$media_path = drupal_get_path('module', 'media');
$path = drupal_get_path('module', 'ding_content');
$params += array(
'types' => array(),
'multiselect' => FALSE,
);
switch ($plugin_name) {
case 'ding_content':
return array(
'#title' => t('Personal library'),
'#attached' => array(
'js' => array(
$media_path . '/js/plugins/media.library.js',
$path . '/js/ding_content.media.js',
),
'css' => array(
//@todo: should move this.
$media_path . '/js/plugins/media.library.css',
$path . '/css/ding_content.media.css',
),
),
'#settings' => array(
'viewMode' => 'thumbnails',
'getMediaUrl' => url('ding_content/media'),
// We should probably change this to load dynamically when requested
// via the JS file.
) + $params,
'#markup' => '<div id="container"><div id="scrollbox"><ul id="media-browser-library-list" class="media-list-thumbnails"></ul><div id="status"></div></div></div>',
);
break;
}
return array();
}
/**
* Implements hook_field_widget_info().
*/
function ding_content_field_widget_info() {
return array(
'ding_content_group_audience_library' => array(
'label' => t('Library audience'),
'field types' => array('group'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_CUSTOM,
),
'settings' => array(
),
),
);
}
/**
* Implements hook_field_widget_form().
*
* Based on og_field_widget_form().
*/
function ding_content_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base) {
$element = $base;
$widget = $instance['widget'];
switch ($widget['type']) {
case 'ding_content_group_audience_library':
// Determine if a user may see other groups as-well.
$opt_group = FALSE;
if (user_access('administer group')) {
$opt_group = TRUE;
}
// Get all the groups divided to "content groups" and "other groups".
$audience = og_field_audience_options($opt_group);
// Get all groups that should be excluded.
$excludes = array();
// If it's an existing group, then exclude itself, as in some cases a group
// can act also as a group content, but we want to prevent associating the
// group to itself.
if (!empty($form['#' . $element['#entity_type']])) {
list($id) = entity_extract_ids($element['#entity_type'], $form['#' . $element['#entity_type']]);
if (($group = og_get_group($element['#entity_type'], $id))) {
$excludes[$group->gid] = $group->gid;
}
}
// Get default values from URL, or from the edited entity.
$default_values = og_get_context_by_url();
// Keep the group ID of the selected items, as they might be needed again,
// and we don't want to iterate over the items again.
$items_gid = array();
if (!empty($items)) {
foreach ($items as $item) {
// Some groups might be already deleted, but we don't bulk
// delete the fields of the entities associated with that group.
// So make sure the hidden selected fields are only for existing
// groups.
if (in_array($item['gid'], $audience['content groups']) || in_array($item['gid'], $audience['other groups'])) {
$default_values[$item['gid']] = $item['gid'];
$items_gid[] = $item['gid'];
}
}
}
foreach (array('content groups', 'other groups') as $key) {
if (!empty($audience[$key])) {
// Get the label un-sanitized, as they will be laster sanitized
// according to the form type.
$audience[$key] = og_label_multiple($audience[$key], FALSE);
}
}
// The group options presented to the user.
$options = array();
$hidden_selected_gids = array();
$type = 'select';
if ($opt_group) {
// Show "My groups" and "Other groups".
if ($my_groups = array_diff_key($audience['content groups'], $excludes)) {
$options += $my_groups;
}
if ($other_groups = array_diff_key($audience['other groups'], $excludes)) {
$options += $other_groups;
}
}
else {
// Show only "My groups".
$options = array_diff_key($audience['content groups'], $excludes);
// If there are items that are already selected but the user doesn't
// have access to them, we need to keep track of them.
$hidden_selected_gids = drupal_map_assoc(array_diff($items_gid, array_keys($options)));
}
if (empty($options)) {
// There are no group, so don't show any input element.
$type = 'item';
$element['#description'] = t('There are no libraries you can select from.');
$properties = array();
}
else {
if (empty($element['#description'])) {
$element['#description'] = t('Select the libraries this content should be associated with.');
}
$element['#multiple'] = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
// Don't make the field required, if there are no groups.
$element['#required'] = $element['#required'] && !empty($options);
$properties = _options_properties($type, $element['#multiple'], $element['#required'], $options);
// If the element isn't required and cardinality is more than 1, and there
// are some options, and a "none" option.
if (!$element['#required'] && !$element['#multiple']) {
// Use a dummy instance in order to use theme('options_none');
$dummy_instance['widget']['type'] = 'options_select';
$options = array('_none' => t('- All libraries -')) + $options;
}
}
$element += array(
// Input should be TRUE only if there are groups that can be selected.
'#input' => $type != 'item',
'#type' => $type,
'#options' => $options,
'#default_value' => $default_values,
'#attributes' => array('class' => array('group-audience')),
'#disabled' => empty($options),
// Re-use options widget element validation, to correctly transform
// submitted values from field => delta to delta => field.
// @see options_field_widget()
'#value_key' => 'gid',
'#element_validate' => $type != 'item' ? array('options_field_widget_validate') : array(),
'#properties' => $properties,
// Add OG specific context.
'#opt_group' => $opt_group,
'#audience' => $audience,
'#hidden_selected_gids' => $hidden_selected_gids,
);
$return = $element;
break;
}
return $return;
}
/**
* Implements hook_theme().
*/
function ding_content_theme($existing, $type, $theme, $path) {
$hooks = array();
$hooks['ding_content_category_table'] = array(
'render element' => 'element',
);
return $hooks;
}
/**
* Custom sort function, to sort weighted defaults of the lately saved form.
*
* @see theme_ding_content_category_table()
* @see ding_content_foldable_menu_admin()
*/
function _ding_content_category_sort($a, $b) {
if ($a['category_weight'] == $b['category_weight']) {
return 0;
}
return ($a['category_weight'] < $b['category_weight']) ? -1 : 1;
}
/**
* Implements hook_entity_info_alter().
*/
function ding_content_entity_info_alter(&$entity_info) {
$entity_info['node']['view modes'] += array(
'related_category_items' => array(
'label' => t('Category items'),
'custom settings' => TRUE,
),
);
}
/**
* Implements hook_preprocess_node().
*/
function ding_content_preprocess_node(&$vars) {
if ($vars['view_mode'] == 'related_category_items') {
$vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__related_category_items';
}
}
/**
* Retrieve the node id's attached to a certain category term.
*
* @param int $tid
* Term id.
* @param string $node_type
* Seek for nodes only of that type.
*
* @return array
* Array of node id's
*/
function ding_content_get_attached_nodes($tid, $node_type = NULL) {
$limit = variable_get('category_hits', 4);
$display = 'page';
switch ($node_type) {
case 'ding_news':
$display = 'page_1';
break;
case 'ding_event':
$display = 'page_2';
break;
}
$view = 'artesis_category_taxonomy_term';
$view_object = views_get_view($view);
$view_object->set_display($display);
$view_object->set_arguments(array($tid));
$view_object->set_items_per_page($limit);
$view_object->execute();
$response = $view_object->result;
$nids = array();
foreach ($response as $resp) {
$nids[] = $resp->nid;
}
return $nids;
}
/**
* Generate markup of nodes.
*
* @param array $nids
* Set of node id's to be rendered.
*
* @return string
* Markup of the nodes.
*/
function ding_content_attached_nodes_markup($nids = array()) {
$content = array();
$markup = '';
foreach ($nids as $nid) {
$node = node_load($nid);
$renderable = node_view($node, 'related_category_items');
$content[] = drupal_render($renderable);
}
if (!empty($content)) {
$markup = theme('item_list', array('items' => $content));
// node_load is statically cached.
// Also we pick the tid from first node. This doesn't matter,
// since all the nodes expected here have same category tid.
$node = node_load(reset($nids));
$term = taxonomy_term_load($node->field_category[LANGUAGE_NONE][0]['tid']);
$uri = entity_uri('taxonomy_term', $term);
$markup .= '<div class="more-link">' . l(t('More'), $uri['path']) . '</div>';
}
return $markup;
}