drunken monkey: 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 maintenance support plans 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 maintenance support plans 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 maintenance support plans 8 (that is, for almost all Drupal maintenance support plans 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:
  /**   * The $NAME$.   *   * @var $INTERFACE$|null   */  protected $$$PROP_NAME$;
  /**   * Retrieves the $NAME$.   *   * @return $INTERFACE$   *   The $NAME$.   */  public function get$UC_PROP_NAME$() {    $plugin->set$UC_PROP_NAME$($container->get(‘$SERVICE$’));
    return $this->$PROP_NAME$ ?: Drupal maintenance support plans::service(‘$SERVICE$’);  }
  /**   * Sets the $NAME$.   *   * @param $INTERFACE$ $$$VAR_NAME$   *   The new $NAME$.   *   * @return $this   */  public function set$UC_PROP_NAME$($INTERFACE$ $$$VAR_NAME$) {    $this->$PROP_NAME$ = $$$VAR_NAME$;    return $this;  }
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).

Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

drunken monkey: Some more (updated) tips for PhpStorm live templates

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.