Drupal 7

Use of external style sheets in Drupal 7

External style sheets can't be added in the themes .info file

In Drupal 7, drupal_add_css is used to add external style sheets. In this example, the minified Bootstrap's CSS is fetched.

 * Implements template_preprocess_html().
function subtheme_preprocess_html(&$variables) {
    array('type' => 'external'));

Also, externally hosted style sheets are never aggregated or compressed.

Create a custom commerce checkout pane

 * @module: 
 * Create a custom checkout pane for Drupal Commerce.

 * Implements hook_commerce_checkout_pane_info()
function MY_MODULE_pane_commerce_checkout_pane_info() {
  $panes = array();
  // Custom checkout pane.
  $panes['CUSTOM_MODULE_PANE_NAME'] = array(
    'title' =--> t('Shipping service'),
    'page' => 'shipping',
    'weight' => 11,
    'base' => 'CUSTOM_MODULE_PANE_NAME_pane',
    'fieldset' => TRUE,
  return $panes;

 * Implements base_settings_form().

Remove all CSS files except current theme files

Function is placed in template.php.

 * Implements hook_css_alter().
function MYTHEME_css_alter(&$css) {
  dpm('-- Before unset --');
  foreach ($css as $key => $value) {
    if ($value['group'] != CSS_THEME) {

  dpm('-- After unset --');

CSS_THEME is a constant defined in common.inc and refers to the group of theme CSS files

Create a subtheme Drupal 7

This example show the basics to create a subtheme in Drupal 7.

First of all, create a new directory in the sites theme folder to add the custom theme.

Create the .info file, aside the mandatory name and core properties, and the recommended desription property, add the base theme property.

The custom theme can be activated on the /admin/appearance page.

Create extra fields on nodes

Use hook_field_extra_fields to use the new fields with the manage display function. The content of the new field is built in hook_node_view.

Set weight on a module

 * Implements hook_install().
function MY_MODULE_install() {
  // Set module weight in order to invoke module after other modules.
  ->fields(array('weight' => 100))
  ->condition('name', 'MY_MODULE')

Create table by using hook_schema

 * Implements hook_schema().
function MYMODULE_schema() {
  $schema['MYTABLE'] = array(
    'description' => 'DESCRIPTION',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      'language' => array(
        'type' => 'varchar',
        'length' => 4,
        'not null' => TRUE,

Show one image on a node teaser

Hook_process_field can be used inside template.php to render only one image on a multiple image field.


Subscribe to RSS - Drupal 7