Automated setupDon't get me wrong - I love Domain Access. I cannot however stand wasting time setting up a new (sub)domain. There are certain steps one can sometimes ignore - domain registration, server side configuration etc - if you happen to have people around to do it for you. Setting up a new domain in Drupal on the other hand cannot just be handed off to the end user especially if your setup includes using unique menus per domain, different front page settings etc. Fortunately Domain Access provides enough hooks to do all of that (and more!) behind the scenes without requiring the user to do anything besides inserting the url and site name of the new domain. Easy peasy, right? For simplicity's sake let's just assume each domain has its own menu and unique front page. Here's the hooks that will save your day and countless hours down the road:
  • hook_form_alter
  • hook_domainupdate
The modules you need to have enabled are the core Domain module and Domain Configuration. Step 1 - Add an "automate" checkbox to domain creation screen Note: Even though the automated workflow will be used 99% of the time chances are at some point you'll need to do it by hand hence make it a configurable option. I'm using hook_form_alter instead of hook_domainform since I want it only on create domain form and not on default Domain settings page. /** * Implementation of hook_form_alter(). */ function mymodule_form_alter(&$form, $form_state, $form_id) { switch ($form_id) { case 'domain_form': // Only show automated setup on new domain setup if ($form['domain_id']['#value'] == NULL) { $form['domain_setup'] = array( '#title' => t('Automated domain setup'), '#type' => 'fieldset', '#collapsed' => FALSE, ); $form['domain_setup']['domain_setup_enabled'] = array( '#title' => t('Create required elements on domain creation'), '#type' => 'checkbox', '#description' => t('The following tasks will be completed behind the scenes:
- Create a new menu for this domain
- Associate menu with this domain as source for primary links and content
- Create and associate a unique home page with this domain'), '#default_value' => 1, ); $form['submit']['#weight'] = 10; } break; } }
As a result admin/build/domain/create will look like this. Step 2 - Execute actions based on checkbox value Note: even though domain creation form includes a checkbox it doesn't mean that it is always checked and it never hurts to make sure you are submitting the form for a brand new domain. /** * Implementation of hook_domainupdate(). */ function mymodule_domainupdate($op, $domain, $form_state = array()) { switch ($op) { case 'create': if (!empty($form_state['values']['domain_setup_enabled']) && empty($form_state['values']['domain_id'])) { _mymodule_domain_setup($domain); } break; } } I've separated the actual setup tasks into a separate function. I've wrapped everything in an if statement to be absolutely sure Domain Conf is enabled. Part 1 - creating a menu and setting it as default for primary links and content creation for the new domain. Menu name length in Drupal 6 db table is 32 characters, "menu-" prefix is added by default which really makes the length of the menu name 27 characters. I've opted for simplicity's sake to use the format of domain-domain_id-primary-links. However the text that the user seems is not menu name but menu title and can be as descriptive as you like. Menu description - well, it's self-explanatory. There currently is no function to call to create a menu so I'm using a regular db_query() to do the insert. Associating the newly created menu with the domain is a piece of cake thanks to the new domain_conf_variable_set() in 6.x-2.x branch. Part 2 - creating a node and setting it as the front page of the new domain. First you create an array of node elements - yes, normally a node is an object but it doesnt have to start out that way. In my case I have a multilingual site hence the language key in the array. Note domain_id and domain_site keys - in my case this node is going to be associated with the new domain only. To make it published to all affiliated you'd need to set domain_site to TRUE. My node also has a menu entry in the newly created menu. Again, if you're not using it simply skip these 3 rows. Why node_validate(), node_submit() and node_save()? node_validate() performs validation checks on given node and among other things turns node array into an object. node_submit() prepares the node for save and allows other modules to make changes. Finally node_save() saves the node object into the database. function _mymodule_domain_setup($domain) { if (module_exists('domain_conf')) { $menu_name = 'domain-'. $domain['domain_id'] .'-primary-links'; $menu_title = $domain['sitename'] .' primary links'; $menu_description = $domain['sitename'] .' primary links are used to navigate sections of the site.'; $result = db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('%s', '%s', '%s')", $menu_name, $menu_title, $menu_description); if (!empty($result)) { // Associate the newly created menu with the domain using domain_conf domain_conf_variable_set($domain['domain_id'], 'menu_primary_links_source', $menu_name); domain_conf_variable_set($domain['domain_id'], 'menu_default_node_menu', $menu_name); } $edit = array( 'type' => 'page', 'name' => 'admin', 'comment' => 0, 'status' => 1, 'title' => 'Front page for '. $domain['sitename'], 'body' => 'Welcome to '. $domain['sitename'], 'domains' => array($domain['domain_id'] => $domain['domain_id']), 'domain_site' => FALSE, 'language' => language_default('language') ); // Add menu entry for the node $edit['menu']['menu_name'] = $domain_menu; $edit['menu']['link_title'] = $domain['sitename'] .' home'; $edit['menu']['weight'] = -50; // Needs to be the first item in the menu items list } node_validate($edit); $node = node_submit($edit); node_save($node); if (!empty($node->nid)) { domain_conf_variable_set($domain['domain_id'], 'site_frontpage', 'node/'. $node->nid); drupal_set_message(t('Created front page node for !sitename', array('!sitename' => $domain['sitename'], '!siteurl' => '/node/'. $node->nid))); } } } Not counting comments and brackets this is altogether less than 60 lines of code. If it will save you from being interrupted while you are writing something truly awesome to set up a new domain, then you can consider mission accomplished.

It's quiet in here! Why not leave a response?

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.