6199 comments
2357 subscribers
Follow me on Twitter (@sachac)
Subscribe! Feed reader E-mail

Drupal shell: quickly evaluating PHP statements in a Drupal context

I often find myself needing to variable_set something temporarily, just to try things out. The drush module provides a command-line interface that solves this problem with a little more hacking. To minimize the effect on my source tree, I’ve unpacked it into the sites directory for my local installation and enabled it in my test database. After I enabled the main drush module and the related modules, I tweaked drush_tools to include an insecure-but-useful eval command. Here it is:

--- sites/local.example.com/modules/drush/drush_tools.module.orig    2008-08-05 17:18:48.000000000 -0400
+++ sites/local.example.com/modules/drush/drush_tools.module    2008-08-05 17:18:55.000000000 -0400
@@ -46,6 +46,10 @@
     'callback' => 'drush_tools_sync',
     'description' => 'Rsync the Drupal tree to/from another server using ssh'
   );
+  $items['eval'] = array(
+    'callback' => 'drush_tools_eval',
+    'description' => 'Evaluate a command',
+  );
   return $items;
 }
 
@@ -156,3 +160,6 @@
   }
 }
 
+function drush_tools_eval($command) {
+  eval($command);
+}

I also added an alias to my ~/.bashrc along the lines of:

alias drush='php ~/drupal/sites/local.example.com/modules/drush/drush.php -r ~/drupal -l http://local.example.com'

where ~/drupal is my multisite Drupal directory root.

After I loaded the alias with “source ~/.bashrc”, I can now execute PHP statements in my Drupal context with commands like:

drush eval "variable_set('hello', 'world');"

Good stuff!

Short URL: http://sachachua.com/blog/p/5042

On This Day...

  • 2012: Building MobileOrg for Android — If you want to use your Android device to work with your Org-mode files, there’s MobileOrg and there’s MobileOrg NG. [...]
  • 2011: Drupal, HTML Purifier, and embedding IFRAMES from YouTube — I know, I know. I shouldn’t allow IFRAMEs at all. But the client’s prospective users were really excited about images [...]
  • 2010: Long weekend reflections — Long weekends? We don’t pack our bags and travel, or kick our feet up and chill out at our (non-existent) [...]
  • 2009: In-jokes and shared experiences — W- helped me put together some wire shelves for plants. One of the rods was deformed, so he brought out [...]
  • 2009: Weekly review: Week ending August 2, 2009 — From last week’s plans: Do some Facebook strategy consulting Find ways to recognize the contributions people had made to the idea lab [...]
  • 2007: Weekly review — This week Send thesis draft to people, really. Done. Check paperwork requirements for post-graduate work permit and start compiling them. Started Revise CASCON paper. Done. Do one [...]
  • 2007: Moving back to PCFinancial — I think I’ll move back to PCFinancial for my banking. I don’t think I’ll need any financial statements for my future [...]
  • 2005: Changing patterns of computing — I managed to leave my laptop at home today! =) That forced me to be productive all day. See how many [...]
  • 2005: On mailing lists — I find it a bit strange that people here like broadcasting their messages by putting everyone in To: or Cc:. I [...]
  • 2004: Philippine comic book blog — http://filipinocomics.blogspot.com E-Mail from Julius Oliveros
  • 2003: Emacs macros — Keyboard macros in Emacs allow me to easily generate the gallery from the pipe-separated values I exported from Gnumeric. Viva open [...]
  • 2003: RSS feed — I think it should be relatively easy to publish an RSS feed of the links from my planner wiki…
  • 2003: Plans — I’m going to try to move my plans up by a year. I suspect I’ll learn more by going to graduate [...]
  • 2003: Experts — I think I’ve a way to deal with my large CS123 class: topic experts. Each group has experts in charge of [...]
  • 2003: Teaching — Holding up. Doing remarkably well, in fact – probably because I’m too busy to do anything else. The sheer magnitude of [...]
  • 2003: Communication — Considering that I tend to text long messages in bursts, it might actually make more sense for me to stay at [...]
  • 2003: Redesign — Using objects in the beginning - graphics right away; bounds and draw promotes object reuse - using objects - implementing methods - expressions - if - loops - [...]
  • 2003: http://www.geocities.com/fanny11sy/featured-projects
  • 2003: JPCS conference — end of august, one way of reviving contact with JPCS. Need a venue for a seminar for the gathering of all [...]
  • 2003: Java patterns — http://www.anupriyo.com/oopfm.shtml