Tuesday, June 21, 2011

Example of drupal_write_record or Example of drupal write record

Insert a value into database :

$transition = array(
'sid' => $form_state['values']['new_transition_from'],
'target_sid' => $form_state['values']['new_transition_to'],
'roles' => $tabroles,
);
drupal_write_record('workflow_transitions', $transition);

Update a value into database :

$transition = array(
'sid' => $form_state['values']['new_transition_from'],
'target_sid' => $form_state['values']['new_transition_to'],
'roles' => $tabroles,
'tid' => $tid
);

drupal_write_record('workflow_transitions', $transition, 'tid');

In Update primary key is tid

Usage of drupal_add_js()

<?php
// This will add a JS file to your head (specifically the $scripts variable in page.tpl.php)
drupal_add_js(drupal_get_path('module', 'my_module') . '/my_module.js');

// This add inline JS to the head of the document
drupal_add_js('alert("Hello!")', 'inline');

// This will add variables in the Drupal.settings object
drupal_add_js(array('my_module' => array('my_setting' => 'this_value')), 'setting');
?>

Including js file in drupal

Adding JavaScript from within a module
<?php
drupal_add_js(drupal_get_path('module', 'mymodule') .'/my_js_file.js');
?>

Adding JavaScript from within a theme
<?php
drupal_add_js(drupal_get_path('theme', 'mytheme') .'/my_js_file.js', 'theme');
?>

Using the theme's .info file (Drupal 6)
name = My theme
description = Theme developed by me.
core = 6.x
engine = phptemplate
version = 6.x-1.0
;
scripts[] = my_script.js

Create a file & provide download option for that file

$myFile = "path_to_dir/test.(EXTENSION)EG jpg,js";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Contents to display";
fwrite($fh, $stringData);
fclose($fh);
if (file_exists($myFile)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($myFile));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($myFile));
ob_clean();
flush();
readfile($myFile);
exit;
}

Example with l of in drupal

l('TITLE','node/33');

Output:
<a href="/?q=node/33">TITLE</a>

l('TITLE','node/33',array('attributes'=>array('target'=>"_blank")));

Output :
<a href="/?q=node/33" target="_blank">TITLE</a>

l('TITLE','node/33',array('query'=>array('disp'=>4),'attributes'=>array
('target'=>"_blank")));

Output :
<a href="/?q=node/33&disp=4" target="_blank">TITLE</a>

l('TITLE','node/33',array('attributes'=>array('target'=>"_blank")),'fragment'=>'comment-
1');

Output :
<a href="/?q=node/33#comment-1" target="_blank">TITLE</a>

l('TITLE','node/33',array('query'=>array('disp'=>4),'attributes'=>array
('target'=>"_blank")),'fragment'=>'comment-1');

Output :
<a href="/?q=node/33&disp=4#comment-1" target="_blank">TITLE</a>

Create translation js file based on the po file content imported in database

//Pass the language code.
function custom_translation_create_js($language) {
// Get information about the locale.
$languages = language_list();
$language = $languages[$language];

// Construct the array for JavaScript translations.
// We sort on plural so that we have all plural forms before singular forms.
$result = db_query("SELECT s.lid, s.source, t.plid, t.plural, t.translation FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid WHERE t.language = '%s' ORDER BY t.plural DESC", $language->language);

$translations = $plurals = array();
while ($data = db_fetch_object($result)) {
// Only add this to the translations array when there is actually a translation.
if (!empty($data->translation)) {
if ($data->plural) {
// When the translation is a plural form, first add it to another array and
// wait for the singular (parent) translation.
if (!isset($plurals[$data->plid])) {
$plurals[$data->plid] = array($data->plural => $data->translation);
}
else {
$plurals[$data->plid] = array($data->plural => $data->translation);
}
}
elseif (isset($plurals[$data->lid])) {
// There are plural translations for this translation, so get them from
// the plurals array and add them to the final translations array.
$translations[$data->source] = array($data->plural => $data->translation) + $plurals[$data->lid];
unset($plurals[$data->lid]);
}
else {
// There are no plural forms for this translation, so just add it to
// the translations array.
$translations[$data->source] = $data->translation;
}
}
}

// Only operate when there are translations.
if (!empty($translations)) {
// Construct the JavaScript file.
$data = "Drupal.locale = { ";

if (!empty($language->formula)) {
$data .= "'pluralFormula': function(\$n) { return Number({$language->formula}); }, ";
}

$data .= "'strings': ". drupal_to_js($translations) ." };";

$data_hash = md5($data);

// Construct the directory where JS translation files are stored.
// There is (on purpose) no front end to edit that variable.
$dir = file_create_path(variable_get('locale_js_directory', 'languages'));
// Only create a new file if the content has changed.
if ($language->javascript != $data_hash) {
if (!empty($language->javascript)) {
// We are recreating the new file, so delete the old one.
file_delete(file_create_path($dir .'/'. $language->language .'_'. $language->javascript .'.js'));
$language->javascript = '';
}
else {
// The directory might not exist when there has not been a translation
// file before, so create it.
file_check_directory($dir, TRUE);
}
$str = "trans";
// The file name of the new JavaScript translation file.
$dest = $dir .'/'. $language->language .'_'. $str .'.js';
$path = file_save_data($data, $dest);

db_query("UPDATE {languages} SET javascript = '%s' WHERE language = '%s'", $path ? $data_hash : '', $language->language);

// Only report updated or creation when there is actually a file created.
if ($path) {
// There has already been a translation file before.
if (!empty($language->javascript)) {
watchdog('locale', t('Updated JavaScript translation file for the language %language.', array('%language' => t($language->name))));
}
else {
watchdog('locale', t('Created JavaScript translation file for the language %language.', array('%language' => t($language->name))));
}
}
else {
watchdog('locale', t('An error occured during creation of the JavaScript translation file for the language %language.', array('%language' => t($language->name))));
}
}
}

// There are no strings for JavaScript translation. However, if there is a file,
// delete it and reset the database.
elseif (!empty($language->javascript)) {
// Delete the old JavaScript file
file_delete(file_create_path(variable_get('locale_js_directory', 'languages') .'/'. $language->language .'_'. $language->javascript .'.js'));
db_query("UPDATE {languages} SET javascript = '' WHERE language = '%s'", $language->language);
watchdog('locale', t('Deleted JavaScript translation file for the locale %language.', array('%language' => t($language->name))));
}
}

Language translation for Drupal JavaScript files.

1. Make sure that you have entered Drupal.t () function before all the text.
2. You have to create a js file (fr-trans.js) like below u for corresponding language
Drupal.locale = {‘strings’: {"Certification":"Certification","Required":"Requis"}};
In my example I have given for French language.
3. Include the js file based on user language

Language translation in Drupal Based on user language or based on website

1. Make sure that you have entered t() function before all the text.
2. Install the locale module.
3. In admin settings to Languages Link add languages which you need.
4. Click on configure you can see language negotiation you have to select”
Path prefix with language fallback. "(You have to do this if you want language translation
based on user selected language in his my account page) Or In admin setting languages Click the default language for the webpage to change page to corresponding language(This will change the language of webpage)
5. Download the default po file provided by drupal from this link
http://localize.drupal.org/ for testing purpose. (Note: You need to create a po file
based on your content of you website )
6. Go to Translate interface link in site building
7. Click on the import link & select the language & import the selected language po file
8. Check for a particular user by changing his language