Posts Tagged 'Drupal'

Print module customization

Tested on: Drupal 5.7, Print 3.4

Drupal Printer Friendly module (Print) add “print this” functionality to nodes. Customization is pretty easy: according to README file you can add a print.tpl.php to your theme (or subtheme) directory and modify it. All override templates are (in check order):

  1. print.__node-type__.tpl.php in the theme directory
  2. print.__node-type__.tpl.php in the module directory
  3. print.tpl.php in the theme directory
  4. print.tpl.php in the module directory (supplied by the module)

You can copy the default print.tpl.php in module directory (4) to one of the 1-3 destinations files, then customize it. You can also use variables like $node->changed to enrich your template. You can read the dynamically generated list by Content Template module, if you have installed it.

Add to Bookmark / Favorites on Drupal

Tested on:

  • Drupal 5.x
  • Zen theme

On your page.tpl.php, change default node links rendering with this:

<?php if ($links): ?>
<div class=”links”>
<?php
# add to favorites / bookmarks link
$node->links['add2fav'] = Array(
‘title’=>trim(t(”Add to favorites”)),
‘href’=>$_GET['q'],
‘attributes’=>Array(
‘class’=>”add2fav”,
‘onclick’=>’bookmark()’,
)
);
# intercept and theme node links
print theme_links($node->links);
print ‘<script type=”text/javascript”>// <![CDATA[
function bookmark(){
var url = window.document.location;
var title = window.document.title;
if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
window.external.AddFavorite(url,title);
} else if (navigator.appName == "Netscape") {
window.sidebar.addPanel(title,url,"");
} else {
alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
}
}
// ]]></script>’;
?>
</div>
<?php endif; ?>

Now any node has your “Add to favorites” link, try to click it.

If you wrap alert message in a t() function, you can also translate displayed message.

See also:

Translate Drupal block titles

A simple snippet based on theme_block() and t() functions. (Tested on Drupal 5.x, Zen subtheme).

  1. Copy and paste this code into your template.php:

    function zen_block($block) {
    $output = “<div class=\”block block-$block->module\” id=\”block-$block->module-$block->delta\”>\n”;
    $output .= ” <h2 class=\”title\”>”.t($block->subject).”</h2>\n”;
    $output .= ” <div class=\”content\”>$block->content</div>\n”;
    $output .= “</div>\n”;
    return $output;
    }

  2. Go to admin/settings/locale/string/search and search the title to translate from English to your localization language.

Add CSS class to drupal form fieldset

Tested on:

  • Drupal 5.x
  • Zen custom subtheme

Customizing Drupal form appearance may sound difficult. But to add a CSS class is (surprisingly!) simple using theme_fieldset themeable function.

  • Copy theme_fieldset code from Drupal manual page
  • Paste to your template.php:
    • If you’re using a Zen theme, change theme_fieldset to zen_fieldset. You can try to use this method with your theme prefix.
    • If you’re using a theme based on phptemplate engine, you can change theme_fieldset to zen_fieldset to have the same effect of above (try one of this, not both).
  • Change the resultant code as you wish.

I’ve attached an example to generate CSS class from fieldset title: Add CSS class to drupal form fieldset.

# custom fieldset CSS class from element #title
$css_class = “my-custom-class-”;
$css_class .= str_replace(” “,”-”,strtolower($element['#title']));
# using transliteration module to transliterate/strip non-ASCII character where available
if(module_exists(’transliteration’)){
# including Transliteration functions
require_once(drupal_get_path(’module’, ‘transliteration’) .’/transliteration.inc’);
$css_class = transliteration_clean_filename($css_class);
}

Adding this code to your template:

  • Take all Drupal generated fieldset title
    • e.g. “Impostazioni del menù”
  • Transform it in a CSS class using Transliteration module
    • e.g. “my-custom-class-impostazioni-del-menu”
  • Queue it with other CSS classes (where available)
    • e.g “collapsible my-custom-class-impostazioni-del-menu”

