Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

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.

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.

Saturday, May 22, 2010

Developer documentation and examples...

The general Drupal API can be found at api.drupal.org

In addition to this, you will also benefit from some select free screencasts - These display real-world usage of Drupal and Modules to provide certain custom page and content views. I can personally recommend these two...
  • Drupal Videos
  • Drupal School
Both of the above are accessible through iTunes and the iTunes Store...

Additionally, you might also like to have a copy of one or all of the Drupal Cheat Sheets lying around - It tells you all about the default variables, such as the login-status of your visiting user, and such essentials...