Drupal hooks code completion with PhpStorm 7

PhpStorm is my favourite IDE for PHP development and is a great tool for Drupal development. Drupal hook autocomplete used to be available via 3rd party plugins with varying reliability. It is now standard with PhpStorm with the recent release of PhpStorm 7 (via the Drupal Plugin).

When you open a module file, PhpStorm should ask you if you want like to enable Drupal support. PhpStorm should automatically recognise that it is a Drupal module. However, I have noticed that when opening a module file that is in a Virtual Machine, it is not automatically detecting it. In this case, you need to manually enable Drupal support.

Manually enable Drupal support.

Go to Preferences and select Drupal under project settings. Tick "Enable Drupal integration" and set the installation path for your Drupal site.

PhpStorm Drupal settings

Hook completion.

Just start typing out the function name as you normally would, and PhpStorm will start to autocomplete it with available hooks.

For example, when implementing hook_form_alter in a module called news:

  1. function news_form

PhpStorm autocomplete a Drupal hook

Select the hook you want and hit enter. PhpStorm will create the function and include the relevant parameters.

PhpStorm Drupal hook implementation

Hook documentation

You can quickly view the documentation for a hook implementation by clicking on the name and hitting F1.

PhpStorm Drupal hook documentation

Navigation to the hook itself

You can navigate to the hook itself by clicking on the ‘navigate to hook invocation’ in the gutter.

Navigate to Drupal hook via PhpStorm gutter

Notes

A couple of notes, on my Mac, F1 is reserved for Spotlight, so I had to give Spotlight a different shortcut.

Also, the transparency for floating windows is set to 50% . I turned off floating window transparency so I could read the documentation better. To turn it off, go to Preferences, then IDE settings and then Appearance. Deselect "Use transparent mode for floating windows". You can also try changing the ratio - what ever floats your boat.

If you liked this, you'll love my book, Master Drupal Module Development.

"..the must have drupal developers book"


Feeling stuck with Drupal 8 module dev?

Get the free 7 lesson course that will help you get started today without feeling overwhelmed.

  • Create Drupal modules with just a few commands using the Drupal Console
  • Create custom pages
  • Create custom blocks
  • Create admin forms
  • Demystify routers and controllers
  • Bonus material

Find out more


Comments

Although starting from phpstorm 7.0, drupal is supported internally.
Drupal coding style need to customize manually. For example the tab space is still default as 4

Blair Wadman's picture

Good point. I might do a separate post on how to fully configure PhpStorm for Drupal development.

Thanks, very useful. Note that it won't do the hook autocomplete in included files, which is a good thing. At first I thought it wasn't working.

Blair Wadman's picture

Cheers for pointing that out.

The title of your post is a little misleading, and may attract users looking for actual field autocomplete code or related hooks, rather than just nice code completion. Just a thought. ;)

Blair Wadman's picture

Good point Elliot. My bad. I have changed the title.

I prefer to use <a href="http://www.codelobster.com" rel="nofollow">Codelobster</a> to develop Drupal projects.

Add new comment