This tutorial describes the features and best practices relevant to using PhpStorm as an IDE for Drupal development (including modules, themes and core). Starting with version 7, PhpStorm bundles the Drupal Plugin providing many Drupal-specific features for Drupal 7 and 6. Drupal 8 is supported starting with PhpStorm 8.

Enabling Drupal Integration in an Existing PhpStorm Project, or Creating a New Drupal Module

Enabling Drupal Integration in an Existing PhpStorm Project

When the project you're working on is recognized as a Drupal Module, you will be offered to enable support for Drupal automatically with a popup and event in the Event Log.

You need to provide some additional information on Drupal Installation Path (root folder of your Drupal Installation) and version number (versions 6, 7, 8 are supported). Select Set up PHP | Include paths to automatically configure include paths for the project.

Drupal integration settings can be changed in Settings | Drupal.

Creating a New Drupal Module

A new Drupal module can be created from the Welcomescreen or by selecting File | New Project

Project type should be set to Drupal Module. After clicking OK you need to provide some additional information on Drupal installation path (root folder of your Drupal Installation) and version number (versions 6, 7, 8 are supported). Select Set up PHP | Include paths to automatically configure include paths for the project.

Initial files in the project will be created automatically (.module and .info files for Drupal 7 & 6, .module and .info.yml files for Drupal 8).

Development Environment Configuration

Whether you enable Drupal support in an existing PhpStorm project or create a new Drupal Module, the IDE checks if the development environment is configured properly for Drupal development. If the configuration does not meet the requirements, pop-ups with a fix suggestion will appear, as well as events in the Event Log. To fix these issues, just click the Fix link.

Drupal Include Paths

As soon as the path and version of the Drupal installation are provided, PhpStorm includes paths such as drupal/includes, drupal/modules, and drupal/sites/all/modules as libraries, so that it can then provide code completion and other intelligent features based on Drupal core and other modules for our Drupal project.

You can always change include paths by going to Settings | PHP | Include path, but don't forget to disable Settings | Drupal | Set up PHP | Include path before.

If the Drupal integration for the project is disabled, paths are automatically excluded.

File Associations

To use code highlighting, you need to set some additional file type associations. A special check is performed by the IDE when Drupal integration is enabled, and in case the required file associations are not set, popups will appear with suggestions to fix the issue, as well as Event Log events.

.module, .install, .theme should be mapped to PHP file type

.info should be mapped to INI file type

Drupal Coding Standard

Pre-configured Drupal Coding Standards (code style) has been available in PhpStorm for a while. It can be configured in Settings | Code Style | PHP | Set From... | Predefined Style | Drupal. However, now you will automatically be offered to set Drupal Coding Standard if Drupal integration is enabled. Drupal code style will also be applied to the project in case a New Drupal Module project is created.

Hook Support

Support for Drupal Hooks is provided in .module files only. Read more about hooks on the Drupal API web site.

Completion for Hook Declaration

Any hook invocations and documentation in *.api.php files are indexed by the IDE, and hook names become available in code completion with Ctrl+Space for creating hook implementations.

Navigation from hook implementation to hook invocations and docs from the gutter

From Drupal hook implementation you can navigate to all hook invocations or hook documentation (if exists) with a 'navigate' icon on the editor gutter:

In case multiple hook invocations are available, all of them are provided so you can choose which one you would like to navigate to.

Navigation to hook documentation reference is also provided here if exists in corresponding *.api.php file.

Let's look at the example of navigation to hook invocation:

When the hook invocation to navigate to is selected, you will be navigated to the very line where the relevant hook is invoked with module_invoke_all() or other invocation method.

Apart from navigation to invocations, you can also navigate to hook documentation, and backwards.

You will be navigated to the hook documentation (relevant *.api.php file).

You can also navigate backwards to the hook implementation with the 'navigate back' icon:

A list of hook implementations is provided, you can navigate to any of them.

Quick Documentation for Hook Declaration

Quick documentation for Drupal Hook can be viewed right in the IDE with View | Quick Documentation (or just by pressing Ctrl+Q (F1 on Mac OS X)). Documentation is taken from *.api.php files provided by core and many other modules for reference purposes. Documentation is also available in hook completion which may be useful in order to choose the right hook.

Drupal Command Line Tool Drush Integration

To configure Drush as a command line tool in the development environment, open Settings | Command Line Tool Support and add a new command line tool with the plus icon:

Specify tool type as Drush, choose visibility (project or global), click OK to proceed, and then provide the path to the tool (which is usually C:/ProgramData/Drush/drush.bat on Windows or /usr/bin/drush on Mac or Linux). Command definitions will be loaded automatically by the IDE.

Now you can bring up the command line tool by selecting Tools | Run Command... or pressing Ctrl+Shift+X (Cmd-Shift-X on Mac OS X).

Command completion is provided for Drush commands. All output is shown in the PhpStorm Command Line Tool Console.

Search in Drupal API Right from the Editor

Drupal API Documentation at https://api.drupal.org/api/drupal can be easily reached from the IDE with text search. Just select the text you are interested in, invoke the context menu in the editor, and then select Search in Drupal API.

