Tuesday, June 29, 2010

Checking whether a user has a specific role...

Drupal 6 makes it easy to check whether or not a user has a specific Access Privilege.  It is indeed used in most modules, as it is essential and yields much power and flexibility in use.

What Drupal 6 does however NOT make trivial, is to check whether or not a user has specific role ( a named role, e.g. "administrator", "authenticated", "member" - whatever roles you may use).
How-to
<?php  global $user;

 
// Check to see if $user has the administrator role.
 
if (in_array('administrator', array_values($user->roles))) {
   
// Do something.
 
}?>

Here we use the default full array that each user has as an attribute - the collected roles of the user!  Note that this array is keyed, and the role names (not unique!) are thus kept in the values of the keyed array!
Why is this useful?
Well, let's suppose that you have a list of nodes (a blog-style for example), and these nodes in the listing are authored by different users - different users with different roles indeed!  Using the technique above, will enable you to theme the blog-entries of each user-role, making it easier to visually tell them apart on-page for the reader of the blog.

Thursday, June 17, 2010

Accelerate drupal performance

Update: There are several modules available to Drupal 6 for caching pages, thus enhancing load-performance of your Drupal website.  I've personally tried the CacheRouter below, and have found this excellent article outlining an alternative - Boost & DB Maintenance.

Dedicated hosting setup
I just installed APC - the PHP opcode cache engine - on our server, and configured it as described in

APC Drupal Performance tuning

APC enabling & optimization makes Drupal websites SCREAM!

Shared hosting setup
If you are running your Drupal website on shared hosting, and don't have the option if APC ( as it is a server-wide installation, and not single-site!) then you might want to check out the Boost/DB Maintenance modules, or the one below...

CacheRouter Module
Update: I've tried this module on a website featuring AJAX loading of node content (through Views 2 Module), and experiencing rather high load-times with this combination.  Maybe Views 2 Cache needs to be enabled for the same content, maybe not.  I will test this later on - And please - if you test, then comment here... Thanks!

Tuesday, June 8, 2010

Page example module - Modules and menu routes

I was specifically searching for examples of how to create modules that present their content when called
through specific URL's in a Drupal CMS website. I came upon this example, and it looks quite simple and useable...

Module menu router example



Monday, June 7, 2010

Theming the "not-so-apparent" parts of Drupal content

An excerpt from the above-linked article/resource:
"You can create theme hooks for modules you did not write
When I wanted to theme the comments form the first thing I did was looking at comment.module. There I found a call like this:

drupal_get_form('comment_form', $edit, $title) 


Ok, I had a comment_form function that I could theme according to Drupal Form API, then I started looking for theme_comment_form but there was none.
How to override a theme hook that does not exist? Just as you do with your own modules, you just need to know the code involved and, this is important, you can include a hook_theme function in your template.php file. This is what I did for Woodpig:

function woodpig_theme() {
return array(
'comment_form' => array(
'arguments' => array('form' => array()),
),
);


The function starts with the theme name and the theme hook takes the form argument because it's a that's what drupal_get_form requires. Then I can write my own woodpig_comment_form($form) function."

Sunday, June 6, 2010

Nodes - Hooking into them, loading and saving with new overridden values

Check out this code-centric article on using hook_nodeapi in Drupal 6.  There are good code-examples and the section "Caveats" is especially worthwhile.

Friday, June 4, 2010

Drupal - How does it work...And how do I make it work for me?

Jeff Eaton presents Drupal to the developer.  He outlines the process of making a web-request, using index.php-controller as the stepping-stone, and how to hook into the Drupal-system.  "Hooking" is Drupals way of modifying functionality, and hooks are primarily implemented inside Modules.

Jeff does an amazing job of keeping it simple to understand, and reveals the processes that power Drupal enough to get you more than started coding your own Drupal Modules.

Wednesday, June 2, 2010

Node: Body outputs entire full node, instead of Body field

The problem is not, as the linked article here suggests, limited to Views representations of Node-body content.  The problem exists in any Node or Page template that is rendered to your website.

The problem arises from the fact that templating BOTH loads a Node, and then runs the "node-alter" routine on it - affecting the Node Body field specifically, to also include contents of CCK fields in that Node.

The problem-code
The problem arises when using this variable-call in a page-template (*.tpl.php) file:
print $node->body;
The solution-code
The solution is to replace the code above with this code instead:
print $node->content['body']['#value'];

Tuesday, June 1, 2010

Fast Install Drupal... DRUSH

DRUpal SHell (Drush) is without a doubt the fastest way of installing Drupal and other modules - the ones you ALWAYS use, the ones that you use for every Drupal installation...

Love thy terminal!
Drush is however a pure terminal (CLI) tool, and as such, you must (!!!) invoke and use it from the terminal... The most useful commands are:
  • "drush -h" - Get Drush help, see all of the things you can do through Drush!
  • "drush dl" - DownLoad (dl) Drupal in it's newest stable version
  • "drush dl [module] [module] [module]... [module]" - DownLoad the modules that you CRAVE!  This corresponds to downloading them from the Drupal Project's Website, and then FTP'ing them to the correct "/sites/all/modules" sub-directory of your Drupal installation
  • "drush en ...[module] [module] [module]... [module]" - Enable the modules that you just downloaded.  This corresponds to Enabling the modules through the Drupal Module Manager on your Drupal website
Install!
Installation of Drush is apparent from its project-page, but here is the specific procedure for Mac OS X - Also for those not quite comfortable with the Mac OS X Terminal Application.