I migrated a site not using Drupal to Drupal. Now I need to make sure that some legacy URLs are redirecting to the right node URL.
The node has the node ID (nid), the legacy ID (field_leg_id), the item ID (item_id), the title (headline_of_the_item), and the body.
The legacy URL example.com/type1/201203080034 should redirect to the new URL example.com/type1/2012/03/08/headline_of_the_item/item_id.
201203080034 is the legacy ID corresponding to 20120308 in the new URL.
How do I redirect users to the new node when I just have the legacy ID?
update
// Redirect based on legacy ID.
$query = db_query("select lid.entity_id, lid.field_legacy_id_value, sid.field_site_content_id_value
from field_data_field_legacy_id lid
inner join field_revision_field_site_content_id sid on lid.entity_id = sid.entity_id
where lid.field_legacy_id_value = $content_id
LIMIT 1");
while ($row = db_fetch_object($query)) {
$s_id = $row->sid.field_site_content_id_value;
$item = node_load($s_id);
if ($item) {
drupal_goto("node/{$item->nid}", array(), 301);
}
}
}
This is giving me 500 error. Tried to debug it without success.
update based on @greendemiurge Below is what worked for me.
function my_item_redirect_item() {
$args = arg();
$content_id = end($args);
$item = my_item_by_legacy_id($content_id);
if ($item) {
drupal_goto("node/{$item->nid}", array(), 301);
}
}
function my_item_by_legacy_id($id) {
// First, check if we have an imported item with this $id (field_legacy_id).
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->fieldCondition('field_legacy_id', 'value', $id, '=')
->range(0, 1)
->addMetaData('account', user_load(1));
$result = $query->execute();
if (isset($result['node'])) {
$nids = array_keys($result['node']);
return node_load($nids[0]);
}
// Check if there is a node with Nid = $id
return node_load($id);
}