Showing posts with label Admin. Show all posts
Showing posts with label Admin. 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.

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!

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.

Monday, May 31, 2010

CCK - Compound Fields - Image + Rich Text Caption + Taxonomy...

Here is a great article, outlining how to create your very own compound fields for use in CCK. The article EVEN (!!!) supplies you with two concrete implementations of modules, as added bonus...
- Image + Rich Text Caption + Taxonomy
- Modified User-page fields

You may download these two modules from the bottom of the same page, as linked to above...

Taxonomy, Vocabularies, Terms, and their uses

When using Taxonomy you are quickly faced with the daunting possibility to "view Taxonomies as you need to"... (in no particular order):
  • Classical categorization; As stated in the Drupal docs, where a single taxonomy corresponds to the animal kingdom classification into groups such as "Mammal", "Fish", "Bird" etc.
  • Used in code-decisions; Use taxonomy and terms to indicated relations between content - For example when presenting a page-view as a collation of nodes (left/right, three-column, multirow... whatever collation). Here you may need a taxonomy named "Page-Position" with the terms ("Left", "Center", "Right" etc.), in order to create a varied but manageable website for your administrators/editors and users.
  • Multiple node-representations at once; Used to view a node in several different contexts on your website. You may for example choose to view a piece of news in a teaser-block on the frontpage, while simultaneously presenting it on a news-archive page in full view-mode (teaser+body+etc.). This is also useful in the case where you selectively want to use nodes in RSS-feeds - Create a taxonomy for RSS, and you're set!
Do note that taxonomies are all capable of being sub-ordinate to other taxonomies, and as such they offer the ability to group content in sub-ordinate relations.

Sunday, May 30, 2010

Themes per content-type, and not only per node ID

This addresses the issue of ONLY having the possibility of theming nodes per node-ID, e.g:
"page-node-101.tpl-php"

In order to overcome this, and provide theming for each content-type (Defaults are of course "page", "story" in Drupal 6) that you may have created using CCK, you should look to modifying the code of the "theme_preprocess_page" function in your main "template.php"-file of your chosen theme.

Code to include in theme_preprocess_page
Here is the code to include...

// Add per content type pages
if (isset($vars['node'])) {
// Add template naming suggestion. It should alway use hyphens.
// If node type is "custom_news", it will pickup "page-custom-news.tpl.php".
if($vars['node']) $prefixName="page-node-";
else $prefixName="page-";
$vars['template_files'][] = $prefixName. str_replace('_', '-', $vars['node']->type);
}

And, as you will see, it gives you access to using two new template-names for your content-type:
- For views etc. node-listings: page-CONTENTTYPE.tpl.php
- For node-details views: page-node-CONTENTTYPE.tpl.php

Monday, May 24, 2010

Pretty URL's - Automatically

Pretty URL's in Drupal 6 are easily introduced at ANY TIME in your development cycle, using the Pathauto Module.

There is however ONE tiny fly in this ointment - In order to enable and use Pathauto, then you will have to introduce the Drupal Rewrite Rules into your web-hosts ".htaccess"-file, or directly into the Apache Directory Rewrite Rules for your website installation. ".htaccess"-files are the most commonly available option, on shared hosting anyway, and you then will just need to download the Drupal installer (if you don't already have this on-disk), and copy the very same ".htaccess"-file to the aforementioned location on your web-hosting.

NB! The Mac OS X Finder and Windows File Explorer (and their likes) may not show you the .htaccess-file, as it is a hidden file (prefixed with ".").
In these cases you will need to view your Drupal-downloaded installation (unzipped), directly in your FTP-application, and make sure that you are viewing all files, even the invisible or hidden ones.

The very last setting...
You need to make one very last setting in your Drupal installation; Visit the page at "/admin/settings/clean-urls" (being logged in as the admin-user), and make sure to enable Clean URLs!

HEEELP!!! Drupal WSOD problem... Why and How to recover

Remember the times when you have seen Bill Gates present a Windows feature to the world, and it all suddenly ends in the BSOD (Blue Screen Of Death)? This kind of problem probably gets a couple of MS-engineers fired ASAP.

Drupal has an equivalent to BSOD - The White Screen Of Death (WSOD) Read on to avoid "getting the boot" from your boss or customer for your Drupal website suddenly going white, and not showing any HTML or other code on inspection!

WSOD - Causes
WSOD in Drupal is most likely to bug you when:
  • You've installed (uploaded and activated!) one or several Modules that have bugs in them, or that they together act erroneously (module madness)
  • You've done something terribly wrong in one of the template/theme files
The first case is the most common, also as you will have guessed that the latter case is easily overcome by simply reverting your own code back to functioning state.

Solution to WSOD - Module madness
Drupal keeps a continuous status of which modules you have installed and enabled for your Drupal installation. These informations are collected together in the database of your Drupal installation. So, using your favorite database-editor (e.g. PhpMyadmin for MySQL), you should take the time to locate the table named "_system" (e.g. "drup_system"). In here, you will observe (upon inspection) that it has two data-columns of special significance; "name" and "status". "name" indicates, as you might've guessed - the Module Name, while the status is enabled/disabled (1/0) for each of these modules.
You will therefore need to disable any of the more recent modules that you've installed, and you should be rid of WSOD once more.
Disabling modules is accomplished by - You guessed it - using that same database-editor (PhpMyadmin) and setting the "status" of the suspected Modules to the value of "0" (ZERO).

Saturday, May 22, 2010

Developer modules

Just like you will need the essential modules, as the previous post outlines - then you, as the developer and designer of a Drupal website, will also benefit greatly from these...
  1. Devel Module+Devel Themer - Visualize content data, which template (theme) files that apply to a certain page or content-view, and use it to modify your pages and content!
  2. SimpleMenu* - Provide a general menu, always displayed at the top of your website - No need to reserve a block-position for this menu - it does it all!
  3. Zen-theme - A fasttrack to theming as you can literally "point-n-click" to create your own theme, and THEN specialize this theme to your liking
* SimpleMenu is great, but it has a conflict with the Devel Themer module (specifically the Themer module!), so use it with care, and when using for the admin-user you can switch Devel Themer on and off in your Modules Control Panel on your website.

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...