-
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”.
123456789101112131415161718192021222324252627282930313233343536// 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);} -
Random Mouse Distance in Keyboard Maestro
Here’s one way to choose a random item in a select list using just mouse movements and a random number. If you’re trying to do this in a browser there are cleaner ways to do it (javascript, chrome/safari actions, etc.), but for general usage across many applications this is a pretty universal solution.
I was surprised to find that you can type variables into the tiny coordinate text fields. They get bigger when you do.
When executed, this will select a random item (excluding “- None -“) from the select list.
-
Update Field Value for All Nodes
I recently added a “Show in Listing” boolean on/off checkbox field to a content type (default value is to be “checked”). There were 133 nodes when I created that field. I needed to update all of those nodes to be “checked”.
Here’s how I updated all of them using entity_metadata_wrapper(). It’s much quicker than any alternative I could think of (feeds, rules, etc.).
1234567891011$nids = db_query('SELECT nid FROM {node} WHERE type = :type', array(':type' => 'blogpost'))->fetchCol();foreach ($nids as $nid) {$node = node_load($nid);if ($node) {$wrapper = entity_metadata_wrapper('node', $node);if (isset($wrapper->field_islisted)) {$wrapper->field_islisted->set(1);$wrapper->save();}}}In this particular case I’m targeting a single content type. I could’ve used a similar query and some slight modifications to set the value on any node whose bundle (content type) supports that field_islisted field.
Lastly, if you have a lot of nodes, you might consider using node_load_multiple() instead of node_load() .