The method below will allow you to add a custom submit handler to a node form, and create an XML playlist file from a "Tracks" CCK field. This method can easily be tweaked to suit many other needs.
First, add a custom submit handler to our "Band" content type node edit form:
<?php
/**
* Implementation of hook_form_alter().
*/
function MODULENAME_form_alter(&$form, $form_state, $form_id){
if($form_id == 'band_node_form'){
$form['buttons']['submit']['#submit'][] = 'MODULENAME_band_submit';
}
}
?>
Note our custom submit handler points to the function MODULENAME_band_submit. Create this function below your hook_form_alter function:
<?php
/**
* Submit handler for the Band node form
*/
function MODULENAME_band_submit($form, &$form_state){
// get the nid
$nid = $form_state['nid'];
// get all uploaded tracks
$tracks = $form_state['values']['field_band_track'];
// remove the button element
unset($tracks['field_band_track_add_more']);
// create a new array which will be sorted by track weight
$weights = array();
foreach($tracks as $track){
$weights[] = $track['_weight'];
}
// sort the weights in asc order
sort($weights);
// start the markup
$output = '<?xml version="1.0" encoding="utf-8"><playlist>';
// cycle through each weight
foreach($weights as $weight){
// cycle through each track and check to match the weight
foreach($tracks as $track){
// if its a match, create the markup
if($track['_weight'] == $weight){
$output .= '<item><title>' . $track['data']['description'] . '</title><path>' . $track['filepath'] . '</path></item>';
break;
}
}
}
// finish the markup
$output .= '</playlist>';
// create the playlist filepath
$filename = file_directory_path() . '/playlists/playlist' . $nid . '.xml';
// open or create the file
$file = fopen($filename, 'w');
// save the file
fwrite($file, $output);
// close the file
fclose($file);
}
?>
As always, be sure to swap MODULENAME with your custom module name.
Note: due to syntax highlighting issues a "?" was omitted from the XML declaration line. Line should read: $output = '<?xml version="1.0" encoding="utf-8"?><playlist>';