Customize "submitted by" information based on node type

description

You can display different author information and still respect the global "display post information" settings.

In this example I wanted flexinode-2 items to be displayed with the usual submission info, but have every other type respect the "display post information" setting.

Note that the content type I created, "news", goes by its flexinode name, and not by what I named it. Here is an example snippet for a node.tpl.php:

Drupal 6/7 programming from an object-oriented perspective

Note: This page is about Drupal 7's (and prior versions') architecture. Drupal 8 changed a great deal and now uses actual OOP practices.

Drupal often gets criticized by those who look to its source code and API expecting to find certain familiar aspects of object-oriented programming (OOP). For example, the word "class" does not appear in Drupal code as often as some might expect. Some have viewed this as a deficiency.

Although Drupal does not make thorough use of the native OOP features of PHP, the Drupal code base and API does reflect some principles found in object-oriented programming and design. This article describes the architecture of Drupal from an OOP perspective, so that programmers familiar with OOP can evaluate Drupal in light of object-oriented design principles, and hopefully feel more at home in the Drupal code base. In the future, as both PHP and Drupal become more mature, Drupal may increasingly adopt the native OOP features of PHP.

Motivations for Current Design

Back as far as version 4.6 of Drupal, the decision was made not to use PHP's class construct. This decision was made for several reasons.

Customizing the login form

This seems to be a duplicate (albeit earlier) page

Please see http://drupal.org/node/350634 which is more recent and more accurate.


Description

These snippets allow you to override the default login layout using a custom user_login.tpl.php.

If you want to customize the full page layout, click through to the Customizing the login, registration and request password full page layout handbook page.

Step 1 of 2

In a text editor like notepad.exe, create a file called template.php using the the following snippet. If you already have a template.php file, simply add it to your existing one.

For use with Drupal 4.7.x and Drupal 5.x

<?php
 /**
   * This snippet catches the default login form and looks for an
   * user_login.tpl.php file in the theme folder
   */

function phptemplate_user_login($form) {
    return _phptemplate_callback('user_login', array('form' => $form));
}
?>

For use with Drupal 6.x

_phptemplate_callback is deprecated in Drupal 6 in favor of the theme registry and preprocess functions. See the Drupal 6 theme guide for more information.


<?php
function mytheme_theme(&$existing, $type, $theme, $path) {
$hooks['user_login'] = array(

How to connect to multiple databases within Drupal

Drupal can connect to different databases with elegance and ease!

Drupal 7

There are two methods for accessing secondary databases in Drupal 7.

Adding additional databases in your configuration

Preferably you would add your configuration in the settings.php file for your site, so that all modules can interact with the new database.

In your settings.php:

<?php
$databases = array();
$databases['default']['default'] = array(
  // Drupal's default credentials here.
  // This is where the Drupal core will store its data.
);
$databases['my_other_db']['default'] = array(
  // Your secondary database's credentials here.
  // You will be able to explicitly connect to this database from your modules.
);
?>

In your module:

<?php
// Use the database we set up earlier
db_set_active('my_other_db');

// Run some queries, process some data
// ...

// Go back to the default database,
// otherwise Drupal will not be able to access its own data later on.
db_set_active();
?>

Setting up databases on the fly

If your module will be alone in using the secondary database you can define the connection directly in your module:

<?php
  $other_database = array(
      'database' => 'databasename',
      'username' => 'username', // assuming this is necessary
      'password' => 'password', // assuming this is necessary
      'host' => 'localhost', // assumes localhost
      'driver' => 'mysql', // replace with your database driver
  );
  // replace 'YourDatabaseKey' with something that's unique to your module
  Database::addConnectionInfo('YourDatabaseKey', 'default', $other_database);
  db_set_active('YourDatabaseKey');

  // execute queries here

  db_set_active(); // without the paramater means set back to the default for the site
  drupal_set_message(t('The queries have been made.'));
?>

See the Database Abstraction Layer for a complete reference of all Drupal database abstraction functions for version 7.

Creating modules - a tutorial: Drupal 4.6/4.7

This tutorial describes how to create a module for Drupal 4.6 or 4.7. It is an update to the tutorial for Drupal 4.3. Please see comments there, also. Most of this tutorial is valid for Drupal 4.7 as well, but you should check the API documentation, as well as the documentation on how to update your modules from one version of Drupal to another.

Theming nodes by content type

Note: To do this in Drupal 7 please see Drupal 7 Template Suggestions.

The file node.tpl.php is used to theme certain types of nodes. This single file generically covers all Content Types and you can edit it to make changes across the board as detailed on the node.tpl.php handbook page. To theme individual content types in different ways, you need to create a file node-[type].tpl.php in your theme's folder, where [type] is the machine readable name of the content type, for each type you wish to theme. Some examples:

node-story.tpl.php
Themes only Story type nodes.
node-page.tpl.php
Themes only Page type nodes. Note that this is different from page.tpl.php which controls the layout of the entire page including header, sidebars and so on.
node-forum.tpl.php
Themes only Forum type nodes.
node-book.tpl.php
Themes only Book type nodes.

Pages

Subscribe with RSS Subscribe to RSS - Drupal 4.7.x