Use CCK link and filefields as redirects to legacy content

Jun 7, 2010 - Posted by: Jody Hamilton

We're working on a project with too much non-uniform legacy HTML to migrate it all into Drupal. Some of it we're handling by migrating certain data into nodes and dumping the entire HTML content into the node body so it can be easily indexed by Solr for search and the titles can be included in other views listings.

The legacy URL's of this content (which will remain present as HTML files on the new site) gets put into various CCK link fields or filefields (for PDF content). We started out overriding various views so that the links to these nodes would actually be links to the URL or file referenced in the CCK field. But I decided it would be simpler longterm if link and filefields had a setting to turn them into a kind of "redirect field" so that when you visited a node with one of these fields you'd be sent to see the legacy content instead. This way we don't have to do anything special with views and search result displays and visitors will never see our unstyled place-holder nodes.

Luckily CCK had a useful drupal_alter for handling widget settings, so I could just add a new checkbox to certain field types and have the data stored for me. I also got to make creative use of hook_menu_alter, which is always a dangerous pleasure.

Here's what I'm using currently in case you need to do something similar (it feels too specialized to be yet another contrib module to me)


/**
* @file
* Allow filefield and link fields the option to
* redirect to legacy content.
*/

/**
* Implementation of hook_widget_settings_alter().
* Add setting to cck fields to use as a redirect.
*/
function field_redirects_widget_settings_alter(&$data, $type, $widget) {
if ($widget['module'] == 'filefield' || $widget['module'] == 'link') {
if ($type == 'form') {
$data['field_redirects'] = array(
'#type' => 'checkbox',
'#title' => t('Use this field as a redirect to legacy content'),
'#default_value' => $widget['field_redirects'],
);
}
elseif ($type == 'save') {
$data[] = 'field_redirects';
}
}
}

/**
* Implementation of hook_menu_alter().
*/
function field_redirects_menu_alter(&$items) {
$items['node/%node']['page callback'] = 'field_redirects_node_view';
}

/**
* Custom callback for node views.
* Redirect appropriately if the node has a field calling for redirect.
*/
function field_redirects_node_view($node) {
$content_type = content_types($node->type);
foreach ($content_type['fields'] as $field_name => $field) {
if ($field['widget']['field_redirects']) {
$field = $node->$field_name;
if ($field['widget']['module'] == 'filefield') {
if ($field[0]['filepath']) {
drupal_goto($field[0]['filepath']);
}
}
elseif ($field[0]['url']) { // link fields don't show widget info..
drupal_goto($field[0]['url']);
}
}
}
// Default behavior.
return node_page_view($node);
}
?>

Jody Hamilton
I am passionate about the quality of our work and I maintain high standards for my team. I am deeply committed to the projects I lead, typically coming up with solutions for them at all hours. I care... Read more

Contact

Send us a message

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.