The default browser will be opened in order to reach Drupal API Documentation with the request you sent from the IDE.

Coder and PHP Code Sniffer Integration

Coder is a Drupal-specific tool for code review and code manipulation.

PHP Code Sniffer is a tool designed to detect violations of a defined coding standard. It is an essential development tool that helps ensure your code remains clean and consistent. It can also help prevent some common semantic errors made by developers.

PHP Code Sniffer is supported in PhpStorm, which means you can get its notifications inside the IDE editor. Coder's Drupal-specific coding standards are easy to add in PHP Code Sniffer, so that Drupal Coding Standard violations will be shown in the IDE editor along with standard PHP Code Sniffer notifications.

2. Download Drupal Coder module (7.x-2.* version is recommended). You don't need to install or use the Drupal Coder module - it contains the Drupal Coding Standards inside.

3. Unpack the downloaded archive and find the coder_sniffer/Drupal subdirectory inside. You need to move the Drupal directory contents to /usr/share/php/PHP/CodeSniffer/Standards/Drupal (path depends on your configuration). There should be ruleset.xml in this directory and some other subdirectories.

4. Configure PHP Code Sniffer by providing the path in Settings | PHP | Code Sniffer. Click the Validate button to check if the Code Sniffer tool can be reached.

6. Now your code is inspected by PHP Code Sniffer with Drupal Coding Standards in addition to PhpStorm's inspections. You will see notifications marked phpcs in the editor.

7. If you run batch inspections with Code | Inspect Code PHP Code Sniffer, the results of PHP Code Sniffer inspections will be listed in the section PHP | PHP Code Sniffer validation.

Drupal Issue Tracker Integration

Drupal.org Issue Tracker can be integrated in PhpStorm as a Generic issue tracker, providing commit message generation, tasks switching in the IDE, and more. Authorisation in the Drupal.org issue tracker is not supported, so the information we can get is limited to that available for anonymous users.

To take advantages of close integration between Drupal and Symfony2 while developing Drupal modules and core, Daniel maintains an additional Drupal Symfony2 bridge plugin, which connects Symfony2 components to Drupal infrastructure in terms of IDE understanding. There is also the PHP Annotations plugin to take advantage of advanced annotations support.

Prerequisites (plugin installation and configuration)

First of all, you need to install the plugin in PhpStorm. Open Settings (Preferences) | Plugins, and click the Browse repositories... button:

Search for Symfony and install the Drupal Symfony2 Bridge plugin by using the context menu. It is dependent on the main Symfony Plugin which will be installed automatically. If you would also like to get advanced annotations support, install PHP Annotations plugin as well.

Restart the IDE to complete the installation of the plugin(s).

As soon as Symfony Plugin with Drupal Symfony2 Bridge plugin are installed, enable Symfony Plugin in your project, if it's not enabled yet. To do that, select Settings/Preferences | Languages & Frameworks | Symfony | Enable Plugin for this Project. Then restart the IDE once more so this configuration takes effect.

Drupal-specific parameters completion in YAML files

Information about modules is now provided inside module_name.info.yml files. PhpStorm with Symfony plugin provides completion for major Drupal-specific parameters in these files as well (such as name, type, description, dependencies, etc):

Completion for t() function strings

Strings suitable for use inside Drupal's t() function are indexed across your project and offered for completion inside the function:

Relevant YAML key values are completed in url() and other Drupal API functions, to make it easier to search for the right value:

You can also navigate to the YAML file with Ctrl+Click (CMD+Click on Mac OS X) or Go To Declaration action (Ctrl+B / CMD+B):

It works for many other use cases in the same way, e.g. forms routing:

Full service container support

There are a lot of handy features for service containers described in YAML files, including completion, navigation with Ctrl+Click / CMD+Click (Go To Declaration action), and more.

Twig template engine support

Symfony's Twig is now the default template engine for Drupal 8, and PhpStorm with Symfony plugins comes with lots of features such as completion, navigation, Drupal function recognition, and many more.

...and many more features from Symfony Plugin and PHP Annotations Plugin

There are a lot of features available in the main Symfony Plugin, and most of them are relevant to Symfony-specific part of your Drupal 8 installation. For more details read about the Symfony Plugin. Symfony plugin also provides various Symfony Framework related features (such as type inference, code completion, navigation, action) for Symfony Core, Symfony Components, Twig templates, Doctrine, and more.

More information about the PHP Annotations plugin is also available. Its features include Attaching PhpClass to their DocTag to support "Code > Optimize Imports”; Auto alias detection on use statement; Filtering annotation classes on targets like method, class, property, etc; Goto for doc tags and property names; Detecting annotation property values on phpclass property name including types; Indexing classes with @Annotation inside doc block; Doctrine related providers; Class import annotator; and more.

Debugging and Profiling Drupal Applications with PhpStorm

Drupal-based projects can be debugged and profiled without any Drupal-specific configuration. Please proceed with standard PhpStorm debugging or profiling workflow.