Some practical tips for PhpStorm with Drupal

Wed, 2013-10-23 11:00 -- drunken monkey

TL; DR:

PhpStorm is an awesome IDE for developing with Drupal, you should definitely give it a try (unless you are too much resolved to only use OSS)! There was now a version 7.0 released which even includes some native support for Drupal.
Read below for some tips from me on how to further tune PhpStorm for use with Drupal.

History

I have for a long time used Eclipse for developing for Drupal (and even the odd Java program), but it had been going worse and worse for a while. Slow speed, large ressource consumption, rather poor PHP integration, lack of macros and other functionality – the list was rather long. I had already tried out NetBeans, though, and found it even worse (although I have to say I recently heard very good things about using it with Drupal), and since I'm an avid supporter of and believer in Open Source I was reluctant to try out one of the proprietary alternatives. I had heard very good things about PhpStorm (and they even give out free licenses to anyone maintaining an Open Source project!), but just couldn't bring myself to give it a shot.

However, once I tried out Drupal 8, Eclipse became just intolerable (No namespace support? Well …) and I finally took PhpStorm for a spin. In just a few minutes, it was far more usable than Eclipse had ever been, it's even running great on my 4.5 years old laptop and it has simplified and sped up developing for me to an extent that I cannot imagine going back to Eclipse under almost any circumstances. Furthermore, obtaining a free license for the Search API was completely painless and done in just a day, even though they weren't an official DA partner back then.

With the new PhpStorm 7.0 version, there's now even built-in support for Drupal, including showing the hook definition when viewing an implementation and finding places where it is called. While there were only very few features PhpStorm previously lacked for me, this is bound to reduce that list even further.

My tips

There is already a great handbook page about configuring PhpStorm for use with Drupal (though I could imagine that some of the instructions might be outdated with the integrated Drupal support in PhpStorm 7.0). What isn't mentioned there is that PhpStorm understands the same style of doc comments that Drupal uses – that means, if you keep to Drupal's API documentation and comment standards, you'll automatically get type inference for your functions'/methods' return values! So, when you type something like:

<?php
$i
= search_api_index_load($id);
$i->ser
?>

you'll automatically get completion suggestions (like server() and postprocessSearchResults(), in this example). Nice, right? (The downside is, of course, that the sensation that almost no-one else is keeping to the coding standards will become even more apparent and annoying.)

Live Templates

The neatest thing I discovered, though, are Live Templates. You can find them in the IDE settings, rather unsurprisingly in the section "Live Templates". Their effect is that when you type a certain combination and then press "Tab" (or a key of your choice), the tempalte will automatically get inserted, let you fill the variable portions defined for it and then even jump to a pre-set location in it. Very useful!
While there are some good ones for PHP in general defined already, you can also (which I didn't realize at first) add your own with the green plus sign on the right side. (You should maybe also use that before to define a new group "Drupal" for your Drupal-specific templates, to keep things tidy.) Here are the ones I defined:

f (function with module prefix)
Template text:
/**
*
*/
function $MODULE$_$NAME$($ARGS$) {
  $END$
}

Special variable settings:

  • MODULE: fileNameWithoutExtension(), skip if defined
form (form constructor and callbacks)
Template text:
/**
* Form constructor for $DESC$.
*
* @ingroup forms
*
* @see $FORM$_validate()
* @see $FORM$_submit()
*/
function $FORM$(array $form, array &$form_state) {
 
}

/**
* Form validation handler for $FORM$().
*
* @see $FORM$_submit()
*/
function $FORM$_validate(array $form, array &$form_state) {
 
}

/**
* Form submission handler for $FORM$().
*
* @see $FORM$_validate()
*/
function $FORM$_submit(array $form, array &$form_state) {
 
}

This makes it very easy to define a new form along with the default callbacks and proper documentation comments!
Could probably also be improved with $MODULE$.

hook (implement hook_X)
Template text:
/**
* Implements hook_$HOOK$().
*/
function $MODULE$_$HOOK$() {
  $END$
}

Special variable settings:

  • MODULE: fileNameWithoutExtension(), skip if defined
m (module)
Template text:
$MODULE$

Special variable settings:

  • MODULE: fileNameWithoutExtension(), skip if defined

Especially the $MODULE$ trick is very neat – however, it sadly only works properly in the module file and others that contain only a single dot in the file name – for others, you'll have to manually delete the portion after the module name (you should probably disabled "skip if defined" if that happens too often), and it of course stops working completely in Drupal 8 class files. I currently haven't discovered a way around this, but maybe someone will come up with a simple plugin for doing this at some point.

Do you have other tips?

If you have other tips to share, want to correct me on something or just discuss, please leave a comment below!

Tags: 

Comments

Add new comment

To prevent spam, submitting full URLs in comments is not allowed. Please omit the "http[s]://" portion of the URL and I will restore the complete URL on review.

Filtered HTML

  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <q> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <sup> <sub> <p> <br>
  • To post pieces of code, surround them with <code>...</code> tags. For PHP code, you can use <?php ... ?>, which will also colour it based on syntax.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.