IntelliJ IDEA 2017.2 Help

Drupal

This feature is supported in the Ultimate edition only.

You can use IntelliJ IDEA as an IDE for Drupal development including modules, themes, and core. The supported versions are 6, 7, and 8.

IntelliJ IDEA provides integration between the Symfony2 framework and Drupal 8 while developing Drupal modules and core.

In this part:

Where do I get Drupal from?

Download Drupal from the Drupal Official website and install it as described in the Drupal installation instructions.

How do I set up a Drupal project in IntelliJ IDEA?

Option 1: Create a IntelliJ IDEA project with a Drupal module
You can create a IntelliJ IDEA project by a Drupal Module template, this project will be set up and configured in accordance with the Drupal requirements.

  1. Open the New Project dialog
    Choose File | New | Project or click Create New Project on the Welcome screen.
  2. Choose the project type and location
    In the left-hand pane, click PHP from the list, then choose Drupal Module in the right-hand pane, and then click Next. On the second page of the wizard, specify the project name and the folder where it will be created.
  3. Choose Drupal installation to use
    Specify the root folder of the Drupal installation and choose the version of Drupal to use, the supported versions are 6, 7, and 8.
  4. Configure Include paths
    Select the Set up PHP | Include paths check box to have Drupal include paths automatically configured for the project. After you leave the dialog box, the following paths will be added to the Include Paths list on the PHP page: <drupal installation root>/includes, <drupal installation root>/modules, and <drupal installation root>/sites/all/modules

Option 2: Import an existing Drupal module
When you open an existing Drupal module, IntelliJ IDEA recognizes the Drupal-specific structure and suggests activating the Drupal support.

  1. Open your project
    Click Open on the Welcome screen or choose File | Open on the main menu, then choose the folder where your Drupal module is stored. IntelliJ IDEA detects a Drupal-specific structure and shows a notification:
    drupalStructureDetected.png
  2. Enable Drupal support
    Click Enable in the notification. In the Drupal Module dialog box that opens, select the Enable Drupal integration check box and proceed as when creating a project with a Drupal module: specify the root folder of the Drupal installation, choose the version to use, and configure include paths.

Whether you enable the Drupal support in an existing IntelliJ IDEA project or create a new project with a Drupal module, IntelliJ IDEA checks if the development environment is configured properly for Drupal development.

Any detected inconsistency is reported in the Event Log tool window and as a pop-up. For each discrepancy IntelliJ IDEA suggests a fix. To apply a suggestion, click the link next to the reported event.

/help/img/idea/2017.2/drupalEventLog.png

How do I associate Drupal-specific files with the PHP file type?

IntelliJ IDEA recognizes and treats files as php files and provides code highlighting based on file type associations.

Option 1: Use the Event log
In the Event Log tool window, click Fix next to the Drupal support: <*.file extension> files are not associated with PHP file type message.

Option 2: Use the File Types page
Open the File Types page (File | Settings | Editor | File Types for Windows and Linux or IntelliJ IDEA | Preferences | Editor | File Types for macOS) and define file masks in the Registered Patterns area. See Creating and Registering File Types for details.

How do I change the Drupal settings?

  • Enable or disable Drupal integration
    On the Frameworks page (File | Settings | Languages and Frameworks | PHP | Frameworks for Windows and Linux or IntelliJ IDEA | Preferences | Languages and Frameworks | PHP | Frameworks for macOS), toggle the Enable Drupal integration check box to activate or deactivate Drupal in the current IntelliJ IDEA project.
    To use another Drupal installation, type the path to the relevant installation folder. Change the version if necessary.
  • Update the include paths
    On the PHP page (File | Settings | Languages and Frameworks | PHP for Windows and Linux or IntelliJ IDEA | Preferences | Languages and Frameworks | PHP for macOS), make the required changes in the Include Paths area.

How do I use Drupal hooks in IntelliJ IDEA?

IntelliJ IDEA provides full native support of Drupal hooks in .module files.

Step 1: Use code completion for hoo declarations
IntelliJ IDEA 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.

/help/img/idea/2017.2/drupalHookNameCompletion.png

Step 2: Navigate to hook invocations

  • To navigate to a hook invocation from the editor, click the /help/img/idea/2017.2/gotoHookInvocation.png icon in the gutter.
  • In case of multiple invocations, IntelliJ IDEA displays a list of available hook invocations so you can choose which one to jump to.
    /help/img/idea/2017.2/chooseHookInvocation.png
    You will be navigated to the line where the relevant hook is invoked with module_invoke_all(), module_invoke(), or drupal_alter().

