PhpStorm 2024.3 Help

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

  1. Open Settings (Ctrl+Alt+S) | PHP | Frameworks.

  2. Extend the Drupal node, select the Enable Drupal integration checkbox, and specify the parameters as follows:

    Enable Drupal support in settings

    Drupal installation path

    Specify the path to the root directory that contains the installed Drupal core.

    Set up PHP | Include paths

    Select the Set up PHP | Include paths checkbox to have PhpStorm automatically add Drupal core directories to the project's include paths.

    To configure the paths manually, open Settings (Ctrl+Alt+S) | PHP and edit the list of direectories on the Include path tab.

    Add Drupal directories to Include paths

    Version

    Choose the version of Drupal to use, the supported versions are 6, 7, 8, and 9.

  3. 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.

  1. Open the New Project dialog by choosing File | New Project in the main menu or clicking Create New Project on the PhpStorm Welcome screen.

  2. In the left-hand pane, select Drupal Module.

  3. In the right-hand pane, specify the Drupal support parameters.

Option 3: Import an existing Drupal module project

When you open (File | 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.

drupalStructureDetected.png

Click Enable Drupal Support to open the modal dialog and specify the parameters.

Drupal settings dialog

    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 Drupal files with the PHP file type

    Associate file types manually

    1. In the Settings dialog (Ctrl+Alt+S) , navigate to Editor | File Types.

    2. On the File Types page that opens, select PHP on the Recognized File Types list.

    3. In the File name patterns area to the right, click (the Add button) and add one by one the *.test, *.install, *.engine, *.profile, and *.theme file name patterns.

      Associate Drupal files with the PHP file type

      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.

      Drupal Hook Name Completion
    • Navigate to hook invocations.

      • To navigate to a hook invocation from the editor, click the the Implementing Drupal Hook icon 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.

        Choose Hook invocation

        You will be navigated to the line where the relevant hook is invoked with module_invoke_all(), module_invoke(), or drupal_alter().

    • View hook documentation.

      Place the caret at the name of the hook and choose View | Quick Documentation Lookup or press Ctrl+Q.

      Quick documentationlookup

    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).

    Enable Drupal style formatting

    Change code style settings manually

    You can also set the predefined code style manually on the Code Style: PHP page.

    1. In the Settings dialog (Ctrl+Alt+S) , navigate to Editor | Code Style | PHP.

    2. Click Set from and choose Drupal from the drop-down list that opens.

    3. 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

    1. Inside composer.json, add the squizlabs/php_codesniffer dependency record to the require or require-dev key. To get code completion for the package name and version, press Ctrl+Space.

    2. 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

    1. Inside composer.json, add the drupal/coder dependency record to the require or require-dev key. To get code completion for the package name and version, press Ctrl+Space.

    2. 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 Editor | Inspections 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.

    View Drupal API documentation

    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

    Configure Drush manually

    1. In the Settings dialog (Ctrl+Alt+S) , go to Tools | PHP Command Line Tool Support.

    2. Click the Add button on the toolbar.

    3. In the Command Line Tools dialog, choose Drush from the list, and specify its visibility level (Project or Global).

      the Command Line Tools dialog
    4. 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.

    5. Click OK to apply changes and return to the PHP Command Line Tool Support page. Optionally, click the Edit button to edit the tool properties, or the Edit Source button to customize the commands set. For more information, refer to Customize a tool.

    Run Drush commands

    • Go to Tools | Run Command 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.

      Running a drush command

    Terminate a command

    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.

    1. In the controller class corresponding to the selected command, click the editor gutter at a code line where you want to set a breakpoint.

      Set a breakpoint in a Drupal command
    2. Create a run/debug configuration that will run the drush tool with the selected command. In the main menu, go to Run | Edit Configurations, then click the Add button and choose PHP Script from the list.

      Run/Debug configuration: PHP script
    3. 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.

      Run/Debug Configurations dialog for Drupal CLI command
    4. On the PhpStorm toolbar, select the created run/debug configuration and click the Debug button. The command execution will stop at the specified breakpoint.

      Debug a Drupal command

    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:

    1. 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.

    2. 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.

    Last modified: 11 October 2024