Pages

Wednesday, August 29, 2012

Drupal Website Building Tips - Part 1


Introduction

Drupal is one of the most widely used website servers in the world. It is even used by the US White House. It is free and open source and can be customised to many different uses by installing additional modules. This page is a cookbook of how I set up a Drupal site and may be helpful for getting an initial site customized. I am assuming you have one (or multiple) Drupal installations running using these instructions.

Initial user setup

  • It is not a good idea to give anonymous users access to the site or create accounts until you have the site completely set up and are ready to publish. There are people trolling the web looking for new setups, who will create an account and start installing modules faster than you can. Turn off anonymous user accounts:
Drupal -> Administer -> User management -> User settings -> Only site administrators can create new user accounts.
  • Create an administrator user role:
Drupal -> Administer -> User management -> Roles -> Add role -> administrator -> edit permissions -> check all
  • Create a new user who will be an administrator
Drupal -> Administer -> User management -> Users -> Add user -> Adminuser -> roles -> administrator
Use this user as your everyday administrator, saving the user you created at installation as the superuser

Create a Welcome page

  • Create a Welcome page:
Drupal -> Create content -> Page
You can choose where the display will end by adding the tag at the end:
<!--break-->
Note: If you don't put this tag at the end, the display of the page (known as the "teaser") will be truncated to 300 characters by default. This behavior can be changed:
Drupal -> Administer -> Content management -> Post Settings -> Length of trimmed posts: Unlimited

Change your default logo

  • The ideal size for a logo is about 110 x 100 pixels. A transparent background for the logo is desirable. (You can use Gimp to create an alpha transparency layer for any photo. See theseinstructions.) You can use Gimp or Gwenview to resize the image.
  • The default logo is specific to the Theme you are using. If you are using the default Garland theme, for example, change the logo:
Drupal -> Administer -> Themes -> Garland -> configure -> Logo image settings -> Use the default logo: unticked ->
Upload logo image: your_own_customised_logo.png

Create a new menu in the left sidebar

When most users create new content (such as a Page or Story), they place it in the Primary Links or Secondary Links menus. At installation, Primary Links and Secondary Links appear at the top of the page, which looks nice. But what if you want a new menu, that appears (for example) only on the left sidebar?
  • Create a new menu:
Drupal -> Administer -> Site building -> Menus -> Add menu ->
Menu name: mynewmenu -> Title: My New Menu -> Description: This is a custom menu I created. ->
  • Place the new menu in the left sidebar:
Drupal -> Administer -> Site building -> Blocks -> Disabled ->
My New Menu -> Left sidebar -> Save blocks
  • Add items to the menu (these can be external links or links to internal pages):
Drupal -> Administer -> Site building -> Menus ->
My New Menu -> Add item
Note that you can also create new content and add it to this new menu at any time.

Increase PHP memory

  • Increase PHP memory (or you will get a "memory exhausted" error). (Use the gedit text editor instead of kate if using Ubuntu instead of Kubuntu.):
sudo kate /etc/drupal/6/sites/mysite_x/settings.php
Add this line at the end:
ini_set('memory_limit', '96M');
I used to use 32M, but 96M helps with graphics.

Increase uploaded file size limits

The PHP scripting language is used for uploads. Absolute upload limits for the Apache webserver are set in a PHP configuration file and must be changed there.
  • Your uploads are probably larger than the default upload limits of PHP (set at 2 Mb, or "2M", by default), so we will need to increase those. In the example below, I will change the upload limit to 100 Mb ("100M"). Two parameters must be changed in the php.ini configuration file in /etc/php5/apache2 (use the gedit text editor instead of kate if using Ubuntu instead of Kubuntu):
cd /etc/php5/apache2
sudo kate php.ini
  • Change:
post_max_size = 8M
to
post_max_size = 100M
  • Change:
upload_max_filesize = 2M
to
upload_max_filesize = 100M
  • Save the file and restart apache2:
sudo /etc/init.d/apache2 restart


Tuesday, August 28, 2012

How does the emailing system (drupal_mail) work in Drupal 6?


Drupal uses a reasonably powerful mechanism to create, prepare and send emails generated within the system. We maintain two modules related to emails in Drupal - Mail Merge and MailQ (Mail Queue) - and get the opportunity to work with the mail subsystem in Drupal. Here is a brief write-up on how the mail system works in Drupal.
The workflow when sending a mail with drupal_mail
1) Module calls drupal_mail
drupal_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE)
The params array should have all the necessary information related to the mail that is to be sent. Do note that the mail is yet to be prepared and that will happen only during hook_mail calls.
2) drupal_mail then calls
2.a) hook_mail of the module calling drupal_mail
hook_mail is to be used by the module to copy parameters to $message.
The following parameters are already mapped back into $message from drupal_mail
  $message = array(
    'id'       => $module .'_'. $key,
    'to'       => $to,
    'from'     => isset($from) ? $from : $default_from,
    'language' => $language,
    'params'   => $params,
    'subject'  => '',
    'body'     => array()
  );