Step 3: View hook documentation
Position the cursor at the name of the hook in question and choose View | Quick Documentation Lookup or press Ctrl+Q.

How do I set up the Drupal code style in a IntelliJ IDEA project?

IntelliJ IDEA automatically offers to apply the pre-configured Drupal Coding Standards (code style) if a project is recognized as a Drupal Module, or if the Drupal integration is enabled in an existing project, or when you create a new project with a Drupal Module. However, you can at any time change or customize this setting on the Code Style: PHP page of the Settings dialog box.

To use the pre-configured Drupal code style in a project
In the Event Log tool window, click Set next to the Drupal-style formatting can be set for this project message.

How do I check my code against the Drupal coding standards?

With IntelliJ IDEA, you can use two tools that detect violations against the Drupal coding standard: Coder and PHP Code Sniffer. They ensure your code remains clean and consistent and help prevent some common semantic errors made by developers.

Option 1: Use PHP Code Sniffer

  1. Install PHP Code Sniffer as a PEAR package or using Composer. For detailed installation instructions, see https://github.com/squizlabs/PHP_CodeSniffer.
  2. Register PHP Code Sniffer in IntelliJ IDEA and configure it as a IntelliJ IDEA inspection as described in PHP Code Sniffer.

Option 2: Use Coder

  1. Download the Drupal Coder module (7x-2.0 version is recommended).
  2. Unpack the downloaded archive and find the coder_sniffer/Drupal subdirectory inside. This directory should contain the ruleset.xml file and some other subdirectories.
  3. Move the contents of coder_sniffer/Drupal to <php installation folder>/CodeSniffer/Standards/Drupal.

How do I view the Drupal API documentation from IntelliJ IDEA?

In the IntelliJ IDEA editor, select the symbol you are interested in and choose Search in Drupal API on the context menu. The Drupal API Documentation opens.

How do I use the Drush command line tool from IntelliJ IDEA?

Step 1: Download and install Drush
Download and install Drush as described at https://github.com/drush-ops/drush.

Step 2: Configure Drush as a command line tool

  1. Open the Command Line Tool Support page
    Choose File | Settings | Tools | Command Line Tool Support for Windows and Linux or IntelliJ IDEA | Preferences | Tools | Command Line Tool Support for macOS. The Command Line Tool Support page opens.
  2. Specify the tool type and visibility
    Click Add and choose Drush in the Choose Tool to Add dialog. Choose whether Drush will be available in the current project (Project visibility or across all IntelliJ IDEA projects (Global visibility). When you click OK, the Drush dialog opens.
  3. Specify the Drush executable
    In the Drush dialog that opens, IntelliJ IDEA has automatically filled in the default executable location, which is usually C:/ProgramData/Drush/drush.bat on Windows and /usr/bin/drush on macOS or Linux. If you followed the standard installation procedure, the predefined path will be correct, just click OK.
    In case of custom installation, type the path to the Drush executable file and click OK.

    In either case, IntelliJ IDEA loads command definitions automatically and returns to the Command Line Tool Support page.

  4. Specify the alias for Drush
    In the Alias text box, specify the alias to use in calls of tool commands. Accept the default alias or edit it, if necessary.
  5. Activate Drush
    Select the Enable check box to activate the detected command set.

Step 3: Run Drush commands
Open the Input pane (Tools | Run Command) and type <alias> (drush by default) and press Ctrl+Space to invoke completion. The result of command execution is shown in the Output tab with the name of the command. Learn more from How do I run a command?.

How do I use Drupal 8 with Symfony2?

IntelliJ IDEA provides close integration between Drupal, version 8 and Symfony2 which connects Symfony2 components and Drupal infrastructure. To take advantage from this integration:

Step 1: Install the Drupal Symfony2 Bridge plugin
Open the Plugins page (File | Settings | Plugins for Windows and Linux or @product@ | Preferences | Plugins for macOS), click Browse Repositories, select the plugins and click Install. For details, see Installing, Updating and Uninstalling Repository Plugins and in Enabling and Disabling Plugins.

Step 2: Enable annotations
To get advanced annotations support, install the PHP Annotations plugin as described in Installing, Updating and Uninstalling Repository Plugins and in Enabling and Disabling Plugins.

What coding assistance for Drupal8 - Symfony2 integration do I get?

  • Completion for major Drupal-specific parameters in <module_name>.info.yml files that contain information about Drupal modules.
  • 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.

See Also

Last modified: 25 July 2017