-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathartesis_frontend.module
368 lines (326 loc) · 11.8 KB
/
artesis_frontend.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
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
<?php
/**
* @file
* Code for the Artesis Frontend feature.
*/
define('IPE_RESTRICTED_ROLE', 'editor');
include_once('artesis_frontend.features.inc');
/**
* Implements hook_views_default_views_alter().
*
* Alter system taxonomy term view to ensure its path doesn't conflict
* with panel page path.
*/
function artesis_frontend_views_default_views_alter(&$views) {
if (isset($views['taxonomy_term'])) {
$views['taxonomy_term']->display['page']->display_options['path'] = 'taxonomy/term/%/views';
}
}
/**
* Helper function to get a list of roles that could be assigned to an user
*/
function artesis_user_roles($membersonly = FALSE, $permission = NULL, $account = NULL) {
$query = db_select('role', 'r');
$query->addTag('translatable');
$query->fields('r', array('rid', 'name'));
$query->orderBy('weight');
$query->orderBy('name');
if (!empty($account)) {
$roles = $account->roles;
$authenticated = user_role_load(DRUPAL_AUTHENTICATED_RID);
unset($roles[DRUPAL_AUTHENTICATED_RID]);
$max_weight = $authenticated->weight;
foreach ($roles as $rid => $role_name) {
$role = user_role_load($rid);
if ($role->weight > $max_weight) {
$max_weight = $role->weight;
}
}
$query->condition('weight', $max_weight, '<=');
}
if (!empty($permission)) {
$query->innerJoin('role_permission', 'p', 'r.rid = p.rid');
$query->condition('p.permission', $permission);
}
$result = $query->execute();
$roles = array();
foreach ($result as $role) {
switch ($role->rid) {
// We only translate the built in role names
case DRUPAL_ANONYMOUS_RID:
if (!$membersonly) {
$roles[$role->rid] = t($role->name);
}
break;
case DRUPAL_AUTHENTICATED_RID:
$roles[$role->rid] = t($role->name);
break;
default:
$roles[$role->rid] = $role->name;
}
}
return $roles;
}
/**
* Permit users to create only roles that do not exceed theirs
*/
function artesis_frontend_form_alter(&$form, &$forms_state, $form_id) {
global $_domain;
if ($form_id == 'user_profile_form' || $form_id == 'user_register_form') {
$form['account']['roles']['#access'] = user_access('change user role');
if (user_access('change user role')) {
global $user;
$account = $form['#user'];
$roles = array_map('check_plain', artesis_user_roles(TRUE, NULL, $user));
// The disabled checkbox subelement for the 'authenticated user' role
// must be generated separately and added to the checkboxes element,
// because of a limitation in Form API not supporting a single disabled
// checkbox within a set of checkboxes.
// @todo This should be solved more elegantly. See issue #119038.
$checkbox_authenticated = array(
'#type' => 'checkbox',
'#title' => $roles[DRUPAL_AUTHENTICATED_RID],
'#default_value' => TRUE,
'#disabled' => TRUE,
);
unset($roles[DRUPAL_AUTHENTICATED_RID]);
$form['account']['roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Roles'),
'#default_value' => (isset($account->roles) ? array_keys($account->roles) : array()),
'#options' => $roles,
DRUPAL_AUTHENTICATED_RID => $checkbox_authenticated,
);
}
}
elseif (module_exists('ding_wayf') && ($form_id == 'user_login_block' || $form_id == 'user_login') && $_domain['machine_name'] == 'artesis') {
$block = module_invoke('ding_wayf', 'block_view');
$form['actions']['wayf'] = array(
'#markup' => render($block->content),
);
}
}
/**
* Implement permissions for users to change only roles, not permissions
*/
function artesis_frontend_permission() {
return array(
'change user role' => array(
'title' => t('Change user role'),
'description' => t('Allow a user to change only the roles of an user and not the permissions defined by that role.'),
),
);
}
/**
* Returns a list of plugins that can be inserted
* on a IPE page by user with IPE_RESTRICTED_ROLE role.
*/
function _artesis_frontend_get_allowed_plugins() {
// Plugin definition follows ctools convention 'module - plugin name'.
// Views must be declared by 'views' value.
// Blocks must be declared by 'block' value.
return array(
'ding_nodelist' => 'ding_nodelist',
'ding_item_list' => 'ding_item_list',
'campaign' => 'campaign',
'term_description' => 'term_description',
'ding_item_viewer-item_viewer' => 'block',
'carousel' => 'carousel',
'ding_library_opening_hours-ding_library_opened_today' => 'block',
);
}
/**
* Implements hook_theme_registry_alter().
* Replaces original IPE "Add new pane" button theming function with a custom set
* of links available for site editors.
*/
function artesis_frontend_theme_registry_alter(&$registry) {
$registry['panels_ipe_add_pane_button']['function'] = '_artesis_frontend_panels_ipe_add_pane_button';
}
/**
* Custom themeing function that replaces standard "Add new pane" button
* generated by panels IPE. The code is copied from original theme function
* defined in IPE sub-module of panels module and slightly modified.
*/
function _artesis_frontend_panels_ipe_add_pane_button($vars) {
$assets_loaded = &drupal_static('ipe_assets_loaded', FALSE);
$region_id = $vars['region_id'];
$renderer = $vars['renderer'];
$menu_width = 150;
$attributes = array(
'class' => 'ctools-use-modal',
'style' => 'width: ' . $menu_width . 'px; height: auto',
);
$plugins = _artesis_frontend_get_allowed_plugins();
$blocks = array_keys($plugins, 'block');
$views = array_keys($plugins, 'views');
$buttons = array();
// Handle plugins first.
foreach ($plugins as $module => $plugin) {
if (!in_array($module, $blocks) && !in_array($module, $views)) {
$plugin_info = ctools_get_content_type($plugin);
$buttons[] = l($plugin_info['title'], $renderer->get_url('add-pane', $region_id, $module, $plugin), array('attributes' => $attributes));
}
}
// Handle views.
foreach ($views as $view) {
$view_obj = views_get_view($view);
$buttons[] = l($view_obj->human_name, $renderer->get_url('add-pane', $region_id, 'views', $view), array('attributes' => $attributes));
}
// Handle blocks.
foreach ($blocks as $block) {
list($module, $delta) = explode('-', $block);
$block_info = module_invoke($module, 'block_info');
$buttons[] = l($block_info[$delta]['info'], $renderer->get_url('add-pane', $region_id, 'block', $block), array('attributes' => $attributes));
}
$buttons = '<div id="ipe-add-' . $region_id . '" class="panels-ipe-newblock ipe-popup">' . theme('item_list', array('items' => $buttons)) . '</div>';
if (!$assets_loaded) {
$mod_path = drupal_get_path('module', 'artesis_frontend');
drupal_add_js($mod_path . '/js/ipe.js', 'file');
drupal_add_css($mod_path . '/css/ipe.css', 'file');
$assets_loaded = TRUE;
}
return '<div class="panels-ipe-newblock panels-ipe-on">' . l(t('Add'), NULL, array('attributes' => array('class' => 'ipe-trigger', 'target_region' => $region_id))) . '</div>' . $buttons;
}
/**
* Store all IPE panes that are not of allowed type
* for further protecting from deletion.
*/
function artesis_frontend_preprocess_panels_ipe_pane_wrapper($vars) {
$pane = $vars['pane'];
$protected_panes = &drupal_static('artesis_frontend_ipe', array());
$allowed_panes = _artesis_frontend_get_allowed_plugins();
if (!(in_array($pane->type, array_values($allowed_panes)) && in_array($pane->subtype, array_keys($allowed_panes)))) {
$protected_panes[] = $pane->pid;
}
}
/**
* Implements hook_preprocess_page().
* Removes delete button on non-nodelist panes for
* certain use role (IPE_RESTRICTED_ROLE).
*/
function artesis_frontend_preprocess_page($page) {
global $user;
if (in_array(IPE_RESTRICTED_ROLE, $user->roles)) {
$protected_panes = drupal_static('artesis_frontend_ipe', array());
$panes_selector = array();
foreach ($protected_panes as $pane_id) {
$panes_selector[] = 'pane-delete-panel-pane-' . $pane_id;
}
if (count($panes_selector)) {
drupal_add_js('(function($) { $(document).ready(function() { $("#' . join(', #', $panes_selector) . '").parent().remove(); }) })(jQuery);', 'inline');
}
}
drupal_add_js(drupal_get_path('module', 'artesis_frontend') . '/js/artesis_frontend.js');
}
/**
* Implements hook_entity_view_alter().
*
* Remove History section from user profile.
*/
function artesis_frontend_entity_view_alter(&$build, $type) {
if ($type == 'user') {
unset($build['summary']);
}
}
/**
* Implements hook node_content_view().
*
* Strip images in teaser view mode for ding_* content types.
*/
function node_content_view($node, $view_mode, $langcode) {
if (in_array($node->type, array('ding_page', 'ding_news', 'ding_event')) && $view_mode == 'teaser') {
$lng = isset($node->field_ding_body[$langcode]) ? $langcode : LANGUAGE_NONE;
// Cannot find the field, so do nothing.
if (empty($node->field_ding_body[$lng])) {
watchdog('artesis_frontend', "Field field_ding_body for language '$lng' not set.", WATCHDOG_DEBUG);
return $node;
}
$node->field_ding_body[$lng][0]['safe_value'] = preg_replace('/<img[^>]+>/i', '', $node->field_ding_body[$lng][0]['safe_value']);
// Remove empty p's since they prevent from displaying actual content.
$node->field_ding_body[$lng][0]['safe_value'] = str_replace('<p></p>', '', $node->field_ding_body[$lng][0]['safe_value']);
}
return $node;
}
/**
* Implements hook_preprocess_views_view_fields().
*
* Do not show address field if the address is not fully filled.
*/
function artesis_frontend_preprocess_views_view_fields(&$variables) {
$view = $variables['view'];
// panel_pane_3 stands for "Left column data" ding event view display.
if ($view->current_display == 'panel_pane_3') {
// Required field not found, do nothing.
if (!isset($variables['fields']['field_address'])) {
return;
}
$field_address = $variables['fields']['field_address'];
$field_content = $field_address->content;
if (strpos($field_content, 'street-block') === FALSE || strpos($field_content, 'locality-block') === FALSE) {
unset($variables['fields']['field_address']);
}
}
}
/**
* Implements hook_block_info().
*/
function artesis_frontend_block_info() {
$blocks = array();
// RSS Icon block.
$blocks['rss_icon'] = array(
'info' => t('RSS Icon'),
'cache' => DRUPAL_CACHE_PER_PAGE,
'status' => 1,
'region' => 'content',
'visibility' => BLOCK_VISIBILITY_NOTLISTED,
'pages' => '',
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function artesis_frontend_block_view($delta = '') {
$block = array();
switch ($delta) {
// RSS Icon block.
case 'rss_icon':
$url = NULL;
$title = NULL;
// Check if we have RSS link in html head.
$headers = drupal_add_html_head();
foreach ($headers as $k => $v) {
if ($v['#tag'] == 'link' && $v['#attributes']['rel'] == 'alternate' && $v['#attributes']['type'] == 'application/rss+xml') {
$title = $v['#attributes']['title'];
$url = $v['#attributes']['href'];
break;
}
}
// Show the block in case the rss for this page is given.
if (!empty($url)) {
$text = t('Subscribe to !feed-title', array('!feed-title' => $title));
$image = theme('image', array(
'path' => 'misc/feed.png',
'width' => 16,
'height' => 16,
'alt' => $text,
));
$block['subject'] = NULL;
$block['content'] = array(
'#theme' => 'link',
'#text' => $image,
'#options'=> array(
'html' => TRUE,
'attributes' => array(
'class' => array('rss-icon'),
'title' => $text,
),
),
'#path' => $url,
);
}
break;
}
return $block;
}