Last updated February 18, 2010. Created by simanta on February 18, 2010.
Edited by LeeHunter. Log in to edit this page.
[Copied from http://drupal.org/node/291039#comment-2614440 from user simanta]
The below given source code will give one flexibility to set custom favicons based on paths. Hope this helps someone. Please review and comment.
File -> favicon.module
=================
<?php
// $Id: favicon.module $
/**
* Implementation of hook_perm
*/
function favicon_perm() {
return array('administer favicons');
}
/**
* Implementation of hook_menu().
*/
function favicon_menu() {
$items['admin/build/favicon'] = array(
'title' => 'Favicons',
'description' => 'Configure favicon for any page or sub section of the site.',
'page callback' => 'favicon_list',
'access arguments' => array('administer favicons'),
);
$items['admin/build/favicon/list'] = array(
'title' => 'Favicons',
'description' => 'Configure favicon for any page or sub section of the site.',
'page callback' => 'favicon_list',
'access arguments' => array('administer favicons'),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/build/favicon/new'] = array(
'title' => 'New favicon',
'page callback' => 'drupal_get_form',
'page arguments' => array('favicon_edit', 3),
'access arguments' => array('administer favicons'),
'type' => MENU_LOCAL_TASK,
);
$items['favicon/edit/%'] = array(
'title' => 'Edit favicon',
'page callback' => 'drupal_get_form',
'page arguments' => array('favicon_edit', 1, 2),
'access arguments' => array('administer favicons'),
'type' => MENU_CALLBACK,
);
$items['favicon/delete/%'] = array(
'title' => 'Delete favicon',
'page callback' => 'drupal_get_form',
'page arguments' => array('favicon_delete', 2),
'access arguments' => array('administer favicons'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_preprocess_page().
*/
function favicon_preprocess_page(&$vars) {
global $base_path;
$display_path_all = array();
$result = db_query("SELECT display_path FROM {favicon_custom} WHERE status = 1 ORDER BY favid DESC");
while($row = db_fetch_array($result)) {
$display_path_all[] = $row['display_path'];
}
$router_item = menu_get_item();
if(in_array($router_item['path'], $display_path_all))
{
$filename = db_result(db_query("SELECT icon_file FROM {favicon_custom} WHERE display_path = '%s'", $router_item['path']));
$vars['head'] = trim(strip_tags($vars['head'], '<meta>')) . "\n"; // Remove all HTML tags from header except <meta>
$vars['head'] .= "<link rel=\"shortcut icon\" href=\"". check_url($base_path.$filename) ."\" type=\"image/x-icon\" />\n";
}
else
{
// Find all icon paths that ends with "/*"
$display_path_wildcard = preg_grep("/(.*)\/\*$/", $display_path_all);
// Check current path matches for which wildcard path. If not matches then check for upper level match.
// If current path is "admin/build/content" then find match for "admin/build/content", "admin/build", "admin".
$path_parts = explode("/", $router_item['path']);
for($i=count($path_parts);$i>=0;$i--)
{
unset($path_parts[$i]);
$test_path = implode("/", $path_parts)."/*";
// If match found then show the matched icon file
if(in_array($test_path, $display_path_wildcard))
{
$filename = db_result(db_query("SELECT icon_file FROM {favicon_custom} WHERE display_path = '%s'", $test_path));
$vars['head'] = trim(strip_tags($vars['head'], '<meta>')) . "\n"; // Remove all HTML tags from header except <meta>
$vars['head'] .= "<link rel=\"shortcut icon\" href=\"". check_url($base_path.$filename) ."\" type=\"image/x-icon\" />\n";
break;
}
}
}
}
function favicon_list()
{
global $base_url;
$rows = array();
$result = db_query("SELECT * FROM {favicon_custom} ORDER BY favid DESC");
while ($row = db_fetch_array($result)) {
$rows[] = array(l(t(basename($row['icon_file'])), $base_url .'/'. $row['icon_file']), $row['display_path'], ($row['status'] ? t('Active') : t('Inactive')), l(t('edit'), 'favicon/edit/'. $row['favid']), l(t('delete'), 'favicon/delete/'. $row['favid']));
}
$header = array(t('Favicon'), t('Display path'), t('Status'), array('data' => t('Operations'), 'colspan' => 2));
return theme('table', $header, $rows);
}
function favicon_edit($form_state = array(), $op, $iconid = NULL)
{
if (empty($iconid) || $op == 'new') {
$iconinfo = array(
'favid' => '',
'icon_file' => '',
'display_path' => '',
'status' => 0,
);
}
else
{
$iconinfo = db_fetch_array(db_query("SELECT * FROM {favicon_custom} WHERE favid = %d", $iconid));
}
$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['icon_upload'] = array(
'#type' => 'file',
'#title' => t('Upload favicon'),
'#description' => 'Supported types : .ico',
'#size' => 57,
//'#required' => TRUE,
);
$form['nodepath'] = array(
'#type' => 'textfield',
'#title' => t('Display path'),
'#maxlength' => 255,
'#default_value' => $iconinfo['display_path'],
'#description' => t("The '*' character is a wildcard. Example paths are blog for the blog page and blog/* for every personal blog."),
'#required' => TRUE,
);
$form['status'] = array(
'#type' => 'checkbox',
'#title' => t('Active'),
'#default_value' => $iconinfo['status'] ? 1 : 0,
);
$form['favicon_op'] = array('#type' => 'value', '#value' => $op);
$form['iconid'] = array('#type' => 'value', '#value' => $iconid);
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
return $form;
}
/**
* Validate the favicon edit page form submission.
*/
function favicon_edit_validate($form, &$form_state) {
// TODO
}
/**
* Process the favicon edit page form submission.
*/
function favicon_edit_submit($form, &$form_state) {
if(isset($_FILES['files']) && $_FILES['files']['name']['icon_upload'] && is_uploaded_file($_FILES['files']['tmp_name']['icon_upload']))
{
$path_parts = pathinfo($_FILES['files']['name']['icon_upload']);
if($path_parts['extension'] == 'ico')
{
$file_upload_path = file_directory_path().'/'.$_FILES['files']['name']['icon_upload'];
file_move($_FILES['files']['tmp_name']['icon_upload'], $file_upload_path);
$fileflag = 1;
}
else
{
drupal_set_message('Please upload a favicon (.ico) file.', 'error');
}
}
if($form_state['values']['favicon_op'] == 'new')
{
db_query("INSERT INTO {favicon_custom} VALUES('', '%s', '%s', %d)", $file_upload_path, $form_state['values']['nodepath'], $form_state['values']['status']);
drupal_set_message(t('Favicon %favicon has been added.', array('%favicon' => $path_parts['basename'])));
}
else
{
if(isset($fileflag) && $fileflag == 1)
{
db_query("UPDATE {favicon_custom} SET icon_file = '%s', display_path = '%s', status = %d WHERE favid = %d", $file_upload_path, $form_state['values']['nodepath'], $form_state['values']['status'], $form_state['values']['iconid']);
}
else
{
db_query("UPDATE {favicon_custom} SET display_path = '%s', status = %d WHERE favid = %d", $form_state['values']['nodepath'], $form_state['values']['status'], $form_state['values']['iconid']);
}
drupal_set_message(t('Favicon %favicon has been updated.', array('%favicon' => $path_parts['basename'])));
}
$form_state['redirect'] = 'admin/build/favicon';
return;
}
/**
* Favicon delete page.
*/
function favicon_delete(&$form_state, $iconid) {
$form['iconid'] = array(
'#type' => 'value',
'#value' => $iconid,
);
$path_parts = pathinfo(db_result(db_query("SELECT icon_file FROM {favicon_custom} WHERE favid = %d", $iconid)));
$filename = $path_parts['basename'];
$form['iconfile'] = array(
'#type' => 'value',
'#value' => $filename,
);
return confirm_form($form, t('Are you sure you want to delete %favicon?', array('%favicon' => $filename)), 'admin/build/favicon', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}
/**
* Process favicon delete form submission.
*/
function favicon_delete_submit($form, &$form_state) {
$filepath = file_directory_path().'/'.$form_state['values']['iconfile'];
file_delete($filepath);
db_query("DELETE FROM {favicon_custom} WHERE favid = %d", $form_state['values']['iconid']);
drupal_set_message(t('Favicon %favicon has been deleted.', array('%favicon' => $form_state['values']['iconfile'])));
$form_state['redirect'] = 'admin/build/favicon';
return;
}
?>File -> favicon.install
================
<?php
// $Id: favicon.install $
/**
* Implementation of hook_install().
*/
function favicon_install() {
// Create tables.
drupal_install_schema('favicon');
}
/**
* Implementation of hook_uninstall().
*/
function favicon_uninstall() {
// Remove tables.
drupal_uninstall_schema('favicon');
}
/**
* Implementation of hook_schema().
*/
function favicon_schema() {
$schema['favicon_custom'] = array(
'description' => 'favicon_custom table for managing multiple favicons',
'fields' => array(
'favid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'icon_file' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
'display_path' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
'status' => array(
'type' => 'int',
'size' => 'tiny',
'not null' => FALSE,
'default' => 1,
)
),
'primary key' => array('favid'),
);
return $schema;
}
?>File -> favicon.info
==============
; $Id: favicon.info $
name = Favicon
description = Set path based custom favicons.
core = 6.x
version = "6.x-1.0"
core = "6.x"
Comments
Great feature!
I really like the idea behind this module and have tried it but it doesn't work :(
I have path module enabled and have been fidling around a bit... have changed line #75 to:
$path_parts = explode("/", $router_item['page_arguments'][0]->path);and now line #82 returns true... and if I var_dump $vars['head'] on line #87 it shows that the module has set the favicon.
Yet in my theme it doesn't and the default favicon is printed in the head...
Will see if I can come up with some solutions but it's a low budget project so if I can't fix this in little time I'm going to have to pass...
Cheers
________________
Live fast die young