Useful TextMate Snippets for Drupal
I took a few minutes this evening to whip up some TextMate snippets that I think will be useful for Drupal development. They're nothing fancy, and certainly not as ambitious as Steven Witten's kitchen sink approach, but I think it'll simplify one of the most repetitive tasks I usually find myself in when writing a new module.
These snippets all use TextMate's built-in tab stop handling, which allows you to quickly insert the snippet and then just tab through all the relevant places where you'd need to change something. Give it a try - especially with the .module file snippet.
Initial setup
- Open up TextMate
- Go to the Bundle Editor (Bundles -> Bundle Editor -> Show Bundle Editor, or
ctrl+option+cmd+b) - Add a new bundle and call it "Drupal"
- From here out, each snippet can be added by choosing "New Snippet"
The .info file
I called this one "Module info file" and gave it the tab trigger drupmod_info. I haven't figured out how to get this one to actually render via a tab trigger, but it's easy enough to just navigate to the Drupal bundle and choose the snippet.
; \$Id\$
name = "${1:Example module}"
description = "${2:Example module description}"
dependencies = $0The .module file
This snippet uses TextMate's variable mirroring to let you quickly rename all the hooks in the snippet with the first tab stop. I used the hooks below because most of my modules implement these hooks. Just follow the pattern for naming, etc. to add your preferred hooks.
I called this one "Module general hooks" and assigned it the tab trigger drupmod_m.
<?php // \$Id\$ /** * Implementation of hook_perm */ function ${1:example}_perm() { return array( '${2:example permission}', ); } /** * Implementation of hook_menu */ function ${1:example}_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array( 'path' => 'admin/settings/${1:example}', 'title' => t('${3:Example}'), 'callback' => 'drupal_get_form', 'callback arguments' => array('${1:example}_admin_form'), 'access' => user_access('administer site configuration'), ); } return $items; } /** * Menu callback - Admin settings form. */ function ${1:example}_admin_form() { $form = array(); return system_settings_form($form); } /** * Implementation of hook_form_alter */ function ${1:example}_form_alter($form_id, &$form) { switch ($form_id) { // Alter node edit form case $form['type']['#value'] .'_node_form': break; } } /** * Implementation of hook_nodeapi */ function ${1:example}_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { switch ($op) { case 'delete': break; case 'insert': break; case 'load': break; case 'submit': break; case 'update': break; case 'view': break; } }
The .install file
This one is pretty similar to the .module file, but I tried to make sure it handled some common uninstall tasks I usually forget to tackle until I absolutely have to.
I called this one "Module install hooks" and assigned it the tab trigger drupmod_i.
<?php // \$Id\$ /** * Implementation of hook_install */ function ${1:example}_install() { switch ($GLOBALS['db_type']) { case 'mysqli': case 'mysql': db_query("CREATE TABLE IF NOT EXISTS {${1:example}} ( id int unsigned NOT NULL auto_increment, field varchar(128) NOT NULL default '' PRIMARY KEY (id), ) /*!40100 DEFAULT CHARACTER SET utf8 */;"); break; } } /** * Implementation of hook_uninstall */ function ${1:example}_uninstall() { db_query("DROP TABLE {${1:example}}"); $variables = db_query("SELECT name FROM {variable} WHERE name LIKE '${1:example}%%'"); while ($variable = db_fetch_object($variables)) { variable_del($variable->name); } }
- Login to post comments


Comments
Thanks for the great snippets! The .module one is especially useful.
I think snippets aren't very suitable for .module, .install and .info files. I prefer Konstantin Käfer's approach, who uses templates: http://kkaefer.com/blog/drupal-tmbundle.
I hadn't seen that bundle from Konstantin before - it's fantastic. Thanks for the link.
I’m not big on commenting, but nice post. I always found beneficial quality information from this site!
Thanks for your feedback!