Thursday, March 10, 2011
Wednesday, February 23, 2011
drupal with jquery
This variable indicates whether or not the browser will support drupal.js and jquery.js. Returns true / false.
Drupal.attachBehaviors()
The Drupal Behaviors feature provides a standard method for attaching some particular information (called a behavior) to zero or more elements on a page.
Drupal.checkPlain()
It allow us to display HTML tags in browser.
example:
myString = "Add a line break using the <br/> tag.";
escapedString = Drupal.checkPlain(myString);
output: Add a line break using the <br/> tag.
Drupal.parseJson()
This function was intended to be used for parsing AJAX data that was returned from the server in the JSON (JavaScript Object Notation) format. JSON data looks like JavaScript.
Here's an example describing a person's name:
jsonString = "{'first': 'Merriet', 'last': 'Jerry'}";
name = Drupal.parseJson(jsonString);
name.first
Output: Merriet
The string is parsed and converted to an object. We can then use that object directly in JavaScript.
Drupal.encodeURIComponent()
It correctly converts other characters while preserving the slashes.
myString = 'node/1/calc%';
Drupal.encodeURIComponent(myString);
Output: node/1/calc%25
Drupal.getSelection()
This tool is used to find out what portion of a text area (<textarea></textarea>) is selected.
For example, when you select a section of text with your mouse, this function can be used to find out the starting and ending locations of the selection.
The Drupal.getSelection() function returns an object with two attributes: start and end. These two properties are integers which represent the beginning and ending of the selection.
Drupal.t()
This function's job is to take a string and perform any translation actions on it.
var params = {
"@siteName": "Example.Com",
"!url": "http://example.com/"
};
var txt = Drupal.t("The URL for @siteName is !url.", params);
alert( Drupal.formatPlural(i, "Johnny has 1 apple.", "Johnny has @count apples."));
}
This function takes these arguments:
- A number (If it is 1, then the singular will be used, otherwise the plural form will be used.)
- A singular string (in English).
- A plural string (in English)
append() and appendTo() jQuery methods
An easily overlooked difference between append() and appendTo() is in the return value; both return jQuery objects. But where append() returns a jQuery object wrapping the thing(s) that have been appended to, the appendTo() call returns the elements that were appended.
//Create our content
var appendHTML = "<p>I'm going to be added to the DOM</p>";
//Select our element, then add the html
$("#container").append(appendHTML);
//Create our content, then select the element to append to
$(appendHTML).appentTo("#container");
Wednesday, February 16, 2011
Rotate content using jquery
var StickyRotate = StickyRotate || {};
StickyRotate.init = function() {
var stickies = $(".sticky");
// If we don't have enough, stop immediately.
if (stickies.size() <= 1 || $('#node-form').size() > 0) {
return;
}
var highest = 100;
stickies.each(function () {
var stickyHeight = $(this).height();
if(stickyHeight > highest) {
highest = stickyHeight;
}
});
stickies.hide().css('height', highest + 'px');
StickyRotate.counter = 0;
StickyRotate.stickies = stickies;
stickies.eq(0).fadeIn('slow');
setInterval(StickyRotate.periodicRefresh, 7000);
};
StickyRotate.periodicRefresh = function () {
var stickies = StickyRotate.stickies;
var count = StickyRotate.counter;
var lastSticky = stickies.size() - 1;
var newcount;
if (count == lastSticky) {
newcount = StickyRotate.counter = 0;
}
else {
newcount = StickyRotate.counter = count + 1;
}
stickies.eq(count).fadeOut('slow', function () {
stickies.eq(newcount).fadeIn('slow');
});
};
Call the script by using
$(document).ready(StickyRotate.init);
Ref book: Drupal 6 JavaScript and jQuery
Tuesday, February 15, 2011
filters and input formats in drupal
To view a list of installed filters, either configure an existing input format or create a new one at Administrator >> Site Cofiguration >> Input formats
Life cycle of text-filtering system
Ref book: Pro Drupal Development
print page content using drupal and javascript
var PrinterTool = {};
PrinterTool.windowSettings = 'toolbar = no, location = no,' + ' status = no, menu = no, scrollbars = yes, width = 650, height = 400';
PrinterTool.print = function (tagID) {
var target = document.getElementById(tagID);
var title = document.title;
if(!target || target.childNodes.length === 0) {
alert("Nothing to Print");
return;
}
var content = target.innerHTML;
var text = '<html><head><title>' + title +
'</title><body>' + content + '</body></html>';
printerWindow = window.open('', '', PrinterTool.windowSettings);
printerWindow.document.open();
printerWindow.document.write(text);
printerWindow.document.close();
printerWindow.print();
};
Call the function with ID. Paste following code in your tpl file/ block
<a href="javascript:PrinterTool.print('main')" >Print</a>
Ref book: Drupal 6 JavaScript and jQuery
Monday, February 14, 2011
Setup drupal subsite in local machine
10 Steps to setup subsite in local machine with windows Environment.
1. Create a subfolder subsite under drupal6/sites/subsite.(drupal6 is an main site)
2. Copy the default.settings.php from sites/default folder and renamed as settings.php, then paste it into drupal6/sites/subsite/settings.php
3. Create folders themes and modules under sites/subsite/themes and sites/subsite/modules. Place the site specific themes and modules.
4. Open the file httpd-vhosts.conf from bin/apache/conf/extra/ httpd-vhosts.conf
6. In windows system goto start > run (win+r) and type drivers and press ok.
7. Then open the folder etc. edit the file hosts.
Add the host name under default host like..
127.0.0.1 tobylove6
127.0.0.1 subsite
Alter page layout based on content type
Friday, February 11, 2011
Switch Contact form template In Drupal
function garland_theme(){
return array(
'contact_mail_page' => array(
'arguments' => array('form' => NULL),
'template' => 'contact-mail-page',
),
);
}
step 2: create template files
contact-mail-page-info.tpl.php
contact-mail-page-quote.tpl.php
print $form_markup;
step 3: Create Preprocess Function
function garland_preprocess_contact_mail_page(&$vars){
switch(arg(1)){
case 'quote':
$vars['form']['name']['#title'] = t('Name');
$vars['form']['message']['#title'] = t('What you need');
$vars['template_file'] = 'contact-mail-page-quote';
break;
case 'info':
$vars['form']['message']['#title'] = t('what info you need??');
$vars['template_file'] = 'contact-mail-page-info';
break;
}
$vars['form_markup'] = drupal_render($vars['form']);
}
Implement Drupal Autocomplete
function custom_menu(){
$items = array();
$items['custom/article'] = array(
'title' => t('Hello'),
'page callback' => 'article_load',
'access callback' => 'user_access',
'access arguments' => array('access user profiles'),
'type' => MENU_CALLBACK,
);
return $items;
}
Form
$form['article'] = array(
'#type' => 'textfield',
'#title' => t('Article Title'),
'#description' => t('Known article'),
'#default_value' => $form['_account']['#value']->article,
'#autocomplete_path' => 'custom/article',
);
function article_load($string = '') {
$matches = array();
if ($string) {
$result = db_query_range("SELECT nid, title FROM {node} WHERE LOWER(title) LIKE LOWER('%s%%')", $string, 0, 10);
while ($user = db_fetch_object($result)) {
$matches[$user->title] = check_plain($user->title);
}
}
drupal_json($matches);
}
Wednesday, December 29, 2010
Get content type using node id
$nid = arg(1);
$node = node_load(array('nid' => $nid));
$type = $node->type;
}
Monday, September 27, 2010
Setup Drupal multisite feature in Local machine
Setup Drupal's multisite feature in Local machine.
For that we have to follow the following three steps.
1. Add alias to our Apache configuration file
2. Redirecting all requests to index.php
3. Creating our settings.php
Add alias to our Apache configuration file
We want requests for the 3 subdirectories(emea, la, uk) to go to the same Drupal instance.We can do this using Apache's Alias functionality.
DocumentRoot “/xampp/htdocs/drupal”
Alias /emea C:/xampp/htdocs/drupal
Alias /la C:/xampp/htdocs/drupal
Alias /uk C:/xampp/htdocs/drupal
I'm supposing here Drupal's codebase is hosted in C:/xampp/htdocs/drupal on your machine
Redirecting all requests to index.php
Now that we're serving all requests from one codebase we have to redirect all requests to index.php. This needs to be done since all Drupal requests are served from one endpoint, called index.php. So we have to do the below changes in the .htaccess file in the same order as mentioned below.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/emea/(.*)$
RewriteRule ^(.*)$ /emea/index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/la/(.*)$
RewriteRule ^(.*)$ /la/index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/emea/(.*)$
RewriteRule ^(.*)$ /uk/index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Creating our settings.php
We can now have different settings (database, ...) for each site by creating a different settings file for each site. Place the settings.php for each file under a directory called after the domain name and subdirectory. In our example we would have the following setting files:
C:/xampp/htdocs/drupal/sites/default/settings.php -Base site Settings.php
C:/xampp/htdocs/drupal/sites/localhost.emea/settings.php -EMEA site Settings.php
C:/xampp/htdocs/drupal/sites/localhost.la/settings.php -LA site Settings.php
C:/xampp/htdocs/drupal/sites/localhost.uk/settings.php -UK site Settings.php
Now, start installing the site by using the below URLS
1. http://localhost – Base site
2. http://localhost/emea – EMEA site
3. http://localhost/la – LA site
4. http://localhost/uk – UK site
Enjoy the Drupal's multisite feature in the subdirectories.
Friday, September 24, 2010
MySQL Command-Line
shell> mysql –help
Connect to Server
shell> mysql -h host -u user -p
Enter password: ********
Disconnect from the Server
mysql> QUIT
On Unix, you can also disconnect by pressing Control-D.
Create Database
mysql> CREATE DATABASE menagerie;
Creating a database does not select it for use; you must do that explicitly. To make menagerie the current database, use this command:
mysql> USE menagerie
Database changed
Use a CREATE TABLE statement to specify the layout of your table:
mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
-> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
Once you have created a table, SHOW TABLES should produce some output:
mysql> SHOW TABLES;
To verify that your table was created the way you expected, use a DESCRIBE statement:
mysql> DESCRIBE pet;
After creating your table, you need to populate it. The LOAD DATA and INSERT statements are useful for this.
To load the text file pet.txt into the pet table, use this statement:
mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet;
If you created the file on Windows with an editor that uses \r\n as a line terminator, you should use this statement instead:
mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet
-> LINES TERMINATED BY '\r\n';
mysql> SELECT VERSION(), CURRENT_DATE;
Keywords may be entered in any lettercase. The following queries are equivalent:
mysql> SELECT VERSION(), CURRENT_DATE;
mysql> select version(), current_date;
mysql> SeLeCt vErSiOn(), current_DATE;
It demonstrates that you can use mysql as a simple calculator:
mysql> SELECT SIN(PI()/4), (4+1)*5;
mysql> SELECT VERSION(); SELECT NOW();
Here is a simple multiple-line statement:
mysql> SELECT
-> USER()
-> ,
-> CURRENT_DATE;
If you decide you do not want to execute a command that you are in the process of entering, cancel it by typing \c:
mysql> SELECT
-> USER()
-> \c
The simplest form of SELECT retrieves everything from a table:
mysql> SELECT * FROM pet;
mysql> SELECT DATABASE();
mysql> SHOW TABLES;
mysql> DESCRIBE pet;
mysql> SELECT USER()