function back_to_top_page_attachments in Back To Top 8
Same name and namespace in other branches
- 2.x back_to_top.module \back_to_top_page_attachments()
File
- ./
back_to_top.module, line 15 - Adds the back to top button.
Code
function back_to_top_page_attachments(array &$attachments) {
$config = \Drupal::config('back_to_top.settings');
$settings = $config
->get();
$button_settings = [
'back_to_top_prevent_on_mobile' => $settings['back_to_top_prevent_on_mobile'],
'back_to_top_prevent_in_admin' => $settings['back_to_top_prevent_in_admin'],
'back_to_top_button_type' => $settings['back_to_top_button_type'],
'back_to_top_button_text' => $settings['back_to_top_button_text'],
];
if ($settings['back_to_top_prevent_on_mobile'] && is_mobile()) {
return FALSE;
}
if ($settings['back_to_top_prevent_in_admin'] && is_adminpage()) {
return FALSE;
}
if ($settings['back_to_top_prevent_in_front'] && \Drupal::service('path.matcher')
->isFrontPage()) {
return FALSE;
}
$attachments['#attached']['library'][] = 'back_to_top/back_to_top_js';
$attachments['#attached']['drupalSettings']['back_to_top']['back_to_top_button_trigger'] = $settings['back_to_top_button_trigger'];
// Add stylesheet for image or text/css button.
if ($settings['back_to_top_button_type'] == "text") {
$attachments['#attached']['library'][] = 'back_to_top/back_to_top_text';
}
else {
$attachments['#attached']['library'][] = 'back_to_top/back_to_top_icon';
}
$css = '';
$hover_css = '';
// Check variables and add placement.
if ($settings['back_to_top_button_place'] == 2) {
$css .= "left: 10px; ";
}
if ($settings['back_to_top_button_place'] == 3) {
$css .= "left: 50%; margin-left: -50px;";
}
if ($settings['back_to_top_button_place'] == 4) {
$css .= "top: 10px;";
}
if ($settings['back_to_top_button_place'] == 5) {
$css .= "top: 10px; left: 10px;";
}
if ($settings['back_to_top_button_place'] == 6) {
$css .= "top: 10px; left: 50%; margin-left: -50px;";
}
if ($settings['back_to_top_button_place'] == 7) {
$css .= "top: 50%;";
}
if ($settings['back_to_top_button_place'] == 8) {
$css .= "top: 50%; left: 10px;";
}
if ($settings['back_to_top_button_place'] == 9) {
$css .= "top: 50%; left: 50%; margin-left: -50px;";
}
// Check variables and add color from settings - this code could be done a bit nicer.
if ($settings['back_to_top_button_type'] == "text" && $settings['back_to_top_bg_color'] !== '#F7F7F7') {
$css .= "background: " . $settings['back_to_top_bg_color'] . ";";
}
if ($settings['back_to_top_button_type'] == "text" && $settings['back_to_top_border_color'] !== '#CCCCCC') {
$css .= "border-color: " . $settings['back_to_top_border_color'] . ";";
}
if ($settings['back_to_top_button_type'] == "text" && $settings['back_to_top_hover_color'] !== '#EEEEEE') {
$hover_css .= "body #backtotop:hover { background: " . $settings['back_to_top_hover_color'] . "; border-color: " . $settings['back_to_top_hover_color'] . "; }";
}
if ($settings['back_to_top_button_type'] == "text" && $settings['back_to_top_text_color'] !== '#333333') {
$css .= "color: " . $settings['back_to_top_text_color'] . ";";
}
if ($css != '') {
$attachments['#attached']['html_head'][] = [
[
'#tag' => 'style',
'#value' => 'body #backtotop {' . $css . '}' . $hover_css,
],
'css',
];
}
// Add settings to js.
$attachments['#attached']['drupalSettings']['back_to_top'] += $button_settings;
}