< script src="jquery-1.7.1.min.js" type="text/javascript" >< /script >
< script language="javascript" >
/*
Disable right click script (on images)- By Mano
for all Browsers :)
*/
$(document).ready(function(){
$('img').bind("contextmenu",function(e){
return false;
});
});
< /script >
Note: This will not work only in IE browser
Basics of Drupal, Techniques in drupal, Customizing the Drupal themes, Custom functions in drupal, Does and Dont in Drupal, Interview Question & Answers in Drupal etc.
Showing posts with label Sample Code. Show all posts
Showing posts with label Sample Code. Show all posts
Tuesday, March 20, 2012
Disable right click on Image tag using Javascript Jquery
< script src="jquery-1.7.1.min.js" type="text/javascript" >< /script >
< script language="javascript" >
/*
Disable right click script (on images)- By Mano
*/
$(document).ready(function(){
$("img").attr("oncontextmenu", "return false;");
});
< /script >
Note: This will not work only in IE browser
< script language="javascript" >
/*
Disable right click script (on images)- By Mano
*/
$(document).ready(function(){
$("img").attr("oncontextmenu", "return false;");
});
< /script >
Note: This will not work only in IE browser
Monday, March 19, 2012
Custom Node Creation
Code in manomodule.module file
/* Error handling */
error_reporting(E_ALL);
ini_set("display_errors","On");
/**
*
* Implementation of menu hook
*/
function manomodule_menu() {
$items = array();
$items['test_form'] = array(
'title' => t('Test form'),
'page callback' => 'get_products',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
*
* Implementation of theme hook
*/
function manomodule_theme() {
return array(
'page_node_form' => array(
'arguments' => array('form' => NULL),
'template' => 'product-list',
),
);
}
function get_products() {
/* Create a new node and form creation */
$node = new stdClass();
$node->type = 'page';
module_load_include('inc', 'node', 'node.pages');
$form_arr = drupal_get_form('page_node_form',$node);
return $form_arr;
}
/**
*
* Implementation of form alter hook
*/
function manomodule_form_alter(&$form, &$form_state, $form_id) {
/* Theme alter based on form id */
if ($form_id == 'page_node_form') {
$form['#theme'] = 'page_node_form';
}
}
/**
*
* Implementation of node related process hook
*/
function manomodule_nodeapi(&$node, $op) {
if ($node->type == 'page') {
switch ($op) {
case 'insert':
$_SESSION['fromform'] = 'product';
break;
case 'view':
// redirect to thank you page when submit
// and workflow process done.
$fromform = $_SESSION['fromform'];
unset($_SESSION['fromform']);
if ($fromform == 'product') {
drupal_goto('admin/content/node');
}
break;
}
}
}
Code in product-list.tpl.php
print drupal_render($form['title']);
print drupal_render($form['body_field']['body']);
print drupal_render($form['form_id']);
print drupal_render($form['form_build_id']);
print drupal_render($form['form_token']);
print drupal_render($form['buttons']['submit']);
/* print drupal_render($form); */
/* Error handling */
error_reporting(E_ALL);
ini_set("display_errors","On");
/**
*
* Implementation of menu hook
*/
function manomodule_menu() {
$items = array();
$items['test_form'] = array(
'title' => t('Test form'),
'page callback' => 'get_products',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
*
* Implementation of theme hook
*/
function manomodule_theme() {
return array(
'page_node_form' => array(
'arguments' => array('form' => NULL),
'template' => 'product-list',
),
);
}
function get_products() {
/* Create a new node and form creation */
$node = new stdClass();
$node->type = 'page';
module_load_include('inc', 'node', 'node.pages');
$form_arr = drupal_get_form('page_node_form',$node);
return $form_arr;
}
/**
*
* Implementation of form alter hook
*/
function manomodule_form_alter(&$form, &$form_state, $form_id) {
/* Theme alter based on form id */
if ($form_id == 'page_node_form') {
$form['#theme'] = 'page_node_form';
}
}
/**
*
* Implementation of node related process hook
*/
function manomodule_nodeapi(&$node, $op) {
if ($node->type == 'page') {
switch ($op) {
case 'insert':
$_SESSION['fromform'] = 'product';
break;
case 'view':
// redirect to thank you page when submit
// and workflow process done.
$fromform = $_SESSION['fromform'];
unset($_SESSION['fromform']);
if ($fromform == 'product') {
drupal_goto('admin/content/node');
}
break;
}
}
}
Code in product-list.tpl.php
print drupal_render($form['title']);
print drupal_render($form['body_field']['body']);
print drupal_render($form['form_id']);
print drupal_render($form['form_build_id']);
print drupal_render($form['form_token']);
print drupal_render($form['buttons']['submit']);
/* print drupal_render($form); */
Tuesday, January 10, 2012
SOLVED: An illegal choice has been detected. Please contact the site administrator
‘#validated’ => ‘TRUE’ add this into your form elements definitions:
Example:
form['my_dynamic_select'] = array(
…
‘#type’ => ‘select’,
‘#validated’ => TRUE
…
)
Reason:
If we are using an input in AJAX related situation we will fill the input field like SELECT (dropdown) this error will shown in the interface. Because when a value is not present in the page load will not allowed in form submission time. So, the Drupal restricts this type of value. This value is considered as illegal value. Say for example an user can inject a set of value in our site and they can choose a particular value in it.
But our case is also same like injecting in sense of pushing a new set of values through AJAX. So now and then we need to used the form field attribute "validate as True", to get a Permanent and Perfect solution too.
Thanks.
Example:
form['my_dynamic_select'] = array(
…
‘#type’ => ‘select’,
‘#validated’ => TRUE
…
)
Reason:
If we are using an input in AJAX related situation we will fill the input field like SELECT (dropdown) this error will shown in the interface. Because when a value is not present in the page load will not allowed in form submission time. So, the Drupal restricts this type of value. This value is considered as illegal value. Say for example an user can inject a set of value in our site and they can choose a particular value in it.
But our case is also same like injecting in sense of pushing a new set of values through AJAX. So now and then we need to used the form field attribute "validate as True", to get a Permanent and Perfect solution too.
Thanks.
Friday, December 23, 2011
Calling the codes in Background processing in PHP
$check_async_calls_dashboard = variable_get('gems_dashboard_async_call', '0');
if ($check_async_calls_dashboard) {
// buffer all upcoming output
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
ignore_user_abort(true);
set_time_limit(0);
ob_start();
echo 'done';
// get the size of the output
$size = ob_get_length();
// send headers to tell the browser to close the connection
header("Content-Length: $size");
header('Connection: close');
// flush all output
ob_end_flush();
ob_flush();
flush();
// close current session
// if (session_id()) session_write_close();
}
/* put your coding here.. which need to call in background process.. */
if ($check_async_calls_dashboard) {
// buffer all upcoming output
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
ignore_user_abort(true);
set_time_limit(0);
ob_start();
echo 'done';
// get the size of the output
$size = ob_get_length();
// send headers to tell the browser to close the connection
header("Content-Length: $size");
header('Connection: close');
// flush all output
ob_end_flush();
ob_flush();
flush();
// close current session
// if (session_id()) session_write_close();
}
/* put your coding here.. which need to call in background process.. */
Friday, December 16, 2011
Custom Caching code for Repeatedly used code in Drupal
global $contentType_cache;
if (!isset($contentType_cache)) {
$cache_content = cache_get("CACHE_TBL_CONTENTTYPE");
$cache_content = $cache_content->data;
if (!isset($cache_content)) {
// Load the complete list of the thumbnail
$result = db_query("SELECT type, name FROM {node_type}");
$records = array();
while ($record = db_fetch_object($result)) {
$records[$record->type] = $record;
}
cache_set("CACHE_TBL_CONTENTTYPE", $records);
$contentType_cache= $records;
} else {
$contentType_cache= $cache_content;
}
}
if (!isset($contentType_cache)) {
$cache_content = cache_get("CACHE_TBL_CONTENTTYPE");
$cache_content = $cache_content->data;
if (!isset($cache_content)) {
// Load the complete list of the thumbnail
$result = db_query("SELECT type, name FROM {node_type}");
$records = array();
while ($record = db_fetch_object($result)) {
$records[$record->type] = $record;
}
cache_set("CACHE_TBL_CONTENTTYPE", $records);
$contentType_cache= $records;
} else {
$contentType_cache= $cache_content;
}
}
Friday, December 9, 2011
Find and Replace in Mysql Query with Example
Syntax:
update [table_name] set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]');
Example:
update [table_name] set [field_name] = replace([field_name],'[string_to_find]','[string_to_replace]');
Example:
UPDATE system SET filename = replace( filename, 'sites/all/themes/gemswebsitetheme', 'sites1/all/themes/gemswebsitetheme2' ) ;Friday, October 7, 2011
Get Child taxonomy items in Drupal
$parent_tid = 1002;
$childs = taxonomy_get_children($parent_tid);
foreach ($childs as $ksubspec) {
$childs_lists .= $ksubspec->name." | ";
}
echo "Child Taxonomy list : ".$childs_lists;
$childs = taxonomy_get_children($parent_tid);
foreach ($childs as $ksubspec) {
$childs_lists .= $ksubspec->name." | ";
}
echo "Child Taxonomy list : ".$childs_lists;
Get Parents taxonomy items in Drupal
$child_tid = 1075;
$parents = taxonomy_get_parents($child_tid);
foreach ($parents as $ksubspec) {
$parents_lists .= $ksubspec->name." | ";
}
echo "Parent Taxonomy list : ".$parents_lists;
$parents = taxonomy_get_parents($child_tid);
foreach ($parents as $ksubspec) {
$parents_lists .= $ksubspec->name." | ";
}
echo "Parent Taxonomy list : ".$parents_lists;
Tuesday, October 4, 2011
Finding a module list which are having a particular hook in Drupal
Syntax:
$sort By default, modules are ordered by weight and filename, settings this option to TRUE, module list will be ordered by module name.
$refresh For internal use only: Whether to force the stored list of hook implementations to be regenerated (such as after enabling a new module, before processing hook_enable).
Example #1
$list = module_implements("cron");
ep($list);
Output #1
$list = module_implements("cron", true);
ep($list);
Output #2
module_implements($hook, $sort = FALSE, $refresh = FALSE)Parameters:
$hook The name of the hook (e.g. "help" or "menu").$sort By default, modules are ordered by weight and filename, settings this option to TRUE, module list will be ordered by module name.
$refresh For internal use only: Whether to force the stored list of hook implementations to be regenerated (such as after enabling a new module, before processing hook_enable).
Example #1
$list = module_implements("cron");
ep($list);
Output #1
Array
(
[0] => dblog
[1] => filter
[2] => node
[3] => search
[4] => system
[5] => update
[6] => captcha
)Example #2
$list = module_implements("cron", true);
ep($list);
Output #2
Array
(
[0] => captcha
[1] => dblog
[2] => filter
[3] => node
[4] => search
[5] => system
[6] => update
)
Checking whether a module having a hook or not
echo "--".module_hook("common", "chellam");
/* if the hook method exists in a module, then it will return 1 else return 0 */
/* if the hook method exists in a module, then it will return 1 else return 0 */
Enabling a module in Drupal
module_enable($list of module name);
Note: You have to give module name insense of array format. You can enable a single or multiple modules at the time.
Example:
module_enable(array("common", "adsense"));
Labels:
Enabling a module in Drupal,
module.inc,
Sample Code
Disabling a module in Drupal
Syntax:
module_disable($list of module name);
Note: You have to give module name insense of array format. You can disable a single or multiple modules at the time.
Example:
module_disable(array("common", "adsense"));
With this example, am disabling two modules at the same time.
module_disable($list of module name);
Note: You have to give module name insense of array format. You can disable a single or multiple modules at the time.
Example:
module_disable(array("common", "adsense"));
With this example, am disabling two modules at the same time.
Checking whether a module is Exists in Drupal or not
/* Checking with valid module */
$status = module_exists("system");
echo "--".$status;
/* output: 1 i.e. Module exists */
/* Checking with invalid module */
$status = module_exists("systems");
echo "--".$status;
/* output: 0 i.e. Module does not exists */
$status = module_exists("system");
echo "--".$status;
/* output: 1 i.e. Module exists */
/* Checking with invalid module */
$status = module_exists("systems");
echo "--".$status;
/* output: 0 i.e. Module does not exists */
Required Modules in Drupal in module.inc
Function Definition:
function drupal_required_modules() {
return array('block', 'filter', 'node', 'system', 'user');
}
Example:
$list = drupal_required_modules() ;ep($list);
Array ( [0] => block [1] => filter [2] => node [3] => system [4] => user )
So, here is the place the list of required core modules desided in Drupal.
Thursday, September 29, 2011
How the drupal_set_message() function works in Drupal?
First of all drupal_set_message() is used to show the notification to the user.
Notifications in 3 different ways
1. status
2. warning
3. error
Example:
drupal_set_message("example for status message", 'status');
drupal_set_message("example for warning message", 'warning');
drupal_set_message("example for error message", 'error');
After the drupal_set_message() function intialized a message into it, it is actullay stored in SESSION variable.
Notifications in 3 different ways
1. status
2. warning
3. error
Example:
drupal_set_message("example for status message", 'status');
drupal_set_message("example for warning message", 'warning');
drupal_set_message("example for error message", 'error');
After the drupal_set_message() function intialized a message into it, it is actullay stored in SESSION variable.
function drupal_set_message($message = NULL, $type = 'status', $repeat = TRUE) {
if ($message) {
if (!isset($_SESSION['messages'])) {
$_SESSION['messages'] = array();
}
if (!isset($_SESSION['messages'][$type])) {
$_SESSION['messages'][$type] = array();
}
if ($repeat || !in_array($message, $_SESSION['messages'][$type])) {
$_SESSION['messages'][$type][] = $message;
}
}
// messages not set when DB connection fails
return isset($_SESSION['messages']) ? $_SESSION['messages'] : NULL;
} To display the message which is stored in the Session, Drupal using a function to achieve this. theme_status_messages ()
function theme_status_messages($display = NULL) {
$output = '';
foreach (drupal_get_messages($display) as $type => $messages) {
$output .= "<div class=\"messages $type\">\n";
if (count($messages) > 1) {
$output .= " <ul>\n";
foreach ($messages as $message) {
$output .= ' <li>' . $message . "</li>\n";
}
$output .= " </ul>\n";
}
else {
$output .= $messages[0];
}
$output .= "</div>\n";
}
return $output;
} Finally, with one example...
/* Assigning the Notification message */drupal_set_message("my testing message", 'warning');/* Displaying the Notification message */
echo theme_status_messages();
Clearing Form values in Drupal
function my_module_my_form($form_state) {
$form['name'] = array(
'#type' => 'fieldset',
'#title' => t('Name'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
// Removes the #required property and
// uses the validation function instead.
$form['name']['first'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#default_value' => "First name",
'#description' => "Please enter your first name.",
'#size' => 20,
'#maxlength' => 20,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
// Adds a new button to clear the form. The #validate property
// directs the form to use a new validation handler function in place
// of the default.
$form['clear'] = array(
'#type' => 'submit',
'#value' => 'Reset form',
'#validate' => array('my_module_my_form_clear'),
);
return $form;
}// This is the new validation handler for our Reset button. Setting
// the $form_state['rebuild'] value to TRUE, clears the form and also
// skips the submit handler.function my_module_my_form_clear($form, &$form_state) {
$form_state['rebuild'] = TRUE;
}
Tuesday, September 27, 2011
Preprocess in Drupal with Example
Process type : moduleName_preprocess
Description:
Do not confuse this with the preprocessor before it. This allows modules that did not originally implement the hook to influence the variables. Applies to all hooks.
Note: After you code completion, You have to do the clear cache from admin panel. Then only the dynamic variables get scope.
Examples:
/* Example #1: Just creating a dynamic variable for theming. */
$vars['preprocess_example1'] = "working mano.. working!";
Displaying this variable in template file:
echo $preprocess_example1;
/* Example #2: Loading a Block content and assigning into a dynamic variable for theming. */
$blocks = module_invoke('block', 'block', 'view', 1);
$blockContent = "";
if ($blocks['content'] != "n/a"){
$blockContent = $blocks['content'];
}
$vars['preprocess_example2'] = $blockContent;
Displaying this variable in template file:
echo $preprocess_example2;
Process type : moduleName_preprocess_page
Note: This dynamic variable only displayed in page.tpl.php, page-front.tpl.php, page-custom-menu.tpl.php pages only. This variable will not shown in our AJAX related separate template files. Becaz that file only points to that template file.
function common_preprocess_page(&$vars) {
/* Example #3: Just creating a dynamic variable for theming. */
$vars['preprocess_example3'] = "working in page templates.. working!";
}
Displaying this variable in template file:
echo $preprocess_example3;
Process type : phptemplate_preprocess
function phptemplate_preprocess(&$vars) {
/* Example #4: Just creating a dynamic variable for theming. */
$vars['phptemplate_example4'] = "testing content global for php template example!";
}
Displaying this variable in template file:
echo $phptemplate_example4;
Process type : phptemplate_preprocess_page
function phptemplate_preprocess_page(&$vars) {
/* Example #5: Just creating a dynamic variable for theming. */
$vars['phptemplate_example5'] = "testing content for specific php template example!";
}
Displaying this variable in template file:
echo $phptemplate_example5;
Process type : themename_preprocess
function danland_preprocess(&$vars) {
/* Example #6: Just creating a dynamic variable for theming. */
$vars['themetemplate_example6'] = "testing content for global php theme template !";
}
Displaying this variable in template file:
echo $themetemplate_example6;
Process type : themename_preprocess_page
function danland_preprocess_page(&$vars) {
/* Example #7: Just creating a dynamic variable for theming. */
$vars['themetemplate_example7'] = "testing content for specific php theme template !";
}
Displaying this variable in template file:
echo $themetemplate_example7;
Description:
Do not confuse this with the preprocessor before it. This allows modules that did not originally implement the hook to influence the variables. Applies to all hooks.
Note: After you code completion, You have to do the clear cache from admin panel. Then only the dynamic variables get scope.
Examples:
/* Example #1: Just creating a dynamic variable for theming. */
$vars['preprocess_example1'] = "working mano.. working!";
Displaying this variable in template file:
echo $preprocess_example1;
/* Example #2: Loading a Block content and assigning into a dynamic variable for theming. */
$blocks = module_invoke('block', 'block', 'view', 1);
$blockContent = "";
if ($blocks['content'] != "n/a"){
$blockContent = $blocks['content'];
}
$vars['preprocess_example2'] = $blockContent;
Displaying this variable in template file:
echo $preprocess_example2;
Process type : moduleName_preprocess_page
Note: This dynamic variable only displayed in page.tpl.php, page-front.tpl.php, page-custom-menu.tpl.php pages only. This variable will not shown in our AJAX related separate template files. Becaz that file only points to that template file.
function common_preprocess_page(&$vars) {
/* Example #3: Just creating a dynamic variable for theming. */
$vars['preprocess_example3'] = "working in page templates.. working!";
}
Displaying this variable in template file:
echo $preprocess_example3;
Process type : phptemplate_preprocess
function phptemplate_preprocess(&$vars) {
/* Example #4: Just creating a dynamic variable for theming. */
$vars['phptemplate_example4'] = "testing content global for php template example!";
}
Displaying this variable in template file:
echo $phptemplate_example4;
Process type : phptemplate_preprocess_page
function phptemplate_preprocess_page(&$vars) {
/* Example #5: Just creating a dynamic variable for theming. */
$vars['phptemplate_example5'] = "testing content for specific php template example!";
}
Displaying this variable in template file:
echo $phptemplate_example5;
Process type : themename_preprocess
function danland_preprocess(&$vars) {
/* Example #6: Just creating a dynamic variable for theming. */
$vars['themetemplate_example6'] = "testing content for global php theme template !";
}
Displaying this variable in template file:
echo $themetemplate_example6;
Process type : themename_preprocess_page
function danland_preprocess_page(&$vars) {
/* Example #7: Just creating a dynamic variable for theming. */
$vars['themetemplate_example7'] = "testing content for specific php theme template !";
}
Displaying this variable in template file:
echo $themetemplate_example7;
Monday, September 26, 2011
Executing PHP code in Drupal
/* Assigning a PHP code into a variable or
We can load PHP code based block content into a variable */
$phpContent = '
<?php
$sampleString = "my sample content!";
echo "Length of <i>" . $sampleString . "</i> is : " . strlen($sampleString);
?>';
/* PHP code is processed and giving output */
echo drupal_eval($phpContent);
Output:
We can load PHP code based block content into a variable */
$phpContent = '
<?php
$sampleString = "my sample content!";
echo "Length of <i>" . $sampleString . "</i> is : " . strlen($sampleString);
?>';
/* PHP code is processed and giving output */
echo drupal_eval($phpContent);
Output:
Length of my sample content! is : 18
File Handling in Drupal - file_save_upload()
Syntax: file_save_upload($source, $validators = array(), $dest = FALSE, $replace = FILE_EXISTS_RENAME)Example:$thumbnailPath = file_directory_path() . "/thumbnail"
$chapter_uploaded_file = file_save_upload($video, array(), $thumbnailPath);
file_set_status($chapter_uploaded_file, FILE_STATUS_PERMANENT);
Subscribe to:
Posts (Atom)