This post describes how to have data entered in the Aegir site be useable in your install profile. The way this works now still feels quite hacky and probably won't stay like this forever. If anyone has better suggestions please post in the comments :).
For anyone with a slightly customized aegir setup, passing data from aegir to your install profile is a must. In Wedful (my own business running on top of Drupal w/ Aegir) our aegir site signup form looks like this:
We've added a few custom fields here including wedding date and theme selection. In addition I also want to get the client name back into my install profile so that I can properly set the account profile data for the user on the site.
If you do have any custom data, the first thing you need to do is get it saved into the aegir database. This will totally depend on your own needs and I won't go into details here on that aspect. In this case, I created a new db table that stores the site and client ids with their corresponding selected theme and wedding date.
Adding new form elements to the aegir site or signup forms changed quite drastically between 0.4 alpha8 and alpha9. As of alpha9 there's a new function that can be used to add fields to the site and signup form _hosting_site_field(). It can be used as follows:
<?php
function mymodule_form_alter((&$form, &$form_state, $form_id) {
if ($form_id == 'site_node_form') {
$node = $form['#node'];
_hosting_site_field($form, $node, 'custom_string', array(
'#type' => 'textfield',
'#title' => t('Custom string'),
'#description' => t('Type a custom test string here'),
'#required' => TRUE,
));
}
}
?>
You'll need to implement your custom nodeapi hooks as well to save/delete the custom_string data
<?php
function mymodule_nodeapi_site_insert($node) {
db_query("INSERT INTO {mymodule_site} (vid, nid, custom_string) VALUES (%d, %d, '%s')", $node->vid, $node->nid, $node->custom_string);
}
function mymodule_nodeapi_site_update($node) {
// First make sure this site already exists in the getwedful site table
$result = db_query("SELECT vid FROM {mymodule_site} WHERE vid = %d", $node->vid);
if (db_result($result)) {
db_query("UPDATE {mymodule_site} SET custom_string = '%s' WHERE vid = %d", $node->vid);
}
else {
mymodule_nodeapi_site_insert($node);
}
}
function mymodule_nodeapi_site_delete($node) {
db_query("DELETE FROM {getwedful_site} WHERE nid = %d", $node->nid);
}
function mymodule_nodeapi_site_delete_revision($node) {
db_query("DELETE FROM {getwedful_site} WHERE vid = %d", $node->vid);
}
?>
Ok, so now that the easy part is out of the way, create (or edit if you already have one) your custom drush.inc, in this case, mymodule.drush.inc:
<?php
function drush_mymodule_pre_hosting_task() {
$task =& drush_get_context('HOSTING_TASK');
if ($task->ref->type == 'site' && $task->task_type == 'install') {
$client = node_load($task->ref->client);
$vid = $task->ref->vid;
$mymodule = db_fetch_object(db_query("SELECT * FROM {mymodule_site} WHERE vid = %d", $vid));
$task->options['client_name'] = $client->client_name;
$task->options['custom_string'] = $mymodule->custom_string;
}
}
?>
This function hooks into pre_hosting_task and copies the data we want (i.e. client name and the custom string) into $task->options.
Next, we need to create another file, and it needs to sit in your .drush folder (unlike the previous mymodule.drush.inc which sits in your custom module folder). Create a mycommandfile.drush.inc in your ~/.drush folder and add something like the following to it:
<?php
function drush_mycommandfile_pre_provision_install($url = NULL) {
drush_set_option('client_name', drush_get_option('client_name'), 'site');
drush_set_option('custom_string', drush_get_option('custom_string'), 'site');
}
?>
This sets the value of the data as drush options into the site context. This is a two step process as the site context doesn't exist pre_hosting_task, and pre_provision_install acts as the bridge between aegir and your actual install profile.
Now that you've set those options in the "site" context you can retrieve them in your install profile like so:
$client_name = drush_get_option('client_name');
$custom_string = drush_get_option('custom_string');
DONE! :)
Attachment | Size |
---|---|
Screen shot 2010-10-04 at 9.55.54 AM.png | 174.13 KB |