-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusersnap.module
119 lines (108 loc) · 4.32 KB
/
usersnap.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
<?php
/**
* @file
* Integrates the Usersnap Web Feedback Tool into Drupal.
*/
define('USERSNAP_VISIBILITY_NOTLISTED', 0);
define('USERSNAP_VISIBILITY_LISTED', 1);
/**
* Implements hook_menu().
*/
function usersnap_menu() {
$items = array();
$items['admin/config/services/usersnap'] = array(
'title' => 'Usersnap Settings',
'description' => 'Enter yout Usersnap API Key to use the Web Feedback Service',
'route_name' => 'usersnap.settings',
);
return $items;
}
function usersnap_page_attachments_alter(&$page) {
$config = \Drupal::config('usersnap.admin_settings');
// @see block_block_list_alter()
$visibility = $config->get('usersnap_visibility') ? $config->get('usersnap_visibility') : USERSNAP_VISIBILITY_NOTLISTED;
$paths = $config->get('usersnap_paths') ? $config->get('usersnap_paths') : '';
$paths = strtolower($paths);
$current_path = \Drupal::service('path.current')->getPath();
$path = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
$path = trim($path, '/');
$page_match = \Drupal::service('path.matcher')->matchPath($path, $paths);
if (isset($_GET['q']) && $path != $_GET['q']) {
$page_match = $page_match || \Drupal::service('path.matcher')->matchPath($_GET['q'], $paths);
}
$show_on_this_page = !($visibility xor $page_match);
if (\Drupal::currentUser()->hasPermission('usersnap') && $show_on_this_page) {
$usersnap_settings = usersnap_get_settings();
$page['#attached']['drupalSettings'] = $usersnap_settings;
$page['#attached']['library'][] = 'usersnap/usersnap';
}
}
/**
* Helper function to return usersnap widget to the current page.
*/
function usersnap_get_settings() {
$config = \Drupal::config('usersnap.admin_settings');
$apikey = $config->get('usersnap_apikey') ? $config->get('usersnap_apikey') : '';
$position = explode('_', $config->get('usersnap_position') ? $config->get('usersnap_position') : 'bottom_right');
$language = $config->get('usersnap_language') ? $config->get('usersnap_language') : 'en';
$button_text = $config->get('usersnap_button_text') ? $config->get('usersnap_button_text') : 'Feedback';
$show_cbox = $config->get('usersnap_show_comment_box') ? $config->get('usersnap_show_comment_box') : FALSE;
$cbox_placeholder = $config->get('usersnap_comment_box_placeholder') ? $config->get('usersnap_comment_box_placeholder') : '';
$cbox_required = $config->get('usersnap_comment_box_required') ? $config->get('usersnap_comment_box_required') : FALSE;
$show_email = $config->get('usersnap_show_email_field') ? $config->get('usersnap_show_email_field') : FALSE;
$email_placeholder = $config->get('usersnap_email_field_placeholder') ? $config->get('usersnap_email_field_placeholder') : '';
$email_required = $config->get('usersnap_email_field_required') ? $config->get('usersnap_email_field_required') : FALSE;
$settings_object = array(
'usersnap' => array(
'apikey' => $apikey,
'language' => $language,
'button' => $button_text,
'valign' => $position[0],
'halign' => $position[1],
'cbox' => (bool) $show_cbox,
'crequired' => (bool) $cbox_required,
'email' => (bool) $show_email,
'erequired' => (bool) $email_required,
),
);
// If a comment box placeholder text is configured, add it.
if (!empty($cbox_placeholder)) {
$settings_object['usersnap']['cplaceholder'] = $cbox_placeholder;
}
// If an email placeholder text is configured, add it.
if (!empty($email_placeholder)) {
$settings_object['usersnap']['eplaceholder'] = $email_placeholder;
}
return $settings_object;
}
/**
* Helper function to return Usersnap supported languages and language codes.
*
* @see https://usersnap.com/docs#language
*/
function usersnap_supported_languages() {
return array(
'en' => t('English'),
'de-informal' => t('German (informal)'),
'de-formal' => t('German (formal)'),
'fr' => t('French'),
'es' => t('Spanish'),
'pl' => t('Polish'),
'fa' => t('Farsi'),
'is' => t('Icelandic'),
'jp' => t('Japanese'),
'it' => t('Italian'),
'hu' => t('Hungarian'),
'ko' => t('Korean'),
'cz' => t('Czech'),
'da' => t('Danish'),
'no' => t('Norwegian'),
'sk' => t('Slovakian'),
'nl' => t('Dutch'),
'sv' => t('Swedish'),
'pt' => t('Portuguese'),
'fi' => t('Finnish'),
'ru' => t('Russian'),
'tr' => t('Turkish'),
);
}