home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (mimemail.theme.inc)

This example Drupal source code file (mimemail.theme.inc) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, css, file, function, if, keep_nextone, matches, null, path, php, return, string, style, theme

The mimemail.theme.inc Drupal example source code

<?php // $Id: mimemail.theme.inc,v 1.11.2.3 2011/01/18 17:26:07 sgabe Exp $

function mimemail_theme_theme() {
  $path = drupal_get_path('module', 'mimemail') .'/theme';

  return array(
    'mimemail_message' => array(
      'arguments' => array('subject' => NULL, 'body' => NULL, 'mailkey' => NULL),
      'template' => 'mimemail-message',
      'pattern' => 'mimemail_message__',
      'file' => 'mimemail.theme.inc',
      'path' => $path,
    )
  );
}

/**
 * A preprocess function for theme('mimemail_message').
 *
 * The $variables array initially contains the following arguments:
 * - $subject: The message subject
 * - $body:  The message body
 * - $mailkey:  The mailkey associated with the message
 *
 * See includes/mimemail.tpl.php for additional variables
 */
function template_preprocess_mimemail_message(&$variables) {
  $theme = variable_get('theme_default', NULL);

  // Fetch the theme for the current domain.
  if (module_exists('domain_theme')) {
    // Assign the selected theme, based on the active domain.
    global $_domain;
    $domain_theme = domain_theme_lookup($_domain['domain_id']);
    // The above returns -1 on failure.
    if ($domain_theme != -1) {
      $theme = $domain_theme['theme'];
    }
  }

  $sitestyle = variable_get('mimemail_sitestyle', 1);
  $mailstyle = drupal_get_path('theme', $theme) .'/mail.css';

  // Check for the existence of a mail.css file in the current theme folder
  if (@file_exists($mailstyle)) {
    $styles = $mailstyle;
  }
  // If no mail.css was found and the site style sheets including is enabled,
  // gather all style sheets and embed a version of all style definitions.
  elseif ($sitestyle) {
    $styles = preg_replace('|<link.*href="'. base_path()
                          .'([^"?]*)[?"].*|', '\1', drupal_get_css());
  }

  // Process each style sheet
  $css = '';
  foreach (explode("\n", $styles) as $style) {
    if (!empty($style) && @file_exists($style)) {
      $css .= @file_get_contents($style);
    }
  }

  // Regexp to match comment blocks.
  $comment     = '/\*[^*]*\*+(?:[^/*][^*]*\*+)*/';
  // Regexp to match double quoted strings.
  $double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
  // Regexp to match single quoted strings.
  $single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";

  // Perform some safe CSS optimizations. (derived from core CSS aggregation)
  $css = preg_replace_callback(
    "<$double_quot|$single_quot|$comment>Sus",  // Match all comment blocks along
    "_mimemail_process_comment",                // with double/single quoted strings
    $css);                                      // and feed them to _mimemail_process_comment().
  $css = preg_replace(
    '<\s*([@{}:;,]|\)\s|\s\()\s*[^\n\S]>S',  // Remove whitespace around separators,
    '\1',                                    // but keep space around parentheses
    $css);                                   // and new lines between definitions.

  // End the file with a new line.
  $css .= "\n";

  // Wordwrap to adhere to RFC821
  $css = wordwrap($css, 700);
  $variables['css'] = $css;

   // Process mailkey to be a proper CSS class.
  $variables['mailkey'] = 'mail-'. str_replace('_', '-', $variables['mailkey']);
}

/**
 * Process comment blocks. (derived from core CSS aggregation)
 *
 * This is the callback function for the preg_replace_callback()
 * used in drupal_load_stylesheet_content(). Support for comment
 * hacks is implemented here.
 */
function _mimemail_process_comment($matches) {
  static $keep_nextone = FALSE;
  // Quoted string, keep it.
  if ($matches[0][0] == "'" || $matches[0][0] == '"') {
    return $matches[0];
  }
  // End of IE-Mac hack, keep it.
  if ($keep_nextone) {
    $keep_nextone = FALSE;
    return $matches[0];
  }
  switch (strrpos($matches[0], '\\')) {
    case FALSE :
      // No backslash, strip it.
      return '';
    case drupal_strlen($matches[0])-3 :
      // Ends with \*/ so is a multi line IE-Mac hack, keep the next one also.
      $keep_nextone = TRUE;
      return '/*_\*/';
    default :
      // Single line IE-Mac hack.
      return '/*\_*/';
  }
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal mimemail.theme.inc source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.