Bulk Add URL Redirects in Drupal 7
Here’s a quick example showing how to add URL redirects (for the redirect module) in bulk.
Instead of a separate page for each list of “fellows”, we built a single view with exposed filters to choose a semester.
The original paths looked like “/fall-2000-fellows” and the new path looks like “/fellows/list?semester=5493”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
// Semester will be passed as a query param. Redirect uses strings if you save // a redirect through the URL Redirects gui, so we'll use strings too. $redirects = array( 'former-fellows-year' => array('path' => 'fellows/list'), 'fall-2010-fellows' => array('path' => 'fellows/list', 'semester' => '5433'), 'fall-2011-fellows' => array('path' => 'fellows/list', 'semester' => '5411'), 'fall-2012-fellows' => array('path' => 'fellows/list', 'semester' => '5408'), 'fall-2013-fellows' => array('path' => 'fellows/list', 'semester' => '5405'), 'fall-2014-fellows' => array('path' => 'fellows/list', 'semester' => '5403'), 'fall-2015-fellows' => array('path' => 'fellows/list', 'semester' => '5401'), 'spring-2010-fellows' => array('path' => 'fellows/list', 'semester' => '5434'), 'spring-2011-fellows' => array('path' => 'fellows/list', 'semester' => '5410'), 'spring-2012-fellows' => array('path' => 'fellows/list', 'semester' => '5409'), 'spring-2013-fellows' => array('path' => 'fellows/list', 'semester' => '5406'), 'spring-2014-fellows' => array('path' => 'fellows/list', 'semester' => '5404'), 'spring-2015-fellows' => array('path' => 'fellows/list', 'semester' => '5402'), 'spring-2016-fellows' => array('path' => 'fellows/list', 'semester' => '5400'), ); foreach ($redirects as $src => $dest) { $redirect = new stdClass(); $redirect_options = array(); if ($dest['semester']) { $redirect_options = array('query' => array('semesters' => $dest['semester'])); } module_invoke('redirect', 'object_prepare', $redirect, array( 'source' => $src, 'source_options' => array(), 'redirect' => $dest['path'], 'redirect_options' => $redirect_options, 'language' => LANGUAGE_NONE, ) ); module_invoke('redirect', 'save', $redirect); } |