Follow us on Twitter!
Login - Register - Latests submissions

jchatard

jchatard's picture

History

Member for
39 weeks 2 days

Tips submitted by jchatard

To let users translate your module strings, you must use the t() function. This allows the Drupal community to create translation files *.po. But this also allows user to tweak some strings to fit their needs, with the String Overrides module.

So everytime your write a sentence or a word in your module, surround your string with the t() function.

This is the bad way:

$foo = "Thank you for your submission";

This is the good way:

$foo = t("Thank you for your submission");

Note that you must always write your module strings in English as this is the default language in Drupal and is used as a base to provide the localization system.

You will need to insert values in your strings:

Don't write:

$points = 3;
$foo = t("You just won $points points");

This is the good way to insert placeholders values:

$points = 3;
$foo = t("You just won !points points", array('!points' => $points));

Check out the resources links to learn more about t() function use.

Note that starting with Drupal 6, you can also make your JavaScript files translatable.

In your code use Drupal.t() function. Example:

greetingMessage = Drupal.t("Your vote has been saved!");

  • Go to Site configuration > Error reporting
  • Set Error reporting list box to Write errors to log
  • Save configuration

When developping a module, we often use some commonly used PHP functions. As you may not know Drupal provides some overrides for these functions. They are often related to strings, they have the same name as their PHP native equivalent except that they are prefixed with drupal_. Here is a list of PHP functions you should replace with its Drupal equivalent:

  • Replace strlen() with drupal_strlen()
  • Replace strtoupper() with drupal_strtoupper()
  • Replace strtolower() with drupal_strtolower()
  • Replace ucfirst() with drupal_ucfirst()
  • Replace substr() with drupal_substr()
  • Replace eval() with drupal_eval()
  • Replace clone with drupal_clone()

For more on this, you should read includes/unicode.inc and includes/common.inc.

It is recommended that you prevent users from registering on your website with some odd usernames like:

  • root
  • admin
  • administrator
  • webmaster

To prevent this from happening:

  • Go to Administer > User management > Access rules > Add rule
  • Set Access type to Deny
  • Set Rule type to Username
  • In Mask type root
  • Click the Add rule button

Repeat this for every username you don't want to be created on your site.

Having an .htaccess file is a pain regarding performances. Apache needs to read it on every single request (the page, every image, CSS files, JS files, etc.).

It is strongly recommended to move rules defined in Drupal .htaccess file in your Apache global configuration or in your vhost configuration file. This way all rules are only loaded 1 time, during Apache start.

To achieve this, just copy/paste your .htaccess content in your Apache server configuration file and don't forget to surround it by the directory where your Drupal install resides.

<Directory /var/www/path/to/drupal/directory/>
  AllowOverride None
  ###      ALL YOUR .HTACCESS CONTENT GOES HERE    ###
</Directory>

Note that we added AllowOverride None to prevent Drupal .htaccess to be read by Apache.

Check for syntaxe error:

$ apachectl configtest

And restart Apache:

$ apachectl restart

Note that if you are on Plesk, you must use the following command before restarting Apache:

$ /usr/local/psa/admin/sbin/websrvmng -u --vhost-name=example.com

  • Go to Administer > Site configuration > Performance
  • Set Caching mode to Normal or Agressive
  • Set Block cache to Enabled
  • Save your settings

In regards to page compression, you will find on admin/settings/performance a description that looks like By default, Drupal compresses the pages it caches in order to save bandwidth and improve download times. This option should be disabled when using a webserver that performs compression. To find out whether your server already performs compression have a peek at /etc/httpd/conf/httpd.conf (CentOS/Redhat) or /etc/apache2/mods-enabled (Debian/Ubuntu) to find out if the module mod_deflate is called. If so, turn off page compression.

You're done!

SEO SEO

If you what Pathauto to transform caracters with accents into simple letters like:

  • éèêë to become e
  • àâï to become a

You must enable in Pathauto the option Transliterate prior to creating alias. But by default, you can't tick the checkbox.

You first need to rename a file. From within the Pathauto directory, rename i18n-ascii.example.txt to i18n-ascii.txt. Once renamed, you can enable the option from the Pathauto settings form.

Go to Site configuration > Performance, in Bandwidth optimizations section:

  • Set Optimize CSS files to Enabled
  • Set Optimize JavaScript files to Enabled
  • Save your settings

It is recommended to put JavaScript files at the bottom of your document (i.e. your theme page.tpl.php). Here is an example with Garland, but it should be the same with other themes. Search for the line <?php print $scripts ?>:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php print $language->language ?>" lang="<?php print $language->language ?>" dir="<?php print $language->dir ?>">
  <head>
    <?php print $head ?>
    <title><?php print $head_title ?></title>
    <?php print $styles ?>
    <?php print $scripts ?>
  </head>

Cut and paste it just before the <?php print $closure ?> statment and before the closing </body> tag, as shown below:

  <?php print $scripts ?>
  <?php print $closure ?>
  </body>
</html>

