Adding a virtual site feature

Last modified: March 8, 2008 - 17:20

It's really easy to add new settings (features) than can be applied to any virtual site you create. For this you can create a new module, just like the included virtual_site_common.module we will use here as an example. Like with any hook, replace virtual_site_common with the name of your module.

Hook in to the virtual sites module

This can be achieved by writing an implementation of hook_feature_info().

<?php
function virtual_site_common_feature_info() {
  return array(
   
// The name of the method that will apply the settings.
   
'virtual_site_common_feature' => array(
     
// The name for the settings as the title of the settings form.
     
'name' => t('Common'),
     
// A description as it will be displayed on top of the settings form.
     
'description' => t('Set common settings for a virtual site.'),
    ),
  );
}
?>

As you might note, one module can offer multiples features. Just add another one to the returned array.

Return the form to set the feature settings

Virtual sites will expect a method begins with the name of the specified 'apply method', but then postfixed with _form. This method receives an array just like the one it will return in the following paragraph and is expected to return a form array that will be displayed to set the feature settings.

<?php
function virtual_site_common_feature_form($context) {

 
// ... (took out some code to make it more simple and clear)
   
 
$menu_options = menu_get_menus();
 
$primary = isset($context['menu_primary_links_source']) ? $context['menu_primary_links_source'] : variable_get('menu_primary_links_source', 'primary-links');
 
$primary_options = array_merge($menu_options, array('' => t('No primary links')));
 
$form['menu_primary_links_source'] = array(
   
'#type' => 'select',
   
'#title' => t('Source for the primary links'),
   
'#default_value' => $primary,
   
'#options' => $primary_options,
   
'#description' => t('Select what should be displayed as the primary links.'),
  );
 
 
// ..   

 
return $form;
}
?>

Return the settings to be saved for the virtual site

Virtual sites will expect a method begins with the name of the specified 'apply method', but then postfixed with _submit. This method receives the &$form en $form_state variables like any form and is expected return the variable (mostly an array) to be saved with the virtual site to be used in the following paragraph.

<?php
function virtual_site_common_feature_submit($form, &$form_state) {
  return
$form_state['values'];
}
?>

Appliying the feature to the virtual site

The 'apply method' specified in the hook_feature_info() method will be called if a virtual site is due. The method receives the same variable it returned one paragraph earlier and is expected to apply it's feature. Most of the time, this means overriding a setting in Drupal's $conf global variable.

<?php
function virtual_site_common_feature($context) {
  global
$conf;
   
  if (
is_array($context)) {
   
$conf['menu_primary_links_source'] = $context['menu_primary_links_source'];
  }
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.