Drupal mail also sets the following as default headers
  $headers = array(
    'MIME-Version'              => '1.0',
    'Content-Type'              => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
    'Content-Transfer-Encoding' => '8Bit',
    'X-Mailer'                  => 'Drupal'
  );
If default_from is present it is used to set the following headers as well. default_from is either the site_mail if set or from php.ini sendmail_from parameter
  $headers['From'] = $headers['Sender'] = $headers['Return-Path'] = $headers['Errors-To'] = $default_from;
  $message['headers'] = $headers;
2.b) hook_mail_alter across all modules
Other modules can then alter $message as required via the hook_mail_alter call.
3) If $send parameter of drupal_mail is not set to FALSE drupal_mail will call drupal_mail_send
3.a) drupal_mail_send calls drupal_mail_wrapper($message) if smtp_library is set and the module is present. This is how the different modules like mimemail or smtp mail hooks into the mailing system in Drupal.
drupal_mail_wrapper is expected to send out the mail and if smpt_library is not set then drupal_mail_sendwill try to send out the mail using the php mail function.
How MailQ works in Drupal by plugging into drupal_mail
MailQ is a module designed to allow drupal sites hosted on shared hosting servers distribute the email loads on the system across time to work around the hourly email limits typically set by the hosting providers.
MailQ is designed to catch all mails sent by drupal by setting up a drupal_mail_wrapper which will queue all mails during non-cron regular site operations and which will send them out during cron runs. MailQ sets the smtp_library to itself forcing drupal_mail_send to call the drupal_mail_wrapper from mailq during normal operations. Mailq drupal_mail_wrapper will then store all the messages into the database queue and then process these in batches during cron runs by calling drupal_mail_send

How to add dynamic login / logout link to your primary menu


The Drupal Menu System handles both the navigational system (visible menus and links) as well as the Drupal callbacks in the back end. The menu links listed on the header of a Drupal site is normally the primary menu or the secondary menu. These menus are sets of static links that you create via the Drupal admin interface. However sometimes it is useful to have a login / logout link in the primary or the secondary menu depending on whether the user is logged out or logged in. Here is how you add this.
Add the following piece of code to your preprocess_page hook in your module (modulename_preprocess_page()) or in your theme template.php (themename_preprocess_page())
  if ($user->uid != 0) {
    $new_links['account-link'] = array(
      'attributes' => array('title' => 'Account link'), 
      'href' => 'user', 
      'title' => 'My Account'
    );
    $new_links['logout-link'] = array(
      'attributes' => array('title' => 'Logout link'), 
      'href' => 'logout', 
      'title' => 'Logout'
    );
  } else {
    $new_links['login-link'] = array(
      'attributes' => array('title' => 'Login link'), 
      'href' => 'user', 
      'title' => 'Login'
    );
  }
  $vars['secondary_links'] = array_merge_recursive($vars['secondary_links'], $new_links);

The above snippet will add a 'My Account' and a 'Logout' link for logged in users and 'Login' link for anonymous users to the secondary_links. If instead you wanted to add the links to your primary links you can replace the last line with
  $vars['primary_links'] = array_merge_recursive($vars['primary_links'], $new_links);

You will also have to make sure that none of the caching mechanisms set up on your site caches this across roles.

How to create a custom view in Drupal 7


By default Drupal 7 has two types of views - The Full node view and the Teaser view. This has been the case for all previous versions of Drupal. However if your requirements are different, then you might have to create a custom view that is tailored to meet your specific needs. Check out how to create a simple custom “View mode” for nodes in Drupal 7.
Step1: Create a custom module
Step2: Create an additional view mode -’Grid Pane’ by simply implementing hook_entity_info_alter() in our custom module.
/** 
* Implements hook_entity_info_alter().
*/
function CustomModule_entity_info_alter(&$entity_info) {
  $entity_info['node']['view modes']['grid_pane'] = array(
    'label' => t('Grid Pane'),
    'custom settings' => TRUE,
  );
}
Step3: Now apply some theme and custom layout for the content. We can add a custom node.tpl.php template for this view mode for flexible theming and use standard hook_preprocess_node() function to control the variables available in the template.
To make Drupal use a different .tpl.php file for view mode we need to add a new template suggestion in hook_preprocess_node (). Copy the standard node.tpl.php for the content type you need and name it like “node--article--grid_pane.tpl.php”.
/**
* Implements hook_preprocess_node().
*/
function tdbcustom_preprocess_node(&$vars) {
  /*dpm(entity_get_info());*/
  if($vars['view_mode'] == 'grid_pane') {
    $vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__grid_pane';
  }
}
Step4: Install the custom module and we are done. Once we create a new view mode, we can go to Admin->Structure->content types->[Manage Display] page and select which fields to display format and additional options for fields, like which image style to use for image fields etc. For example:
Drupal