Drupal comes with a set of TXT files at root level (things like install instructions, upgrade instructions, etc.). To avoid malicious users to have information on the Drupal version you are using, it is strongly recommended to remove those files.

From the root directory of your installation, remove the following files:

  • CHANGELOG.txt
  • COPYRIGHT.txt
  • INSTALL.mysql.txt
  • INSTALL.pgsql.txt
  • INSTALL.txt
  • LICENSE.txt
  • MAINTAINERS.txt
  • UPGRADE.txt

Whether or not these files contains security information (like Drupal version for example), the all contain a CVS header that gives a really precise version information on your Drupal installation.

You can gain performances by disabling unecessary contributed modules from your live site. Here is a list of modules you can safely disable on a production site:

  • Devel
  • Devel generate
  • Devel node access
  • Performance Logging
  • Theme developer
  • Advanced help example
  • ImageCache UI
  • Views UI

Create a node (a page content type for example) with some extra information so that your visitors don't ever fall on the default 404 page not found.

Once this node is created:

  • Remember its node ID,
  • Go to Administer > Site configuration > Error reporting
  • Set Default 404 (not found) page to the node ID you just created
  • Save your settings

You can alternatively use the Search 404 module.

By default Drupal front page lists all nodes promoted to front page. We will often want the front page to display something different (a View, a panel, a node, etc.). To do so:

  • Go to Administer > Site configuration > Site Information
  • Set Default front page to the path of your View, Panel, node or anything.
  • Save your settings

Note that the default value for Default front page is node.

Considering the domain name drupal-check.org, we want to redirect all users accessing your website from http://drupal-check.org to http://www.drupal-check.org.

Edit .htaccess file from the root directory of your Drupal installation, find the <IfModule mod_rewrite.c> section and uncomment the following lines, replacing example.com with your own domain name, in this example drupal-check.org:

RewriteCond %{HTTP_HOST} ^drupal-check\.org$ [NC]
RewriteRule ^(.*)$ http://www.drupal-check.org/$1 [L,R=301]

More information is provided in the .htaccess file itself.

Sometimes, you need to enter email adresses in your content. Good examples are your About page, or even your Contact page. But this the better way to get spammed!

It is strongly recommened to install a module such as SpamSan:

  • Download and activate SpamSan module
  • Go to Site configuration > Input formats

For each of your input format do the following:

  • Click on Edit
  • Check that Hide email addresses is enabled and save
  • Click the Rearrange tab and set Hide email adresses with a super high weight (10 for example)
  • Click Save configuration

All email adresses are now spam protected.

It's highly recommended that you protect user with uid 1, i.e. the one who has life and death rights on your Drupal installation.

Disallow user 1 deletion:

Disable user 1:

  • Be sure to have at least one user (other than uid 1) that has the permission administer users from user module.
  • Login with this account (again other than uid 1)
  • Go to Administer > User management > Users
  • Edit user with uid == 1
  • Set Status to Blocked
  • Click Save

Now user 1 can't login to your website. No more risk for password discovery for this account.

Please note that you should check enabled modules code, sometimes they use user 1 to achieve some tasks. And this could break some modules features. So use with caution.

It is strongly recommended that you put your site off-line before performing any module/theme updates.

  • Go to Administer > Site configuration > Site maintenance
  • Set Site status to Off-line
  • Save your settings
  • Make a backup of your database and file structure
  • Upload new files to your server
  • As user 1, run http://www.example.com/update.php
  • Be sure to check that everything works fine after the update process
  • Go to Administer > Site configuration > Site maintenance
  • Set Site status to Online
  • Save your settings

Use full PHP tags

Write:

<?php print $foo; ?>

Don't write:

<? print $foo; ?>

Semicolons
Write:

<?php print $foo; ?>

Don't write:

<?php print $foo ?>

No closing tags
When writing a module or customizing your theme template.php, don't use PHP closing tag ?>.

Notice that at the bottom of the following snippet, there is no PHP closing:

<?php
// Your file starts here
function phptemplate_foo() {
  return $bar;
}
// Your file ends here
// Don't add PHP closing tag

By doing this you prevent PHP interpreter to stop and restart on the next source code file. This mean better performance.

There are some conventions when using contributed modules and/or themes. Most users would want to place them in modules and themes folders.

Don't ever do that! Never place modules or themes in these directories!

Everything added to a fresh Drupal install should go somewhere in the sites directory:

  • Contributed modules (from drupal.org) should go in sites/all/modules/contrib
  • Project custom modules should go in sites/all/modules/custom
  • Contributed themes (from drupal.org) should go in sites/all/themes/contrib
  • Project custom themes should go in sites/all/themes/custom

Note that if you have a multisite environment, you could place somes of the modules/themes:

  • in sites/default/..
  • or sites/example.com/..
  • or sites/anotherdomain.com/..
  • and so on..

... depending on your configuration.

Note that using a contrib and custom directory convention will help you find out which modules are contributed by the drupal community and which you have developped specifically for the website.

This will allow you to update more easily your websites as the only directory you will need to preserve will be sites/*.