I apply a previous substitution of whitespace using “-” (transliteration module use underscore “_”) and you can do the same, but later remember to use transliteration_clean_filename to safely sanitize class name for any other case you’ve not contemplated (accents, special characters).

Customize links menu in Drupal

Tested on: Zen theme, Drupal 5.7

  1. Copy function theme_links from includes/themes.inc
  2. Paste it into your template.php
  3. Rename it in theme_primarylinks (or anything you want)
  4. In your page.tpl.php, change
    1. print theme(’links’, $primary_links);
    2. to print theme(’primarylinks’, $primary_links);
  5. Apply some changes to theme_primarylinks to customize your primary links.

According to theme manual, for any first argument passed to theme function a theme_functionname function is called.

Contemplate and views: create a link without $node->path

In contemplate and views, $node->path return current page path. But this variable is only available to users with “administer alias” permission, so as administrator you can view links, but as normal user you cannot use them.

Here a $node->path alternative:

l(strip_tags($node->title),"node/".$node->nid)

Since $node->title is raw input, strip_tags strips all HTML tags.
A node/$node->nid links to article. Drupal link function returns the (in case) aliased path.

Enable user profile field token on Drupal

Tested on:

  1. In token module directory, rename token_user.inc to token_user.inc.orig
  2. Copy pmail/patches/token_user.inc in  Token module directory
  3. Go to Personalized E-mails setting and watch your profile fields as tokens.

Raise Drupal files upload limit

  1. In your Drupal site, go to admin/settings/uploads
  2. Raise upload limits. If the value you want to insert is greater than PHP allowed value:
  3. On bottom of your .htaccess file (drupal/.htaccess) add these lines:
    # upload settings
    # cfr. http://it.php.net/manual/it/function.ini-set.php
    php_value post_max_size 50M
    php_value upload_max_filesize 50M
    php_value memory_limit 128M

    Change values as you wish.
  4. Return to admin/settings/uploads and watch changes on PHP limit.

See also:

Improve Drupal performance

I use YSlow to check a Drupal site performance. Here some tips to get an A with Drupal 5.x.

Step 1: Fewer HTTP requests:

  • Aggregate CSS (/admin/settings/performance)
  • Aggregate JS with Javascript Aggregator (experimental). To avoid JavaScript errors in some modules (i.e. TinyMCE), you can use the dirscard list patch by derjochenmeyer on the dev version (it’ll be added to the next release). To Drupal 6.x users: Drupal 6.x comes with a native JS Aggregator support, so you don’t need an additional module.
  • Reduce CSS background images: select carefully your theme.

Step 2: Compress pages on-the-fly:
Many modern browsers support Gzip compression. Through .htaccess, you can tell your server to serve gzipped HTML pages, JavaScript and CSS files (or any other text file, since images like JPG and PNG are compressed natively). You can use two approach, depending on your server settings, just add on bottom of your .htaccess:

  1. PHP flag method (compress only php served pages):
    # via-PHP compression (only with compatible browsers)
    php_flag zlib.output_compression On
  2. Apache mod_deflate method (preferred method where available)
    # Enable file compression by MIME type
    AddOutputFilterByType DEFLATE text/html text/plain text/xml
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE text/css
    # Exclude Not compatible browsers (uncomment to activate)
    # BrowserMatch ^Mozilla/4 gzip-only-text/html
    # BrowserMatch ^Mozilla/4\.0[678] no-gzip
    # BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

    Using this method, you can compress HTML pages, JavaScript, CSS files specifying ;MIME type

    See also / references

    Use lightbox for all inline elements

    Prerequisites:

    You simply enable lightbox v2 on image nodes (admin/settings/lightbox2) but your lightbox show a small imagecache thumbnail?

    • Disable lightbox on image nodes (admin/settings/lightbox2)
    • Enable “Display image with a link to the image file” (admin/settings/inline)
    • Apply this Inline lightbox patch

    This patch simply add a rel=”lightbox” to each inline image link, natively pointing to full-size image.

    Next Page »