Some more (updated) tips for PhpStorm live templates
A few years ago I started using the PhpStorm IDE for PHP development, was immediately smitten and, after a bit of use, wrote a blog post with some tips I found for makig better use of the tools PhpStorm gives you.
In the four years since then there have been some new developments. Firstly, of course, Drupal 8 was finally released – and, consequently, the one complaint I had back in 2013 about the $MODULE$
variable only working in the module file itself became more of a problem. (Also, I added one more live template that's very useful for Drupal 8.)
But secondly, a few weeks ago PhpStorm finally added scripting support for live templates, so it's now possible to write more powerful templates that way – and fix the $MODULE$
variable.
The new di
live template
In general, when writing OOP code for Drupal 8 (that is, for almost all Drupal 8 code) you should use dependency injection as much as possible. There's several different styles for doing that, I'm using one which uses setter methods and calls them in create()
(instead of adding all injected objects to the constructor). This makes inheritance easier and keeps the constructor “cleaner” – and becomes much easier with a good live template:
/**<br> * The $NAME$.<br> *<br> * @var $INTERFACE$|null<br> */<br> protected $$$PROP_NAME$;<p> /**<br> * Retrieves the $NAME$.<br> *<br> * @return $INTERFACE$<br> * The $NAME$.<br> */<br> public function get$UC_PROP_NAME$() {<br> $plugin->set$UC_PROP_NAME$($container->get('$SERVICE$'));</p><p> return $this->$PROP_NAME$ ?: \Drupal::service('$SERVICE$');<br> }</p><p> /**<br> * Sets the $NAME$.<br> *<br> * @param $INTERFACE$ $$$VAR_NAME$<br> * The new $NAME$.<br> *<br> * @return $this<br> */<br> public function set$UC_PROP_NAME$($INTERFACE$ $$$VAR_NAME$) {<br> $this->$PROP_NAME$ = $$$VAR_NAME$;<br> return $this;<br> }</p>
Variable definitions:
Name
Expression
Skip if defined
VAR_NAME
N
SERVICE
N
INTERFACE
clipboard()
Y
NAME
underscoresToSpaces(VAR_NAME)
Y
UC_NAME
underscoresToCamelCase(VAR_NAME)
Y
UC_PROP_NAME
capitalize(PROP_NAME)
Y
Usage:
- Copy the service interface's FQN to your clipboard.
- Put the service ID either into a secondary clipboard (e.g., middle mouse button on Linux) or remember it.
- Execute live template (at the position where you want the getter and setter).
- Input variable name (in snake_case), then input service name.
- Move the property definition and the
create()
line (temporarily stored as the first line of the getter in the template) to their appropriate places. - In the code, alway use the getter method for accessing the service.
Fixing the $MODULE$
variable
Since the code for this is quite complex, we better just put it into a separate file. So, first download the script file and save it to some known location, then simply use the (absolute) path to the script file as the argument for groovyScript()
, like this:
This can be used for all the live templates that contain a $MODULE$
variable (though it will, of course, be less useful for the procedural ones, than for the simple m
template).