|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 EAP.|
- Enabling Drupal Integration in an Existing PhpStorm Project, or Creating a New Drupal Module
- Enabling Drupal Integration in an Existing PhpStorm Project
- Creating a New Drupal Module
- Development Environment Configuration
- Hook Support
- Completion for Hook Declaration
- Navigation from hook implementation to hook invocations and docs from the gutter
- Quick Documentation for Hook Declaration
- Drupal Command Line Tool Drush Integration
- Search in Drupal API Right from the Editor
- Coder and PHP Code Sniffer Integration
- Drupal Issue Tracker Integration
- Symfony2-specific features for Drupal 8 and higher (via Drupal Symfony2 bridge plugin)
- Prerequisites (plugin installation and configuration)
- Drupal-specific parameters completion in YAML files
- Completion for t() function strings
- Completion for routing (URL generation) and navigation to routing YAML files
- Full service container support
- Twig template engine support
- ...and many more features from Symfony2 Plugin and PHP Annotations Plugin
- Debugging and Profiling Drupal Applications with PhpStorm
- Video Tutorial on Drupal Development using PhpStorm
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.
A new Drupal module can be created from the Welcome screen 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).
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.
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.
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
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.
Coding Standard can be always re-configured by going to Settings | Code Style | PHP
|Support for Drupal Hooks is provided in .module files only. Read more about hooks on the Drupal API web site.|
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.
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 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.
Drush is a command line shell and scripting interface for Drupal.
PhpStorm's command line tool supports Drush 5.8 and higher versions.
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.
Read more about command line tools in PhpStorm.
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 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.
5. Turn on PHP Code Sniffer inspections in Settings | Inspections | PHP | PHP Code Sniffer validation. Select Drupal coding standard in the list (use the refresh button if needed). Click OK.
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.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.
Please check our detailed tutorial on Integration with an Issue Tracking System in PhpStorm and Integrating PhpStorm and the Drupal.org Issue Queue blog post by Cameron Eagans, which provides detailed information on Drupal.org issue tracker configuration.
Issue Tracker Integration configuration is available in Settings | Tasks | Servers. Add a new Generic server with http://drupal.org as the Server URL.
Specify the commit message on the relevant tab if necessary.
Tasks List URL is a Drupal.org issue tracker search address (e.g. http://drupal.org/project/issues/drupal?categories=All for drupal core project) with ServerUrl as a placeholder for http://drupal.org
The Task pattern is
Now we can open Drupal.org issue tracker tasks in the IDE in Tools | Tasks & Contexts | Open Task, switch between them, create VCS changelists based on task we are working on, and more.
Drupal, starting from version 8, is based on a number of Symfony2 framework components. PhpStorm provides excellent support for Symfony2 framework through core features and the Symfony2 Plugin (an Open Source plugin maintained by Daniel Espendiller).
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.
First of all, you need to install the plugin in PhpStorm. Open Settings (Preferences) | Plugins, and click the Browse repositories... button:
Search for Symfony2 and install the Drupal Symfony2 Bridge plugin by using the context menu. It is dependent on the main Symfony2 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 Symfony2 Plugin with Drupal Symfony2 Bridge plugin are installed, enable Symfony2 Plugin in your project, if it's not enabled yet. To do that, select Settings (Preferences) | Symfony2 Plugin | Enable Plugin for this Project. Then restart the IDE once more so this configuration takes effect.
In case of any problems, select File | Invalidate Caches / Restart to reindex your project.
Information about modules is now provided inside module_name.info.yml files. PhpStorm with Symfony2 plugin provides completion for major Drupal-specific parameters in these files as well (such as name, type, description, dependencies, etc):
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:
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.
Symfony's Twig is now the default template engine for Drupal 8, and PhpStorm with Symfony2 plugins comes with lots of features such as completion, navigation, Drupal function recognition, and many more.
There are a lot of features available in the main Symfony2 Plugin, and most of them are relevant to Symfony-specific part of your Drupal 8 installation. For more details read about the Symfony2 Plugin. Symfony2 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.
Drupal-based projects can be debugged and profiled without any Drupal-specific configuration. Please proceed with standard PhpStorm debugging or profiling workflow.