PHPStorm Live Templates for Drupal

PHPStorm Live Templates for Drupal image

I’m a Drupal developer and like most of my colleagues, I’m excited over PHPStorm. It has a lot of advantages (performance being the main one for me). One great advantage is Live Templates, which are predefined code fragments that you can insert into your source code. The best part about it – if you’re a Drupal dev –  is that there is already a repo of live templates for Drupal.

(Tip: if you use OSX & PHPStorm 7+, then you’ll probably need to take one more step before the 7th: ln -s ~/Library/Preferences/WebIde70 ~/.WebIde70 (until bug “.WebIde## not found” is fixed in the main branch)).

After installing Live Templates feature, you can check for available templates by opening Preferences and choosing Live Templates in IDE settings.There are many code templates for basic Drupal hooks, render arrays, theme functions (like theme_image, theme_item_list…) and others in the repo that just can speed up your workflow.

There is also an option to add new live templates, and it brings even more benefits. There are a lot of standard subtasks that developers usually perform when working on projects. For some of them you can find a predefined template, and if not, you can create one yourself.

The day after installing Live Templates, I have extended the base with 3 new templates useful for me – t function, get entity metadata wrapper and constructor for custom Drupal forms.

Below I would like to show you how to do this.

1) t function

t_function_livetemplates

Fill *Abbreviation *field with the command you want to call the live template with. *Description *if needed. *Template *text – basic logic of the template.

$SELECTION$ – predefined variable, used for surround templates, will be replaced by your selection in code after applying live template.

Make it applicable in necessary file types. Then click Apply and Save.

Now when you select text in your template, you will be able to wrap it with the t function. (Shortcut: Alt + ⌘ + J in Mac or Alt + Ctrl + J in Windows):

t_template

Result:

<?php print t(‘Want to be Translatable’); ?>

2) Get entity metadata wrapper

For easy access and manipulation of field data we usually use entity metadata wrapper, which is provided by Entity API, so it might be a good idea to have a template for it:

emd

Here I’ve used the variable ‘module’ and created a template for it. Live templates are quite flexible with variables, there are some predefined expressions for most purposes (to manage them, just click *Edit variables *above options). In this case – as you can see in the screenshot – I used two of them. Therefore, by default there will be the first word of a filename, without an extension. This way it will be easy to search in watchdog for errors debugging.

Now you can just select any node nid (like predefined constant in your code or argument from the url, or nid of related node) and wrap it with emd function. Or just start typing emd and choose it from the tooltip.

3) Custom Drupal form creation

form

Here you can see a variable $FORM_ID$ which you will be able to insert while applying a live template, and that makes it flexible for different modules. So just type the abbreviation of the live template: custom_drupal_form, then press Tab (by default) and receive basic functions for the custom form.

Those were just some examples that can be useful and I’m pretty sure you will add plenty other as you use this feature more.

Live Templates are convenient and really save your time. It’s always great to discover new features for tools that you already like and use. Making time for regular research to find possible improvements always pays back.

Do you use live templates for Drupal? Which ones do you find most useful?

KEEP MOVING FORWARD

Yulia Tsyba / drupal