Quick Tip: Drupal Hook Commenting Using Vim

Vim

According to the Drupal Coding Standards for Documenting Hook Implementations, its considered a good practice to quickly chuck a comment before any function which implements a Drupal hook (eg, hook_menu). This helps someone reading your code quickly see that the function is actually linked with a hook in Drupal and isn't just a function in your module to be called directly.

But… Well… The thing is… Does anyone else get bored of writing the following over and over again? I know do…

/**
 * Implements hook_menu().
 */

Wouldn't it be nice if you could just type in "menu" and Vim could just fill it our for you? Here follows a little Vim script for inserting a "hook implements" comment at the current cursor position.

function! DrupalImplementsComment(hook)
  set paste
 
  exe "normal! i/**\<CR>"
  \          . " * Implements hook_" . a:hook . "()\<CR>"
  \          . " */\<Esc>"
 
  set nopaste
endfunction
 
map <C-C><C-C><C-C> :call DrupalImplementsComment(input("Enter Hook name:"))<CR>

Wherever your cursor is, press Ctrl+C 3 times, you then get prompted to enter the hook name. When you press enter, a comment gets inserted. Hopefully this will save someone some time - its already saving me time!

To install the script, I just have it in a file called DrupalCommenting.vim inside my ~/.vim/ folder. Then, inside my ~/.vimrc file, I have a line which imports the source file, eg: so ~/.vim/DrupalCommenting.vim.

Any improvements very welcome!

Comment Icon

8 Comments

The most recent comment was on Tue, 15th Nov 2011 - 10:40

Nice! Thanks very much for this snippet!

I have this in my snipmate snippets:

/**
 * implements hook_$2
 */
function ${1:`expand("%:t:r")`}_${2:hookName}(${3}) {
  ${4:// @TODO: implement}
}

Cool stuff, I was thinking of writing the same thing recently. Here's my take:

function! DrupalImplementsComment(fname)
  let hook = substitute(a:fname,"^[0-9a-zA-Z]\\+_","","")
  set paste
 
  exe "normal! O/**\<CR>"
  \          . " * Implements hook_" . hook . "().\<CR>"
  \          . " */\<Esc>"
 
  set nopaste
endfunction
nmap <Leader>h :call DrupalImplementsComment(expand("<cword>"))<CR>

So on my computer, moving your cursor onto the function name (eg "mymodule_menu") and pressing \h will automatically insert the comment "Implements hook_menu()."

The "Leader" is a user-configurable mapping prefix (defaults to "\", often set to ",") that's often used to define custom commands.

This implementation of DrupalImplementsComment adds a trailing period to the comment ("Implements hook_menu()."), which the original version omits.

Happy vimming.

-- Alex Dergachev, TWech lead, Evolving Web, http://evolvingweb.ca

Anonymous's picture

Hi,

Here is a Drupal Vim snippets:

https://github.com/tanarurkerem/drupal-snippets

just type hook_[NAME_OF_THE_HOOK]<tab> and you will get a complete hook definition with Docblock, correct footprint and sample code.

It contains plus two snippets (fi<tab> for form item and mi<tab> for menu item in hook_menu), and suggested indentation and syntax highlight settings. (source: http://drupal.org/node/29325)

If you install correct this with snipmate-snippets, you will get php.snippets which give you many Docblock snipets(file, class, function etc.) and many php snippets of course.

I hope it help you.

pp

Awesome, thanks!

Or you could use module builder, which also writes the function definition for you :-)

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <img> <p>
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • You can enable syntax highlighting of source code with the following tags: <code>, <pre>, <bash>, <css>, <html>, <js>, <jquery>, <mysql>, <php>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.