tricks

Extreme Form Handling in Drupal

Via: AgileApproach Blog

Drupal Form-Handling support goes far beyond just the documented part of so-called Forms API. You can do pretty much anything with forms in Drupal and you can use/display the forms anywhere.

Here is an example. Let's assume we want to construct a custom node type with custom fields, using CCK. Then we want to display this form into some non-standard page. To further complicate things, let's assume we also want custom verification and processing routines.

And last but not least - we want to use full power of Drupal and write a minimal amount of code. Following is a snippet demonstrating key points to achieving this task (thorough understanding of Drupal is required):

How to Hack "Read More" in Drupal

As you know Drupal displays the "Read more" link among all other after-teaser links in the blog postings view. This is not a usual user-experience. Most users expect "read more" link to be right after the teaser part of the posting. It can get confusing, so following is a quick-n-dirty hack that can fix it for you:

Locate the piece of code in node.tpl.php where it reads like:

 <?php print $content ?>

Put the following right after it:

<?php
// Extract "read more" link from $links so we can display it separately.
if (preg_match('!<a[^>]+>'.t('Read more').'</a>!', $links, $match)) {
  $links = preg_replace('/<a.+?href.+?>'.t('Read more').'<\/a>/i', '', $links);
  $more = '<div align="right">'. $match[0] . '</div>';
  $more = str_replace ("Read more", "Read the rest of the posting...", $more);
}
else {
  $more = '<span class="readmore-fill"></span>';
}

if ($more) { print $more; }

?>

Basic PHP Logging

One of the most powerful ways to debug a web application is using logging; especially for a highly complex system like Drupal. PHP comes with a built-in logging function: error_log();. The exact behavior of this function depends on several configuration parameters.

We suggest that you log into standard Apache error log and create one log per virtual host. To achieve this, make sure that in php.ini you have "error_log = " directive commented-out, so that PHP does not log either in one specific file or syslog, and also make sure you have "log_errors = On". In addition, in httpd.conf (or wherever you have virtual hosts configured in Apache) for the virtual host configurations, make sure you have separate log files per a Virtual Host with a directive like: ErrorLog logs/buzzmonitor.log

The basic logger is not as powerful as the PEAR logger, but is much safer. If you use PEAR logger, you must be sure that anywhere your code will be installed, PEAR Log module will be installed, too. That is not a safe assumption, especially for Drupal developers that publish their code in open-source.

Drupal: Limit Roles in Users List

Pierre today needed a Drupal hack to limit the set of roles, members of which would be displayed on a Drupal users listing page. This is the listing that comes up when you access a URI like: http://example.com/profile. Of course, it took us less than 5 minutes to figure-out how to do it, since Drupal is so awesome. When we were done, we thought about it (yes, it was quicker to do than analyze) and it's a pretty useful hack. I mean, it should be a quite common need to only display, say, bloggers in the user listing but not expose admin user and other technical members.

I am too lazy to make a module of the hack, but here are simple steps of how to do the same on your Drupal install:

  1. Open modules/profile/profile.module file for editing
  2. Find function definition for theme_profile_listing($account, $fields = array())
  3. In the beginning of the function insert something like:
     //--- HACKED. 
      if ( !in_array("blogger", $account->roles ) ) return "";
      //--- End HACK
    

    where "blogger" is the role members of which you want to be displayed in the listing. If you have several such groups, you can do something like:

     //--- HACKED. 
      if ( !in_array("blogger", $account->roles )  
        && !in_array("editor", $account->roles )  ) return "";
      //--- End HACK
    

where you will now be displaying members of "blogger" and "editor" groups. You can keep adding groups like that until the code gets too ugly at which point you can ask yourself - why in the world do you need so many groups? or write a different code.

Have fun

Syndicate content