-
Notifications
You must be signed in to change notification settings - Fork 1
/
hover_card.module
220 lines (192 loc) · 7.95 KB
/
hover_card.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
<?php
/**
* @file
* Provides Hover Card for all user links with "username" as their class.
*/
/**
* The minimum version of Hover Card required.
*/
define('HOVER_CARD_MIN_PLUGIN_VERSION', '1.0');
/**
* Implements hook_help().
*/
function hover_card_help($path, $arg) {
global $base_url;
switch ($path) {
case 'admin/help#hover_card':
$output = '';
$output .= '<p>' . t('Hovercard is a module which is based on <a href="@hovercard">hovercard</a> which is a free light weight jQuery plugin that enables you to display related information with the hovered label, link, or any html element of your choice. This module extends Drupal to provide Hovercard for the users of the website.', array('@hovercard' => 'http://designwithpc.com/Plugins/Hovercard')) . '</p>';
$output .= '<h6>' . t('Dependencies') . '</h6>';
$output .= '<ul>';
$output .= t('<li><a href="@libraries">Libraries API 2</a></li>', array('@libraries' => 'https://drupal.org/project/libraries'));
$output .= '</ul>';
$output .= '<h6>' . t('Installation') . '</h6>';
$output .= '<p>' . t('The Hover Card module is very similar to other Drupal modules which requires Libraries Module to use for the 3rd party code integration. Hence, for installation of the Hover Card module please follow the below mentioned steps:') . '</p>';
$output .= '<ol>';
$output .= t('<li>Install as usual, see <a href="@1294804">https://www.drupal.org/node/1294804</a> for further information.</li>', array('@1294804' => 'https://www.drupal.org/node/1294804'));
$output .= '<li>' . t('Download and install the Libraries Module - 2.x and after the module installation create a new folder called <code>libraries</code> under your <code>sites/all/</code> folder. (Creating this folder will help us to locate our 3rd party code integration.)') . '</li>';
$output .= '<li>' . t('Download the compressed version of jQuery <a href="@github">Hovercard Plugin</a> and extract the files into <code>sites/all/libraries/hover_card/</code>', array('@github' => 'https://github.com/prashantchaudhary/hovercard/archive/master.zip')) . '</li>';
$output .= '<li>' . t('Now, in your <code>sites/all/modules/sandbox/</code> directory download the Hover Card module using GIT clone code <code>git clone --branch 7.x-1.x http://git.drupal.org/sandbox/RishiKulshreshtha/2308117.git hover_card</code>.') . '</li>';
$output .= '<li>' . t('Enable the Hover Card module.') . '</li>';
$output .= '</ol>';
$output .= '<h6>' . t('Configuration') . '</h6>';
$output .= '<ul>';
$output .= '<li>' . t('After enabling it please check your <a href="@status">Status Report</a> where there should be a new option showing <code>Hover Card Plugin - v1.0 installed</code> with a success (green) status.', array('@status' => $base_url . '/admin/reports/status')) . '</p>';
$output .= '<li>' . t('This module has no menu or modifiable settings. There is no configuration. When enabled and configured properly, this module will display the hover card to the user links with <code>username</code> as class to their anchor tags. To disable the hover card from user links, disable the module and clear caches.') . '</p>';
$output .= '</ul>';
return $output;
}
}
/**
* Implements hook_libraries_info().
*/
function hover_card_libraries_info() {
$libraries['hover_card'] = array(
'name' => 'Hover Card Plugin',
'vendor url' => 'http://designwithpc.com/Plugins/Hovercard',
'download url' => 'https://github.com/prashantchaudhary/hovercard/archive/master.zip',
'version arguments' => array(
'file' => 'jquery.hovercard.js',
'pattern' => '@[\s\S]*(?<=Version )([0-9.]+)@',
),
'files' => array(
'js' => array(
'jquery.hovercard.min.js',
),
),
);
return $libraries;
}
/**
* Implements hook_menu().
*/
function hover_card_menu() {
$items['hover-card/%user'] = array(
'title' => 'Hover Card',
'page callback' => 'hover_card',
'page arguments' => array(1),
'access arguments' => array('access user profiles'),
'type' => MENU_CALLBACK,
'delivery callback' => 'hover_card_ajax_callback',
);
$items['admin/config/people/hover-card'] = array(
'title' => 'Hover Card Settings',
'description' => 'Control default behavior of Hover Card, whether to display the Email address of the users or not.',
'page callback' => 'drupal_get_form',
'page arguments' => array('hover_card_settings'),
'access arguments' => array('hover card config'),
'file' => 'hover_card.admin.inc',
'weight' => 0,
);
return $items;
}
/**
* Implements hook_permission().
*/
function hover_card_permission() {
// Set permission for managing module configurations.
return array(
'hover card config' => array(
'title' => t('Display Hover Card to Users'),
),
);
}
/**
* Returns HTML with hovered user details.
*
* @param array $user
* Array with parameters for this action: depends on the trigger.
*/
function hover_card($user = array()) {
$picture = array();
$name = $mail = $roles = "";
if (isset($user->name) && !empty($user->name)) {
$name = $user->name;
}
if (isset($user->mail) && !empty($user->mail) && \Drupal::config('hover_card.settings')->get('hover_card_user_email_display_status')) {
$mail = $user->mail;
}
if (isset($user->picture->uri) && !empty($user->picture->uri)) {
$picture_build = array(
'#theme' => 'image_style',
'#path' => $user->picture->uri,
'#style_name' => 'thumbnail',
);
$picture = drupal_render($picture_build);
}
foreach ($user->roles as $value) {
$roles = $value;
}
$user_data = array(
'name' => \Drupal\Component\Utility\SafeMarkup::checkPlain($name),
'mail' => \Drupal\Component\Utility\SafeMarkup::checkPlain($mail),
'picture' => $picture,
'roles' => \Drupal\Component\Utility\SafeMarkup::checkPlain($roles),
);
$hover_card_template_build = array(
'#theme' => 'hover_card_template',
'#details' => $user_data,
);
$hover_card_template = drupal_render($hover_card_template_build);
return $hover_card_template;
}
/**
* Page callback; Initiates when 'hover-card/%user' path is accessed.
*
* @param string $html
* Prints the values at the requested menu, such as hover-card/1.
*/
function hover_card_ajax_callback($html) {
// AJAX output.
print $html;
drupal_page_footer();
}
/**
* Implements template_preprocess_username().
*/
function hover_card_preprocess_username(&$variables) {
// Declaring Variables.
$hover_card = 'hover_card';
$module_path = drupal_get_path('module', $hover_card);
// Initializing the Hover Card Plugin from Libraries.
libraries_load($hover_card);
// Adding CSS.
// @FIXME
// The Assets API has totally changed. CSS, JavaScript, and libraries are now
// attached directly to render arrays using the #attached property.
//
//
// @see https://www.drupal.org/node/2169605
// @see https://www.drupal.org/node/2408597
// drupal_add_css($module_path . '/stylesheets/hover_card.css');
// Adding JS.
// @FIXME
// The Assets API has totally changed. CSS, JavaScript, and libraries are now
// attached directly to render arrays using the #attached property.
//
//
// @see https://www.drupal.org/node/2169605
// @see https://www.drupal.org/node/2408597
// drupal_add_js($module_path . '/javascripts/hover_card.js');
// @FIXME
// The Assets API has totally changed. CSS, JavaScript, and libraries are now
// attached directly to render arrays using the #attached property.
//
//
// @see https://www.drupal.org/node/2169605
// @see https://www.drupal.org/node/2408597
// drupal_add_js(array($hover_card => array('module_path' => $module_path)), array('type' => 'setting'));
}
/**
* Implements hook_theme().
*
* This will render some basic output for this module.
*/
function hover_card_theme($existing, $type, $theme, $path) {
return array(
'hover_card_template' => array(
'variables' => array('details' => NULL),
'template' => '/templates/hover-card',
),
);
}