Essential Drupal Tips for Drupal 4.6.5

I've been running Drupal for a few months and I've learned a few tips for interacting with Drupal, as well as a few "gotchas" present in Drupal. I'd like to share them with you in this article. Unless mentioned otherwise, everything in this article applies to Drupal 4.6.5.

Always use the cache! I cannot emphasise this enough. If one has a few modules loaded, the number of database queries can easily go into the dozens or higher. You can verify this yourself by loading the devel module and viewing the database queries that are made. When reading from a cached page, however, the number of queries made drops to about 2 or 3. This reduces load on the database and causes the page to display much quicker.

Note that cached pages are only displayed for anonymous users. Logged in users always get a dynamic page generated for them. Also, creating/modifying a node or a block causes the entire cache to be wiped. This can be a handy trick for quickly reducing the size of the database when going to make a backup. (You are making backups, right?)

Be careful about uploading HTML files as attachments to nodes. One annoying thing about Drupal is that it tries to second guess what you upload. In the file_check_upload() function found in includes/file.php, Drupal checks to see if the file contains text or ends in an obvious extension. If it finds that to be the case, it will append .txt to the filename before saving it to disk. This is very annoying if you want to attach HTML files, because clicking on the links to download this files will cause the HTML source to be displayed in your browser. My workaround for this was to comment out the offending lines in file_check_upload(). I'm sure there's another way to keep this from happening, though.

Never have leading or trailing slashes on path aliases. You'll know this has happened if you just submitted a new node and are greeted with a "file not found" message. For reasons unknown, Drupal's path module does not gracefully handle leading or trailing slashes when setting up a path alias. The workaround here is to not do that. If you find that you have put a leading or trailing slash into a path alias, you'll need to go into the content menu under /admin and click the "edit" link for that node.

Problems getting modules to work properly? Make sure that after putting a module into your modules/ directory that you did the following: 1) Ran any MySQL scripts. 2) Went into the modules menu and enabled it. 3) Went into the access control menu and gave yourself rights to manage that module's settings.

One strength of Drupal is that it's cool about having modules in subdirectories under the modules/ directory. It only scans through that directory when on the modules screen and stores the actual paths to file for each module in the database. This means that if you have many modules, you can group them by directory to eliminate clutter in the modules/ directory. However, make sure to reload the modules page (and submit the form there) after moving around physical files so that Drupal knows the new file locations.

Search index not being updated. Sometimes the search index stops being updated for reasons unknown. After ensuring that the cron facility is really being run, one way to "reset" the search index is changing the minimum word length to index. You can change it from 3 to 4 and back to 3 again. This will cause the entire site to be reindexed during cron runs.

Do not index more than 10 nodes per cron run! At least, don't do this if you are in ANY sort of shared hosting environment. When nodes are indexed during cron runs, the actual process involves reading each node, passing it through whatever input filters you might have, splitting up the result into words, and writing the results to the database. The filtering and splitting part is very CPU intensive. I used to index 100 nodes per cron run, and my webhost informed me that Drupal was using 100% of the CPU for several minutes at a time. Obviously this did not sit well with them. You may safely disregard this if you are running Drupal from your own machine and aren't worried about any speed issues.

Keep nodes under 50 K if possible. As previously mentioned, indexing a node requires a lot of CPU time so you want to limit the number of nodes indexed. However, that won't help if each node is 500 Kilobytes in size. The way to shrink down nodes is to use the book module and place individuals chapters/sections/whatever into child nodes. Here is an example of this. If you still want to make the original full-length item available to the user, I recommend zipping it up and attaching the file to the main load so the user can download it. File attachments aren't indexed. Smiling

You can easily determine what nodes are over 50 K by installing and running my sanity module.

Be careful about changing directories that file attachments are stored in. I learned this one the hard way. I was trying out Drupal's awesome multi-site hosting feature, and decided that I didn't want each site's files hanging out in /files, but rather to put each site's files in a subdirectory of /files. The idea did eventually work out, but along the way I ran into issues with userpics not being displayed anymore. Looking at the log showed the error "system/files not found" when trying to load the userpic. The root cause was that the users.picture field in the database pointed to the old location. The only fix for this is to go into MySQL and change the values manually.

That pretty much sums up the main "gotchas" that I discovered while working with Drupal. If you know of anything else that should be listed here, please let me know.

If you found this article useful, you might find these other resources of interest:

No votes yet
Your rating: None