forked from CherepanovaVS/follow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
follow.module
257 lines (234 loc) · 7.07 KB
/
follow.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
<?php
/**
* @file
* Allows users to add links to their social network profiles.
*
* Contains Drupal\follow\follow.module.
*/
use Drupal\follow\FollowLink;
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_help().
*/
function follow_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'follow.user':
case 'follow.admin':
return t('Please copy and paste the url for your public profile or page for each service you would like to display in the block. Links need to match the domain of the service in question. If you want to hide the title of the links, enter "<none>" in the customized name.');
}
}
/**
* Implements hook_theme().
*/
function follow_theme() {
$items = array();
$items['follow_links'] = array(
'variables' => array(
'links' => array(),
'networks' => array(),
'alignment' => NULL,
// The type of the links, 'user' or 'site'.
'type' => NULL,
),
);
$items['follow_link'] = array(
'variables' => array('link' => NULL, 'title' => NULL),
);
$items['follow_css'] = array(
'variables' => array(
'icon_style_name' => NULL,
'icon_style' => array(),
'icon_path' => NULL,
'selector_prefix' => '',
'css_overrides' => '',
),
'template' => 'follow-css',
);
return $items;
}
/**
* Implements hook_preprocess_page().
*/
function follow_preprocess_page(&$variables) {
// Expose the site follow links as a variable to the page template.
if ($links = FollowLink::load()) {
$args = array(
'links' => $links,
'networks' => follow_networks_load(),
'alignment' => \Drupal::config('follow.settings')->get('follow_site_alignment'),
'type' => 'site',
);
$variables['follow_site'] = array(
'#theme' => 'follow_links',
'#links' => $args['links'],
'#networks' => $args['networks'],
'#alignment' => $args['alignment'],
'#type' => $args['type'],
);
}
}
/**
* Implements hook_preprocess_follow_css().
*/
function template_preprocess_follow_css(&$variables) {
// Allow for template overrides based on style name.
$variables['theme_hook_suggestions'][] = 'follow-css__' . $variables['icon_style_name'];
// Load up the style array.
if ($style = follow_get_icon_style($variables['icon_style_name'])) {
$variables['icon_style'] = $style;
}
// Normalize the selector prefix to have a single space at the end.
if (!empty($variables['selector_prefix'])) {
$variables['selector_prefix'] = rtrim($variables['selector_prefix']) . ' ';
}
// Set up the icon path.
if (empty($variables['icon_path'])) {
$variables['icon_path'] = _follow_style_icon_path($style);
}
// Convert the CSS overrides into a string.
if (empty($variables['css_overrides']) && !empty($style['css-overrides'])) {
$css_overrides = $variables['selector_prefix'] . "a.follow-link {\n";
foreach ($style['css-overrides'] as $line) {
$css_overrides .= " $line\n";
}
$variables['css_overrides'] = $css_overrides . "}\n";
}
}
/**
* Helper function to build the block title.
*
* @param $uid
* The uid of the user account. Defaults to the site form, $uid = 0.
*/
function _follow_block_subject($uid = 0) {
return follow_link_title($uid) .':';
}
/**
* Helper function to build the block content display.
*
* @param string $alignment
* The alignment of the icons, vertical or horizontal.
* @param string $style
* The style of the icons, small, large, etc.
* @param $uid
* The uid of the user account. Defaults to the site form, $uid = 0.
* @return array
* A renderable array of follow links.
*/
function _follow_block_content($type, $uid = 0) {
$element = array();
if ($links = FollowLink::load($uid)) {
$element = _follow_links_element($links,
follow_networks_load($uid),
$type
);
}
return $element;
}
/**
* Theme function to output a list of links.
*
* @param $links
* An array of follow link objects.
* @param $networks
* An array of network names, keys are machine names, values are visible titles.
* @param $alignment
* A string depicting whether the icons should be displayed in a "horizontal"
* or "vertical" list.
*
* @ingroup themeable
*/
function theme_follow_links($variables) {
$element = _follow_links_element($variables['links'],
$variables['networks'],
$variables['type'],
$variables['alignment']
);
return render($element);
}
/**
* Theme function to print an individual link.
*
* @param $link
* A follow link object.
* @param $title
* The translated title of the social network.
*
* @ingroup themable
*/
function template_preprocess_follow_link(&$variables) {
$link = $variables['link'];
$title = $variables['title'];
$classes = array();
$classes[] = 'follow-link';
$classes[] = "follow-link-{$link->name}";
$classes[] = $link->uid ? 'follow-link-user' : 'follow-link-site';
$attributes = array(
'class' => $classes,
'title' => follow_link_title($link->uid) .' '. $title,
);
// Clean up the blank titles.
if ($title == '<none>') {
$title = '';
}
$link->options['attributes'] = $attributes;
if ($link->lid == 1) {
// $url = Url::fromRoute('rss.xml', $link->options);
// $variables['link'] = \Drupal::l($title, $url);
$variables['link'] = '';
}
else {
$url = Url::fromUri($link->path, $link->options);
$variables['link'] = \Drupal::l($title, $url, $link->options);
}
}
/**
* Implements hook_contextual_links_view_alter().
*
* @param $element
* A renderable array representing the contextual links.
* @param $items
* An associative array containing the original contextual link items, as
* generated by menu_contextual_links(), which were used to build
* $element['#links'].
*/
function follow_contextual_links_view_alter(&$element, $items) {
// Add the Follow configuration links to both of the Follow blocks.
$block = isset($element['#element']['#block']) ? $element['#element']['#block'] : NULL;
if (is_object($block) && $block->module == 'follow') {
$uid = arg(1);
if ($block->delta == 'site' && \Drupal::currentUser()->hasPermission('edit site follow links')) {
$element['#links']['follow-edit'] = array(
'title' => t('Edit Follow links'),
'href' => 'admin/config/services/follow',
'query' => drupal_get_destination(),
'attributes' => array(
'title' => t('Configure the site-wide web service follow links.'),
),
);
}
elseif ($block->delta == 'user' && follow_links_user_access($uid)) {
$element['#links']['follow-edit'] = array(
'title' => t('Edit Follow links'),
'href' => 'user/' . $uid . '/follow',
'query' => drupal_get_destination(),
'attributes' => array(
'title' => t('Update the associated web service follow links.'),
),
);
}
}
}
/**
* Implements hook_admin_paths().
*
* Have the Follow links for the user pop up in the Overlay.
*/
function follow_admin_paths() {
$paths = array(
'user/*/follow' => TRUE,
);
return $paths;
}