site_banner.module in Site Banner 7
Same filename and directory in other branches
Main module file implementing callbacks for the site banner module.
File
site_banner.moduleView source
<?php
/**
* @file
* Main module file implementing callbacks for the site banner module.
*/
/**
* Implements hook_menu().
*/
function site_banner_menu() {
$items['admin/config/user-interface/site_banner'] = array(
'title' => 'Site banner',
'description' => 'Administer site banner settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'site_banner_admin_settings_form',
),
'access arguments' => array(
'manage site banner settings',
),
'file' => 'site_banner.admin.inc',
);
return $items;
}
/**
* Implements hook_html_head_alter().
*/
function site_banner_html_head_alter(&$head_elements) {
// Enable the site banner only if the site banner is
// setup globally or enabled by a context.
$banner_active = variable_get('site_banner_status', FALSE);
$debug_mode = site_banner_check_if_debug_mode_active();
if (module_exists('context')) {
$plugin = context_get_plugin('reaction', 'change_banner_status');
if ($plugin) {
$plugin
->execute($banner_active, $debug_mode);
}
}
if ($banner_active) {
drupal_add_css(drupal_get_path('module', 'site_banner') . '/site_banner_screen.css', array(
'media' => 'screen',
));
drupal_add_css(drupal_get_path('module', 'site_banner') . '/site_banner_print.css', array(
'media' => 'print',
));
}
}
/**
* Implements hook_page_build().
*/
function site_banner_page_build(&$page) {
$page = site_banner_build_banner($page);
}
/**
* Builds the site banner and implementing the context hooks.
*
* @param array $page
* the page object being formatted.
*
* @return array
* the page object formatted with the site banner.
*/
function site_banner_build_banner($page) {
$banner_active = variable_get('site_banner_status', FALSE);
$debug_mode = site_banner_check_if_debug_mode_active();
if (module_exists('context')) {
$plugin = context_get_plugin('reaction', 'change_banner_status');
if ($plugin) {
$plugin
->execute($banner_active, $debug_mode);
}
}
if ($banner_active) {
// ******************************************************
// Add link to banner CSS files and define banner colors.
// ******************************************************
$background_color_as_text = variable_get('site_banner_background_color', site_banner_get_default_background_color());
$text_color_as_text = variable_get('site_banner_text_color', site_banner_get_default_text_color());
// Execute context hooks to see if we need to alter the text or background
// colors. Will overwrite $background_color_as_text and $text_color_as_text
// variables.
if (module_exists('context')) {
$plugin = context_get_plugin('reaction', 'change_banner_text_color');
if ($plugin) {
$plugin
->execute($text_color_as_text);
}
$plugin = context_get_plugin('reaction', 'change_banner_background_color');
if ($plugin) {
$plugin
->execute($background_color_as_text);
}
}
$banner_css_colors_value = "#siteBannerFooterBanner, #siteBannerHeaderBanner {background-color:{$background_color_as_text}; color:{$text_color_as_text};}";
drupal_add_css(filter_xss_admin($banner_css_colors_value), array(
'group' => CSS_DEFAULT,
'type' => 'inline',
'media' => 'all',
));
// ************************
// Add banner text to page.
// ************************
$banner_text = variable_get('site_banner_text', site_banner_get_default_text());
if ($debug_mode) {
$banner_text = site_banner_generate_link_to_admin_page($banner_text);
}
// Execute context hooks to see if we need to alter the text or background
// colors. Will overwrite $background_color_as_text and $text_color_as_text
// variables.
if (module_exists('context')) {
$plugin = context_get_plugin('reaction', 'change_banner_text');
if ($plugin) {
$plugin
->execute($banner_text);
}
}
$top_banner_active = variable_get('site_banner_top_banner_active', TRUE);
$page_array = array();
if ($top_banner_active) {
$page_element = array(
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => array(
'id' => 'siteBannerHeaderBanner',
'class' => array(
'toolbar',
'clearfix',
'toolbar-processed',
'overlay-displace-top',
),
),
'#value' => filter_xss_admin($banner_text),
'#weight' => -100,
);
array_push($page_array, $page_element);
}
$bottom_banner_active = variable_get('site_banner_bottom_banner_active', TRUE);
if ($bottom_banner_active) {
$page_element = array(
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => array(
'id' => 'siteBannerFooterBanner',
'class' => array(
'toolbar',
'clearfix',
'toolbar-processed',
'overlay-displace-top',
),
),
'#value' => filter_xss_admin($banner_text),
'#weight' => -100,
);
array_push($page_array, $page_element);
}
// Define the site banner text in html body.
$page['page_top'] = $page_array;
}
return $page;
}
/**
* Implements hook_permission().
*/
function site_banner_permission() {
return array(
'manage site banner settings' => array(
'title' => t('Administer site banner settings.'),
'description' => t('Alter the text and color used in the site.'),
),
);
}
/**
* Implements hook_context_plugins().
*/
function site_banner_context_plugins() {
$plugins = array();
$plugins['site_banner_context_reaction_change_banner_status'] = array(
'handler' => array(
'file' => 'site_banner_context_reaction_functions.inc',
'class' => 'SiteBannerContextReactionChangeBannerStatus',
'parent' => 'context_reaction',
),
);
$plugins['site_banner_context_reaction_change_banner_text'] = array(
'handler' => array(
'file' => 'site_banner_context_reaction_functions.inc',
'class' => 'SiteBannerContextReactionChangeBannerText',
'parent' => 'context_reaction',
),
);
$plugins['site_banner_context_reaction_change_banner_text_color'] = array(
'handler' => array(
'file' => 'site_banner_context_reaction_functions.inc',
'class' => 'SiteBannerContextReactionChangeBannerTextColor',
'parent' => 'context_reaction',
),
);
$plugins['site_banner_context_reaction_change_banner_bg_color'] = array(
'handler' => array(
'file' => 'site_banner_context_reaction_functions.inc',
'class' => 'SiteBannerContextReactionChangeBannerBackgroundColor',
'parent' => 'context_reaction',
),
);
return $plugins;
}
/**
* Implements hook_context_registry().
*/
function site_banner_context_registry() {
return array(
'reactions' => array(
'change_banner_status' => array(
'title' => t('Change site banner status'),
'plugin' => 'site_banner_context_reaction_change_banner_status',
),
'change_banner_text' => array(
'title' => t('Change site banner text'),
'plugin' => 'site_banner_context_reaction_change_banner_text',
),
'change_banner_text_color' => array(
'title' => t('Change site banner text color'),
'plugin' => 'site_banner_context_reaction_change_banner_text_color',
),
'change_banner_background_color' => array(
'title' => t('Change site banner background color'),
'plugin' => 'site_banner_context_reaction_change_banner_bg_color',
),
),
);
}
/**
* Searches the form response to determine the background color to be saved.
*
* @param int $position_in_color_array
* the form_sate object containing user settings.
* @param string $custom_color_code
* a HTML hex color code denoting selected custom color.
* @param bool $is_custom_color
* whether a custom color is being used or not.
*
* @return string
* the background color as HTML hex color code.
*/
function site_banner_get_background_color_code($position_in_color_array, $custom_color_code, $is_custom_color = TRUE) {
$background_color_code = '#ffffff';
if ($is_custom_color) {
$background_color_code = $custom_color_code;
}
else {
$background_colors = array_keys(site_banner_get_background_colors());
$background_color_code = $background_colors[$position_in_color_array];
}
return $background_color_code;
}
/**
* Searches the form response to determine the text color to be saved.
*
* @param int $position_in_color_array
* the form_sate object containing user settings
* @param string $custom_color_code
* a HTML hex color code denoting selected custom color
* @param bool $is_custom_color
* whether a custom color is being used or not.
*
* @return string
* the background color as HTML hex color code
*/
function site_banner_get_text_color_code($position_in_color_array, $custom_color_code, $is_custom_color = TRUE) {
$text_color_code = '#000000';
if ($is_custom_color) {
$text_color_code = $custom_color_code;
}
else {
$text_colors = array_keys(site_banner_get_text_colors());
$text_color_code = $text_colors[$position_in_color_array];
}
return $text_color_code;
}
/**
* Validates banner text against existing values to ensure no inconsistencies.
*
* @param array $site_banner_array
* the current context array.
* @param array $new_banner_context
* the tested banner context.
* @param array $current_banner_text_index
* the current context index being analysed.
* @param array $new_banner_text_index
* the tested context index being analysed.
* @param array $context_name
* the context name.
* @param array $context_tag
* the context tag.
* @param array $form_section
* the form section to be tested.
*/
function site_banner_verify_banner_text($site_banner_array, $new_banner_context, $current_banner_text_index, $new_banner_text_index, $context_name, $context_tag, $form_section) {
$banner_text = $site_banner_array[$context_tag][$current_banner_text_index];
$new_banner_context = $new_banner_context[$new_banner_text_index];
if ($banner_text != $new_banner_context) {
$error_message = t("Inconsistent %form_section detected for context: %context_name. tag: %context_tag. Using first detected %form_section.", array(
'%form_selection' => $form_section,
'%context_name' => $context_name,
'%context_tag' => $context_tag,
'%form_section' => $form_section,
));
drupal_set_message($error_message, 'warning');
}
}
/**
* Returns new banner text by collapsing all active contexts into string.
*
* @param array $contexts
* the context array.
* @param string $banner_text
* the existing banner text
*
* @return string
* the updated banner text via $banner_text
*/
function site_banner_generate_context_banner_text_from_contexts($contexts, &$banner_text) {
$site_banner_array = array();
$banner_active = variable_get('site_banner_status', FALSE);
$debug_mode = site_banner_check_if_debug_mode_active();
if (module_exists('context')) {
$plugin = context_get_plugin('reaction', 'change_banner_status');
if ($plugin) {
$plugin
->execute($banner_active, $debug_mode);
}
}
foreach ($contexts as $context) {
if (!empty($context->reactions['change_banner_text'])) {
$new_banner_context = $context->reactions['change_banner_text'];
if (!empty($new_banner_context)) {
$context_tag = $context->tag;
if (!array_key_exists($context_tag, $site_banner_array)) {
// Tag does not exist - populate with initial values.
$site_banner_array[$context_tag]['prepend_text'] = $new_banner_context['site_banner_tag_prepend_text'];
$site_banner_array[$context_tag]['delimiter_text'] = $new_banner_context['site_banner_tag_delimiter_text'];
$site_banner_array[$context_tag]['append_text'] = $new_banner_context['site_banner_tag_append_text'];
$site_banner_array[$context_tag]['banner_text'] = array();
}
else {
site_banner_verify_banner_text($site_banner_array, $new_banner_context, 'prepend_text', 'site_banner_tag_prepend_text', $context->name, $context_tag, 'prepended banner text');
site_banner_verify_banner_text($site_banner_array, $new_banner_context, 'delimiter_text', 'site_banner_tag_delimiter_text', $context->name, $context_tag, 'delimiter banner text');
site_banner_verify_banner_text($site_banner_array, $new_banner_context, 'append_text', 'site_banner_tag_append_text', $context->name, $context_tag, 'appended banner text');
}
array_push($site_banner_array[$context_tag]['banner_text'], $new_banner_context['site_banner_text']);
}
}
}
if (!empty($site_banner_array)) {
$outer_first = TRUE;
$new_site_banner = '';
foreach ($site_banner_array as $site_banner_entry) {
if ($outer_first) {
$outer_first = FALSE;
}
else {
// As not the first time printing a banner entry,
// add a blank space to separate successive context entries.
$new_site_banner .= ' ';
}
$new_site_banner .= $site_banner_entry['prepend_text'];
$inner_first = TRUE;
foreach ($site_banner_entry['banner_text'] as $site_banner_text_entries) {
if ($inner_first) {
$inner_first = FALSE;
}
else {
// As not the first time printing a delimited banner entry,
// add a delimiter to separate successive context entries.
$new_site_banner .= $site_banner_entry['delimiter_text'];
}
$new_site_banner .= $site_banner_text_entries;
}
$new_site_banner .= $site_banner_entry['append_text'];
}
$banner_text = $new_site_banner;
}
if (!empty($context) && $debug_mode) {
$banner_text = site_banner_generate_link_to_admin_page($banner_text);
$banner_text .= ' (Active contexts: ';
foreach ($contexts as $context) {
$banner_text .= l($context->name, 'admin/structure/context/list/' . $context->name);
if (!($context === end($contexts))) {
$banner_text .= ', ';
}
}
$banner_text .= ')';
}
}
/**
* Determines if the debug mode by user permissions and debug mode status.
*
* @return bool
* whether debug mode is active or not.
*/
function site_banner_check_if_debug_mode_active() {
return user_access('manage site banner settings') && variable_get('site_banner_debug_status', FALSE);
}
/**
* Formats a link to the administration page.
*
* @return string
* a formatted string link to the Site Banner admin page.
*/
function site_banner_generate_link_to_admin_page($text) {
return l($text, 'admin/config/user-interface/site_banner');
}
/**
* A function checking for valid color codes.
*
* @return string
* regex expression describing a valid html color code.
*/
function site_banner_get_html_color_regex_pattern() {
return '/#([a-f]|[A-F]|[0-9]){6}\\b/';
}
/**
* Determines default site banner text.
*
* @return string
* default banner text.
*/
function site_banner_get_default_text() {
return '';
}
/**
* Determines default site banner background color.
*
* @return string
* default banner background color code.
*/
function site_banner_get_default_background_color() {
return '#ffff00';
}
/**
* Determines default site banner text color.
*
* @return string
* default banner text color code.
*/
function site_banner_get_default_text_color() {
return '#000000';
}
Functions
Name | Description |
---|---|
site_banner_build_banner | Builds the site banner and implementing the context hooks. |
site_banner_check_if_debug_mode_active | Determines if the debug mode by user permissions and debug mode status. |
site_banner_context_plugins | Implements hook_context_plugins(). |
site_banner_context_registry | Implements hook_context_registry(). |
site_banner_generate_context_banner_text_from_contexts | Returns new banner text by collapsing all active contexts into string. |
site_banner_generate_link_to_admin_page | Formats a link to the administration page. |
site_banner_get_background_color_code | Searches the form response to determine the background color to be saved. |
site_banner_get_default_background_color | Determines default site banner background color. |
site_banner_get_default_text | Determines default site banner text. |
site_banner_get_default_text_color | Determines default site banner text color. |
site_banner_get_html_color_regex_pattern | A function checking for valid color codes. |
site_banner_get_text_color_code | Searches the form response to determine the text color to be saved. |
site_banner_html_head_alter | Implements hook_html_head_alter(). |
site_banner_menu | Implements hook_menu(). |
site_banner_page_build | Implements hook_page_build(). |
site_banner_permission | Implements hook_permission(). |
site_banner_verify_banner_text | Validates banner text against existing values to ensure no inconsistencies. |