Tips and Tricks

By shane
2011-07-18 09:08
1 comment

I frequently run into a common situation that I am sure many Drupal developers run into. For example, lets say I am printing out some text that has a number followed by a noun. An example might be 10 products. However, often times the number that you print out will be a variable. In this case everything is fine until you run into this situation 1 products. While this is more of an annoyance than anything, in the spirit of being grammatically correct, what it should say is 1 product.

By adam
2011-07-12 14:33
3 comments

This is a guest post from Adam, my Co-founder at BEgINr Media

Here is a simple way to make a background image resize correctly on different screen resolutions.

The image that I used was originally sized 1011px X 249px. For this tutorial I broke the image
into three different sections. See diagram below for more information.

By shane
2011-07-12 10:39
5 comments

Occasionally you may need to hide a page title in Drupal 6 for a number of reasons. This can be especially useful if you are theming a node page and want to display the page title somewhere else in the node content. I often need to use this when theming Ubercart product pages. It is really very simple to do, just add this function to your template.php file inside your theme.

By shane
2011-06-28 20:50
4 comments

There are many times when you will be developing a module that you will need to confirm an action. An example might include before deleting a piece of content, or changing the status of something. Here is a very simple example of how to create a confirmation form in a Drupal 6 module.

By shane
2011-06-27 15:18
0 comments

Here are two ways to get a list of users in Drupal 6. The first is to get a list of users in a specific role.

$sql = "SELECT u.uid, u.name FROM {users} u
   INNER JOIN {users_roles} ur ON u.uid = ur.uid
   WHERE ur.rid = %d";
 
//replace the 3 with whatever role id you are looking for
$results = db_query($sql, 3);
 
while ($result = db_fetch_object($results)) {
   //do whatever you need to with the results
 }
By shane
2011-06-27 11:52
1 comment

Ran into a situation earlier today where I needed to validate a Drupal username and email address entered into a custom form that would create a Drupal 6 User account (see http://codekarate.com/content/create-user-account-drupal-6-programmatically for more information on the submit handler of the form). There are easy to use Drupal functions to validate the username and email addresses. See the examples below:

//check if this is a valid Drupal username
user_validate_name('USERNAME');
 
//check if this is a valid email address
user_validate_mail('<a href="mailto:test@test.com">test@test.com</a>');
By shane
2011-06-16 15:49
1 comment

Here is a quick issue I ran into earlier today while working on a Drupal 6 website that I wanted to share. This one will be short and sweet.

Lets say you want to create a block in a Drupal module that will only ever be on a users profile page, or perhaps you have another situation where you are adding some code for display on a users profile page. If you are trying to get the Drupal user object of the currently viewed user, here is a quick way to do it.

$user = menu_get_object('user_uid_optional');
By shane
2011-06-13 17:25
3 comments

Typically when performing a database insert in Drupal 6 you use drupal_write_record. However, there is an instance where this does not work as expected. If you are creating a module that has a database table (defined by hook_schema), and you need to populate that table with data from within your install process, you might run into a problem. Lets look at an example.

Lets say you have a simple install file that looks like this:

By shane
2011-06-08 15:52
11 comments

Looking to do this in Drupal 7? See my updated post on how to Create a user account in Drupal 7 programmatically.

There have been a few situations in which I needed to create a user on a Drupal site based on some action. There are many other situations where this might be necessary so I put together a really quick example of how it can work. The code I have below is based heavily on the Ubercart code that creates new users when they purchase a product on an Ubercart powered Drupal site.

 
By shane
2011-06-06 13:21
0 comments

Adding Javascript to a Drupal 6 Module is a pretty simple process. This post can serve as a very simple primer to get you started.

It starts with implementing hook_init() in your .module file.

/**
 * Implements hook_init().
 */
function MYMODULE_init() {
  drupal_add_js(drupal_get_path('module', 'MYMODULE') .'/MYMODULE.js');
}

Then you will need to create a Javascript file called MYMODULE.js in your module folder. Drupal by default comes with the JQuery library. Your Javascript file should look similar to the one below:

By shane
2011-05-25 23:26
2 comments

As I write this, one of the websites I frequent for information is currently down. It is not uncommon for a website (or the server the website is on) to have a problem. I am not sure if the site is going through planned maintenance or if this is unexpected downtime, however it reminded me of a few things.

By shane
2011-05-12 12:58
3 comments

If you need to alter a CCK form or perhaps a form being constructed by another module, sometimes using hook_form_alter() isn't enough. Sometimes the form has not yet been fully constructed and you need to modify a part of the form that is not yet available for you to touch.

This is where the After Build function comes in handy. It allows you to modify the form after the form is built and all the form_alter's have been run. Here is a simple code snippet on how to begin using the Drupal form after build function.

By shane
2011-05-05 13:44
5 comments

One of the things I am very interested in as of late, is coding efficiency. What do you do to increase your coding efficiency? Do you use specific tools or tricks?

I am going to go ahead and give away some of my secrets in hopes of helping others (and maybe to pick up some additional tricks).

My Development Environment

By shane
2011-04-05 11:12
3 comments

So I finally got around to looking into module development with Drupal 7. In fact, I really started to get my hands on Drupal 7 for the first time. I was very pleasantly surprised with the admin overlay and the entire usability of Drupal 7.

While building a module with Features, Views, and Fields (most of CCK was rolled up into Drupal 7 core with some changes and is now called Fields), I quickly ran into a problem. I wanted to do a simple query of node data. In Drupal 6 I would have accomplished this with a simple select statement. Something like:

 
By shane
2011-03-24 15:22
4 comments

When I first began working with forms in Drupal 6 I dove right in without reading all the documentation available on Drupal.org. However, I soon realized using drupal_goto() inside my form's submit handler was not the correct way to do things. This is just a little bit of information on how to set form redirects correctly from in the main form function, the submit function, and even a form alter function.

From the main form function:

By shane
2011-03-16 22:00
0 comments

So one day I wake up to a phone call from a client saying their website was broken. After looking into it, I was able to determine that the website itself was working fine, however there were now red error messages showing up on almost every page. After further looking, they were not really errors, they were PHP notices. The notices sprung up during server upgrades that migrated from PHP 5.2 to PHP 5.3. At the time, we ended up just changing the PHP error setting to no longer display notices, and the crises was averted. however looking back now we could have done it this way.

By shane
2011-03-16 00:03
1 comment

I recently had a small issue where my Drupal 6 node object did not have all the current and correct data because the node that was stored in the Drupal node cache was out of date. This is not something I have run into very often (I think only one other time before tonight), however if you run into the same problem then here is the solution:

Rather than just using:

Subscribe to Tips and Tricks