You are here

function back_to_top_page_build in Back To Top 7

Implements hook_page_build().

Add javascript & css but first check if prevent on mobile

File

./back_to_top.module, line 44
Back To Top link using JQuery.

Code

function back_to_top_page_build() {
  $settings = array(
    'back_to_top_button_text' => check_plain(variable_get('back_to_top_button_text', 'Back to top')),
  );
  if (variable_get('back_to_top_prevent_on_mobile', TRUE) && is_mobile()) {
    return FALSE;
  }
  if (variable_get('back_to_top_prevent_on_non_mobile', FALSE) && !is_mobile()) {
    return FALSE;
  }
  if (variable_get('back_to_top_prevent_in_admin', TRUE) && is_adminpage()) {
    return FALSE;
  }
  if (variable_get('back_to_top_prevent_in_front', FALSE) && drupal_is_front_page()) {
    return FALSE;
  }
  $pages = drupal_strtolower(variable_get('back_to_top_pages', ''));
  $visibility = (int) variable_get('back_to_top_visibility', BACK_TO_TOP_VISIBILITY_NOTLISTED);
  $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
  $page_match = drupal_match_path($path, $pages);

  // Compare the lowercase internal and lowercase path alias (if any).
  $page_match = drupal_match_path($path, $pages);
  if ($path != $_GET['q']) {
    $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
  }
  $page_match = !($visibility xor $page_match);
  if (!$page_match) {
    return FALSE;
  }
  drupal_add_library('system', 'effects');
  $settings['#attached']['library'][] = array(
    'system',
    'ui',
  );
  drupal_add_js(drupal_get_path('module', 'back_to_top') . '/js/back_to_top.js', array(
    'group' => JS_DEFAULT,
    'every_page' => TRUE,
  ));
  drupal_add_js(array(
    'back_to_top' => array(
      'back_to_top_button_trigger' => variable_get('back_to_top_button_trigger', 100),
    ),
  ), 'setting');

  // Add stylesheet for image or text/css button.
  if (variable_get('back_to_top_button_type', 'image') == "text") {
    drupal_add_css(drupal_get_path('module', 'back_to_top') . '/css/back_to_top_text.css', array(
      'group' => CSS_DEFAULT,
      'every_page' => TRUE,
    ));
  }
  else {
    drupal_add_css(drupal_get_path('module', 'back_to_top') . '/css/back_to_top.css', array(
      'group' => CSS_DEFAULT,
      'every_page' => TRUE,
    ));
  }

  // Check variables and add placement.
  if (variable_get('back_to_top_button_place', '1') == 2) {
    $css = "#backtotop { left: 10px; }";
    drupal_add_css($css, 'inline');
  }
  if (variable_get('back_to_top_button_place', '1') == 3) {
    $css = "#backtotop { left: 50%; margin-left: -50px; }";
    drupal_add_css($css, 'inline');
  }
  if (variable_get('back_to_top_button_place', '1') == 4) {
    $css = "#backtotop { top: 10px; }";
    drupal_add_css($css, 'inline');
  }
  if (variable_get('back_to_top_button_place', '1') == 5) {
    $css = "#backtotop { top: 10px; left: 10px; }";
    drupal_add_css($css, 'inline');
  }
  if (variable_get('back_to_top_button_place', '1') == 6) {
    $css = "#backtotop { top: 10px; left: 50%; margin-left: -50px; }";
    drupal_add_css($css, 'inline');
  }
  if (variable_get('back_to_top_button_place', '1') == 7) {
    $css = "#backtotop { top: 50%; }";
    drupal_add_css($css, 'inline');
  }
  if (variable_get('back_to_top_button_place', '1') == 8) {
    $css = "#backtotop { top: 50%; left: 10px; }";
    drupal_add_css($css, 'inline');
  }
  if (variable_get('back_to_top_button_place', '1') == 9) {
    $css = "#backtotop { top: 50%; left: 50%; margin-left: -50px; }";
    drupal_add_css($css, 'inline');
  }

  // Check variables and add color from settings
  // this code could be done a bit nicer.
  if (variable_get('back_to_top_button_type', 'image') == "text" && variable_get('back_to_top_bg_color', '#F7F7F7') !== '#F7F7F7') {
    $setting = variable_get('back_to_top_bg_color', '#F7F7F7');
    $css = "#backtotop { background: " . $setting . "; }";
    drupal_add_css($css, 'inline');
  }
  if (variable_get('back_to_top_button_type', 'image') == "text" && variable_get('back_to_top_border_color', '#CCCCCC') !== '#CCCCCC') {
    $setting = variable_get('back_to_top_bg_color', '#CCCCCC');
    $css = "#backtotop { border-color: " . $setting . "; }";
    drupal_add_css($css, 'inline');
  }
  if (variable_get('back_to_top_button_type', 'image') == "text" && variable_get('back_to_top_hover_color', '#EEEEEE') !== '#EEEEEE') {
    $setting = variable_get('back_to_top_hover_color', '#EEEEEE');
    $css = "#backtotop:hover { background: " . $setting . "; border-color: " . $setting . "; }";
    drupal_add_css($css, 'inline');
  }
  if (variable_get('back_to_top_button_type', 'image') == "text" && variable_get('back_to_top_text_color', '#333333') !== '#333333') {
    $setting = variable_get('back_to_top_text_color', '#333333');
    $css = "#backtotop { color: " . $setting . "; }";
    drupal_add_css($css, 'inline');
  }

  // Add settings to js.
  drupal_add_js(array(
    'back_to_top' => $settings,
  ), 'setting');
}