Drupal
You can use PhpStorm as the IDE for Drupal development, including development of Drupal modules, themes, and the core. The supported versions are 6, 7, 8, and 9.
Enable Drupal support in PhpStorm
Option 1: Enable Drupal support in PhpStorm settings
Open
(Ctrl+Alt+S) .Extend the Enable Drupal integration checkbox, and specify the parameters as follows:
node, select theDrupal installation path
Specify the path to the root directory that contains the installed Drupal core.
Set up PHP | Include paths
Select the include paths.
checkbox to have PhpStorm automatically add Drupal core directories to the project'sTo configure the paths manually, open Include path tab.
(Ctrl+Alt+S) and edit the list of direectories on theVersion
Choose the version of Drupal to use, the supported versions are 6, 7, 8, and 9.
Click Apply to save the changes.
Option 2: Create a new PhpStorm project from a Drupal template
You can create a new Drupal project in PhpStorm from a Drupal Module template.
PhpStorm generates and configures a project stub in accordance with the selected Drupal version. For Drupal 8, a module_name.info.yml file is generated.
Open the New Project dialog by choosing in the main menu or clicking Create New Project on the PhpStorm Welcome screen.
In the left-hand pane, select Drupal Module.
In the right-hand pane, specify the Drupal support parameters.
Option 3: Import an existing Drupal module project
When you open (
in the main menu) an existing Drupal module project in PhpStorm, the IDE recognizes the Drupal-specific project structure and suggests activating Drupal support.Click Enable Drupal Support to open the modal dialog and specify the parameters.
Associate Drupal files with PHP type
To provide proper code highlighting, PhpStorm needs to associate Drupal file formats with the PHP file type.
When you open a Drupal project in PhpStorm, the IDE detects the project type automatically and suggests associating Drupal file types with the PHP type.
Associate file types manually
In the Settings dialog (Ctrl+Alt+S) , navigate to .
On the File Types page that opens, select PHP on the Recognized File Types list.
In the File name patterns area to the right, click () and add one by one the
*.test
,*.install
,*.engine
,*.profile
, and*.theme
file name patterns.For more information, refer to File type associations.
Use Drupal hooks in PhpStorm
PhpStorm provides full native support for Drupal hooks in .module files.
Use code completion for hook declarations.
PhpStorm indexes any hook invocation whereupon hook names become available in code completion for creating hook implementations. To complete a declaration, start typing the hook name and press Ctrl+Space.
Navigate to hook invocations.
To navigate to a hook invocation from the editor, click the icon in the gutter.
In case of multiple invocations, PhpStorm displays a list of available hook invocations, and you can choose the one to jump to.
You will be navigated to the line where the relevant hook is invoked with
module_invoke_all()
,module_invoke()
, ordrupal_alter()
.
View hook documentation.
Place the caret at the name of the hook and choose
or press Ctrl+Q.
Set up Drupal code style in a PhpStorm project
If PhpStorm recognizes the project to be a Drupal Module, or if the Drupal integration is enabled in an existing project, or when you create a new project from a Drupal Module template, PhpStorm automatically suggests applying the pre-configured Drupal Coding Standards (code style).
Change code style settings manually
You can also set the predefined code style manually on the Code Style: PHP page.
In the Settings dialog (Ctrl+Alt+S) , navigate to .
Click Set from and choose Drupal from the drop-down list that opens.
If applicable, customize the code style using the controls on the page.
Check code against the Drupal coding standards
With PhpStorm, you can use the PHP_CodeSniffer tool, which detects coding standard issues, in combination with Coder, which provides a set of Drupal-specific standards to PHP_CodeSniffer. This will ensure that your code is clean, consistent, and free of some common errors.
To get started, install PHP_CodeSniffer using any technique described in Install and configure PHP_CodeSniffer. Probably the easiest way is to install it with Composer.
Install PHP_CodeSniffer
Inside composer.json, add the
squizlabs/php_codesniffer
dependency record to therequire
orrequire-dev
key. To get code completion for the package name and version, press Ctrl+Space.Do one of the following:
Click the Install shortcut link on top of the editor panel.
If the Non-installed Composer packages inspection is enabled, PhpStorm will highlight the declared dependencies that are not currently installed. Press Alt+Enter and select whether you want to install a specific dependency or all dependencies at once.
Next, install Coder, which will provide Drupal-specific standards to PHP_CodeSniffer.
Install Coder
Inside composer.json, add the
drupal/coder
dependency record to therequire
orrequire-dev
key. To get code completion for the package name and version, press Ctrl+Space.Do one of the following:
Click the Install shortcut link on top of the editor panel.
If the Non-installed Composer packages inspection is enabled, PhpStorm will highlight the declared dependencies that are not currently installed. Press Alt+Enter and select whether you want to install a specific dependency or all dependencies at once.
The Coder package will be installed, and the corresponding Drupal standard will be selected for the PHP_CodeSniffer validation inspection automatically. If necessary, you can further customize the inspection on the page of the Settings dialog (Ctrl+Alt+S) . For more information, refer to Enable PHP_CodeSniffer as a PhpStorm inspection.
View the Drupal API documentation from PhpStorm
In the PhpStorm editor, select the symbol you are interested in and choose Search in Drupal API from the context menu. The Drupal API Documentation opens.
Use the Drush command line tool from PhpStorm
PhpStorm integrates with the Drush command line shell and scripting interface version 5.8 and later. Before you begin, download and install Drush as described in the official Drush documentation.
Configure Drush automatically
When you install Composer dependencies in a Drupal project, PhpStorm detects and configures Drush automatically and displays a respective notification in the Composer Log.
If you want to customize the tool, click in the gutter to quickly jump to the PHP Command Line Tool Support settings page.
Configure Drush manually
In the Settings dialog (Ctrl+Alt+S) , go to .
Click on the toolbar.
In the Command Line Tools dialog, choose Drush from the list, and specify its visibility level (Project or Global).
When you click OK, the Drush dialog opens.
Specify the path to the Drush executable. The default location is commonly C:/ProgramData/Drush/drush.bat on Windows and /usr/bin/drush on macOS or Linux. If you followed the standard installation procedure, click OK to accept the predefined path.
In case of custom installation, type the path to the Drush executable file and click OK.
Click OK to apply changes and return to the PHP Command Line Tool Support page. Optionally, click to edit the tool properties, or to customize the commands set. For more information, refer to Customize a tool.
Run Drush commands
Go to
or press Ctrl twice.In the Run Anything window that opens, type the call of the command in the
<drush> <command>
format.The command execution result is displayed in the Run tool window.
Terminate a command
Click on the Run tool window toolbar.
Debug Drush commands
Drupal commands are defined in controller classes that extend DrushCommands
. To debug a command, it is crucial that you initiate a debugging session for the command itself, and not the controller class file it is defined in. Otherwise, the Drupal bootstrapping process will be skipped, and the execution will fail.
In the controller class corresponding to the selected command, click the editor gutter at a code line where you want to set a breakpoint.
Create a run/debug configuration that will run the drush tool with the selected command. In the main menu, go to , then click and choose PHP Script from the list.
In the PHP Script dialog, provide the run/debug configuration parameters.
In the File field, provide the path to the drush executable file.
In the Arguments field, type the actual command and its arguments, such as
status
.
On the PhpStorm toolbar, select the created run/debug configuration and click . The command execution will stop at the specified breakpoint.
Using Drupal 8 with Symfony
PhpStorm provides close integration between Drupal, version 8, and Symfony. Through this integration, Symfony components are connected with Drupal infrastructure. To take advantage of this integration:
Install the Drupal Symfony Bridge plugin
In the Settings dialog (Ctrl+Alt+S) , navigate to the Plugins page. Click Browse Repositories, select the plugins and click Install. For more information, refer to Install plugins.
Enable annotations
To get advanced annotations support, install the PHP Annotations plugin as described in Install plugins.
What coding assistance for Drupal 8 - Symfony integration is available?
Strings suitable for use inside Drupal-specific t() function are indexed across your project and offered for completion.
Completion for relevant yml key values in url() and other Drupal API functions, which makes search for the right value easier.
Navigation to the yml file by pressing Ctrl+B or choosing Navigate | Go To Declaration.
Full support for service containers described in yml files, including completion and navigation with Ctrl+B.
Support of the Twig template engine, which is now the default template engine for Drupal 8, including completion, navigation, and recognition of Drupal functions. See also Twig in Drupal 8.