You are here

add_to_head.module in Add To Head 7

Same filename and directory in other branches
  1. 8 add_to_head.module
  2. 6 add_to_head.module

Add To Head allows abritrary insertion of code into the head of the page based on path selection.

File

add_to_head.module
View source
<?php

/**
 * @file
 * Add To Head allows abritrary insertion of code into the head of the page
 * based on path selection.
 */

/**
 * Implements hook_menu().
 */
function add_to_head_menu() {
  $items = array();
  $items['admin/config/development/add-to-head'] = array(
    'title' => 'Add To Head',
    'description' => 'Configure <em>Add To Head</em>.',
    'page callback' => 'add_to_head_overview',
    'file' => 'add_to_head.admin.inc',
    'access arguments' => array(
      'administer add to head',
    ),
  );
  $items['admin/config/development/add-to-head/add'] = array(
    'title' => 'Add New Profile',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'add_to_head_add_profile',
    ),
    'file' => 'add_to_head.admin.inc',
    'access arguments' => array(
      'administer add to head',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/development/add-to-head/%add_to_head_profile'] = array(
    'title' => 'Edit Profile',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'add_to_head_edit_profile',
      4,
    ),
    'file' => 'add_to_head.admin.inc',
    'access arguments' => array(
      'administer add to head',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/development/add-to-head/%add_to_head_profile/delete'] = array(
    'title' => 'Delete Profile',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'add_to_head_delete_profile_confirm',
      4,
    ),
    'file' => 'add_to_head.admin.inc',
    'access arguments' => array(
      'administer add to head',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_forms().
 */
function add_to_head_forms($form_id, $arg) {
  return array(
    'add_to_head_add_profile' => array(
      'callback' => 'add_to_head_edit_profile',
    ),
  );
}

/**
 * Implements hook_permission().
 */
function add_to_head_permission() {
  return array(
    'administer add to head' => array(
      'title' => t('Administer add to head'),
      'description' => t('Insert code into the head of the page based on path selection.'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Argument load handler for %add_to_head_profile URL placeholders
 */
function add_to_head_profile_load($arg) {
  $settings = variable_get('add_to_head_profiles', array());
  return isset($settings[$arg]) ? $settings[$arg] : FALSE;
}

/**
 * Implements hook_process_html().
 *
 * This is used to inject any code onto the appropriate pages.
 */
function add_to_head_process_html(&$vars) {
  $output = array();

  // Fetch the profile information stored in the DB.
  $settings = variable_get('add_to_head_profiles', array());

  // Allow other modules to alter profile settings. Additional profiles may be added here.
  drupal_alter(basename(__FILE__, '.module') . '_profiles', $settings);

  // If applicable, append each profile's code to the output.
  foreach ($settings as $profile) {

    // Determine if the code should be visible on the current page.
    $path_visibility = isset($profile['path_visibility']) ? $profile['path_visibility'] : 0;
    $path_list = isset($profile['paths']) ? $profile['paths'] : '';
    $page_match = add_to_head_match_page($path_visibility, $path_list);

    // Determine if the code should be visible given the current user's roles.
    $role_visibility = isset($profile['roles']) && isset($profile['roles']['visibility']) ? $profile['roles']['visibility'] : 0;
    $role_list = isset($profile['roles']) && isset($profile['roles']['list']) ? $profile['roles']['list'] : array();
    $role_match = add_to_head_match_role($role_visibility, $role_list);

    // Add this profile's code if all conditions are met.
    if ($page_match && $role_match) {
      $output[] = array(
        '#markup' => $profile['code'],
        '#suffix' => "\n",
      );
    }
  }

  // If we have any output from the profiles, render it.
  if (!empty($output)) {
    $vars[$profile['scope']] .= drupal_render($output);
  }
}

/**
 * Determines if code should be displayed on a particular page.
 *
 * Originally from block_list().
 *
 * @param boolean $visibility
 *   TRUE if visible only on selected pages or FALSE if visible on all except listed.
 * @param array $paths
 *   The list of pages to match.
 *
 * @return boolean
 *   TRUE if the code should be displayed on the page; FALSE otherwise.
 */
function add_to_head_match_page($visibility, $paths) {

  // Get the current path.
  $path = drupal_get_path_alias($_GET['q']);

  // Compare with the internal and path alias (if any).
  $page_match = drupal_match_path($path, $paths);
  if ($path != $_GET['q']) {
    $page_match = $page_match || drupal_match_path($_GET['q'], $paths);
  }

  // When $visibility has a value of 0, the code is displayed on
  // all pages except those listed in $paths. When set to 1, it
  // is displayed only on those pages listed in $paths.
  return !($visibility xor $page_match);
}

/**
 * Determines if code should be displayed for a particular role.
 *
 * Originally from _googleanalytics_visibility_roles().
 *
 * @param boolean $visibility
 *   TRUE if visible only for selected roles or FALSE if visible for all roles except listed.
 * @param array $roles
 *   The list of roles to match.
 *
 * @return boolean
 *   TRUE if the code should be displayed on the page; FALSE otherwise.
 */
function add_to_head_match_role($visibility, $roles) {

  // Assume the default visibility until we find one of the current roles on the list.
  $enabled = $visibility;
  if (array_sum($roles) > 0) {

    // One or more roles are selected.
    foreach (array_keys($GLOBALS['user']->roles) as $rid) {

      // Is the current user a member of one of these roles?
      if (isset($roles[$rid]) && $rid == $roles[$rid]) {

        // Current user is a member of a role that should be included or excluded from display.
        $enabled = !$visibility;
        break;
      }
    }
  }
  else {

    // No role is selected. Therefore all roles should be enabled.
    $enabled = TRUE;
  }
  return $enabled;
}

Functions

Namesort descending Description
add_to_head_forms Implements hook_forms().
add_to_head_match_page Determines if code should be displayed on a particular page.
add_to_head_match_role Determines if code should be displayed for a particular role.
add_to_head_menu Implements hook_menu().
add_to_head_permission Implements hook_permission().
add_to_head_process_html Implements hook_process_html().
add_to_head_profile_load Argument load handler for %add_to_head_profile URL placeholders