background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London's picture

If you're running a multi-site Drupal configuration, it's helpful to know which settings.php is being loaded. This information may be useful to your module or theme, or for troubleshooting purposes. For instance, you might be sharing the same theme across a few sites, but you'd like to modify the theme for one of your sites. Sharing themes (and modules) is capable in a multi-site configuration by placing your themes and modules in the [sites/all] folder, while defining a directory and settings.php file for each site installation in [sites]. To be able to tell which settings.php is being loaded, you could add the following code to each settings.php file you define:

<?php
$exploded
= explode("/", __FILE__);
$conf['multi_site_hostname'] = $exploded[count($exploded)-2];
?>

For example, you could pass this information to your page.tpl theme file:

<?php
// defining a preprocess_page function, placed in my template.php file:
function MYTHEME_preprocess_page(&$variables) {
 
// example: pass the configuration directly to the page.tpl.php:
 
$variables['multi_site_hostname'] = variable_get('multi_site_hostname',NULL);

 
// another example:
 
switch (variable_get('multi_site_hostname',NULL)) {
    case
'thedrupalblog.erl.dev':
    case
'ericlondon.com':
     
$variables['siteHostName'] = 'ericlondon.com';
      break;
    default:
     
$variables['siteHostName'] = 'default';
      break;
  }

}
?>

A recent task that came my way way to create the functionality to capture email addresses using a simple subscription form. Since email is not a reliable means of capturing data, I used CCK to create nodes to capture the data and a view to show the results. First, I create a CCK node type called "email_subscription" which did not have a body or any custom fields, just the title for email address. I then created a view that shows the nodes in a table. Here's the module I created that generates a block module that contains the CCK node form. The block uses the CCK node form to capture the data. When the node is created, it uses the nodeapi hook to send a conformation email. There is also a function to validate the user's email address.

<?php
function MYMODULE_subscribe_block($op='list', $delta=0) {
 
// listing of blocks, such as on the admin/block page
 
if ($op == "list") {
   
$block[0]["info"] = t('Subscribe Block');
    return
$block;
  } else if (
$op == 'view') {
   
$block['subject'] = 'Subscribe';
   
$block['content'] = _MYMODULE_subscribe_block_main();
    return
$block;
  }
}

function
_MYMODULE_subscribe_block_main() {
 
// show the cck node form
 
return drupal_get_form('email_subscription_node_form',
    array(
     
'type' => 'email_subscription',
     
'uid' => $GLOBALS['user']->uid,
     
'name' => $GLOBALS['user']->name,
    )
  );
}

function
MYMODULE_subscribe_form_alter($form_id, &$form) {
  if (
$form_id == 'email_subscription_node_form') {
   
// remove preview button
   
unset($form['preview']);

   
// add custom validation
   
$form['#validate'] = array_merge(array('_MYMODULE_subscribe_node_form_validate' => array()), $form['#validate']);
  }
}

function
_MYMODULE_subscribe_node_form_validate($form_id, $form_values, $form) {
 
// validate title as email address
 
if (!valid_email_address($form_values['title'])) form_set_error('title', 'Email is not valid.');

 
// get list of already submitted emails
 
$sql = "select title from {node} where type = 'email_subscription'";

 
// if editing an existing node:
 
if ($form_values['nid']) $sql .= " and title!='" . db_escape_string($form_values['title']) . "'";

 
$resource = db_query($sql);
 
$results = array();
  while (
$row = db_fetch_array($resource)) $results[] = $row['title'];

 
// ensure email address has not already been subscribed
 
if (in_array($form_values['title'],$results)) form_set_error('title', 'Email has already been subscribed.');

}

function
MYMODULE_subscribe_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if (
$node->type == 'email_subscription' && $op=='insert') {
   
// send email to user
   
mail($node->title, variable_get('site_name') . ' Subscription', 'Thank you for subscribing!', "From: " . variable_get('site_mail'));
  }
}
?>

Here's a code snippet to show you how to create an admin settings page for your module. In this example, I'll create a form that allows the user to enable/disable checkboxes for each node type. This might be useful if you wanted to modify the functionality of a node and allow the user to choose which nodes.

<?php
function MYMODULE_menu() {
 
$items = array();
 
// ...code...
 
$items['admin/settings/MYMODULE'] = array(
   
'title' => t('MY TITLE'),
   
'description' => t('MY DESCRIPTION'),
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('MYMODULE_admin_settings'),
   
'access arguments' => array('administer site configuration'),
  );
 
// ...code...
 
return $items;
}

function
MYMODULE_admin_settings() {
 
$form = array();

 
// get a list of node types
 
$nodeTypes = node_get_types();
   
 
// loop through node types and create an array of options
 
$options = array();
  foreach (
$nodeTypes as $k => $v) {
   
$options[$k] = $v->name;   
  }
   
 
// add form input element
 
$form['MYMODULE_enabled_node_types'] = array(
   
'#type' => 'checkboxes',
   
'#title' => t('Enabled node types'),
   
'#options' => $options,
   
'#default_value' => variable_get('MYMODULE_enabled_node_types', array()),
  );
   
  return
system_settings_form($form);
}
?>

Screenshot:

Here is a code snippet to redirect the user back and forth from HTTP to HTTPS. This example shows how to transfer node/add forms in SSL.

Adding code to the menu hook to ensure it's executed before anything else:

<?php
function MYMODULE_menu() {
 
// set a variable to keep track if something in the page should be sent in ssl
 
variable_set('MYMODULE_ssl', FALSE);
 
// ...code...
 
$items = array();
 
// ...code...
 
return $items;
}
?>

Adding code to the form_alter hook, to check if certain forms should be sent in SSL

<?php
function MYMODULE_form_alter($form_id, &$form) {
 
// ...code...
  // check if form should be sent in ssl
 
if (substr($form_id, -10) == '_node_form') {
   
variable_set('MYMODULE_ssl', TRUE);
  }
 
// ...code...
}
?>

The guts of the code are added to a function that's called from the theme:

<?php
function _MYMODULE_check_ssl($enableSSL = FALSE, $messages = NULL) {

 
// check for ssl enabled
 
$sslOn = ($_SERVER['HTTPS'] == 'on' ? TRUE : FALSE);
   
 
// check if the page needs to be redirected
 
if ($sslOn != $enableSSL) {
       
   
// if there are drupal messages, set them to the session.
    // when the page is redirected, the messages will be returned to the theme
   
if (strlen($messages)) variable_set('MYMODULE_ssl_messages', $messages);
       
   
// create a new URL
   
$newURL = 'http' . ($sslOn ? '' : 's') . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
       
   
// redirect user
    // drupal_goto?
   
header("Location: $newURL");
    die;
  }
   
 
// check if there is a message stored in the session
 
$messages = variable_get('MYMODULE_ssl_messages', NULL);
  if (
strlen($messages)) {
       
   
// unset message in session
   
variable_set('MYMODULE_ssl_messages', NULL);
       
   
// return message to theme
   
return $messages;
  }

}
?>

Lastly, I added a line to the top of the page.tpl.php file in my theme:

<?php
$messages
.= _MYMODULE_check_ssl( variable_get('MYMODULE_ssl', FALSE), $messages );
?>

Also, check out this page to see how to change links from http to https using jQuery.

Syndicate content