
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!

8 Comments
Nice! Thanks very much for
Nice! Thanks very much for this snippet!
I have this in my snipmate
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
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
Bah, just realized I had a
Bah, just realized I had a typo in my last comment's signature. Sadly there's no way for me to fix it. Perhaps you should consider using Disqus (http://drupal.org/project/disqus) for your blog? It works really well for us. Example: http://www.evolvingweb.ca/story/drupalcamp-toronto-2011
Cheers,
Hi,
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 andmi<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
nice!
nice!
i created a python version of this, with a bit more sugar:
https://gist.github.com/1354039
Awesome, thanks!
Awesome, thanks!
Or you could use module
Or you could use module builder, which also writes the function definition for you :-)
Add new comment