Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: proofreading

Redirect
locationhttps://www.jetbrains.com/help/phpstorm/joomla-specific-coding-assistance.html
delay2

Include Page
Social Buttons PhpStorm
Social Buttons PhpStorm

...

Note
iconfalse

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

...

.

Note
iconfalse

Joomla is an award-winning content management system (CMS), which enables you to build websites and powerful online applications. Many aspects, including its ease-of-use and extensibility, have made Joomla the most popular website software available. Best of all, Joomla is an open source solution that is freely available to everyone.

Image Added

Table of Contents
maxLevel3

Enabling

...

Joomla! Integration in an Existing PhpStorm Project

...

Enabling Drupal Integration in an Existing PhpStorm Project

...

 When we open an existing Joomla! Project in PhpStorm, PhpStorm should detect that we'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.

Image Removed

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.

Image Removed

Drupal integration settings can be changed in Settings | Drupal.

Image Removed

Creating a New Drupal Module

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

Image Removed

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.

Image Removed

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

Image Removed

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.

Image Removed

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.

Image Removed

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.

Image Removed

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.

Image Removed

Image Removed

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

Image Removed

Coding Standard can be always re-configured by going to Settings | Code Style | PHP

Image Removed

Hook Support

Note
iconfalse

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.

Image Removed

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:  Image Removed

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:

Image Removed

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.

Image Removed

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

Image Removed

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

Image Removed

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

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

Image Removed

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.

Image Removed

Drupal Command Line Tool Drush Integration

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: Image Removed

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.

Image Removed

Image Removed

Image Removed

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

Image Removed

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.

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.

Image Removed

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

Image Removed

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.

1. Install PHP Code Sniffer tool as a PEAR package or using the Composer tool. Installation instructions are available in PhpStorm web help and the PHP Code Sniffer webpage.

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.

Image Removed

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.

Image Removed

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.

Image Removed

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.

Image Removed

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.

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.

Image Removed

Specify the commit message on the relevant tab if necessary.

Image Removed

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 

Code Block
languagehtml/xml
<td class="views-field views-field-title" >\s*<a href="/node/({id}.+?)">({summary}.+?)</a>

Image Removed

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.

Image Removed

Symfony2-specific features for Drupal 8 and higher (via Drupal Symfony2 bridge plugin)

Note
iconfalse

Image Removed
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 Symfony 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.

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:

Image Removed

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.

Image Removed

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.

Image Removed

In case of any problems, select File | Invalidate Caches / Restart to reindex your project.

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

Image Removed

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:

Image Removed

Completion for routing (URL generation) and navigation to routing YAML files

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

Image Removed

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

Image Removed

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

Image Removed

Image Removed

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.

Image Removed
Image Removed

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.

Image Removed

Image Removed

Image Removed

Image Removed

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

Drupala Joomla! application and ask us to enable the Joomla! integrations.

Image Added

 

When we click Enable, PhpStorm will ask us to browse to the location where the Joomla! codebase is installed. In my case, it is the root of the project I'm working on. It's also worth clicking the second prompt that asks you if you want to detect the namespace roots; this saves us having to configure the paths for the project manually. If you don't get prompted by the Joomla! Support prompt, you can enable Joomla! support manually by opening the preference pane, and navigating to Languages & Frameworks then PHP and Joomla! Support.  

Image Added

Once we've enabled Joomla! support, PhpStorm asks if we would like to enable the Joomla! code styles and if we would like to enable the Joomla! docblock templates. Joomla! has its own rigorous code style and docblocks, so enabling these is always a good idea; we'll learn more about these later.

Image Added

Creating a New Joomla Module/Plugin/Component/Empty Joomla! Project

To create a new Joomla project, go to File then New Project... and select Joomla! Integration from the left-hand options. You'll need to tell PhpStorm where to find your Joomla! installation path (unless you've already specified it at the previous step), give your project a name, and select what type of Joomla! project you're going to create. Here we're creating a new Joomla! module.

Image Added

PhpStorm will then create the `joomla-module.php` file, and the `joomla-module.xml` file that you need to use this project as a Joomla! module. You'll need to edit the configuration XML file before you can use the module.

Development Environment Configuration

Joomla! Include Paths

With Joomla! support enabled and PhpStorm knowing where your Joomla! install is, the path to this Joomla! install should be included in your include paths by default. You can check this by selecting External Libraries in the left-hand project browser, and check that you can see joomla library root under PHP. If you don't see this, you'll need to add the path to your Joomla! install to your project manually in Settings | PHP | Include path.

Image Added

Joomla! Coding Standard (Code Style)

PhpStorm ships with a coding standard for Joomla! code style, and you should be asked if you want to enable this when you enable Joomla! support. If you wish to enable the Joomla! code style manually, browse to Editor, Code Style then PHP in the preference pane. There, you can add the pre-defined Joomla! styles by clicking Set From..., and selecting Predefined Style then Joomla!

Image Added

JHtml::_($argument) and JText::_($argument)  support

JHtml::_($argument) support

The latest versions of PhpStorm come with support for the ` _` static method of the `JHtml' class. This is a magic method that allows you to invoke helpers by passing in a string that contains the class name plus the method name, separated by a dot. PhpStorm can parse these strings, and provide you with the usual helpers associated with class and method names in the IDE. For example, here we are using the `form.token` helper, and when we invoke Brief Info over the string (default to CMD/CTRL plus hover over the item), we see the method signature for this helper method:

Image Added

Holding CMD/CTRL and clicking the string takes us straight to the `token` static method of the `JHtmlForm` class just as we would expect. Of course, the `JHtml` class comes with full code completion and type hinting like all classes do in PhpStorm.

JText::_($argument) support

Similarly to JHtml support, PhpStorm now also supports the static methods of the `JText` class. Text allows you to handle translations from definitions (typically in .ini files) and output the translation in the correct place. When you use the `_` or `sprintf` or 'script' methods of `JText` and pass in a valid string with a key name, PhpStorm will allow you to use Brief Info and navigate to the definition (using CMD/CTRL and click) to be taken directly to the .ini file that defined that key.

Image Added

Databases

Data source detection

PhpStorm comes with a database browser built right in, and the Joomla! integrations allow us to quickly and easily configure the database tool from the configuration file that Joomla! creates containing our credentials. Once we've opened the Database tool (I usually hover over the menu icon in the bottom left of the screen and select Database), we can add a new data source for our Joomla! database by clicking the + icon and then selecting Import from sources...

We then see the Add New Datasource dialog, but with all the fields completed from the settings in the `configuration.php` file. Simply click Test Connections to check everything is working, and then click OK.

Joomla DatasourceImage Added

Database prefix support

You can see that Joomla! adds a prefix to the database tables (which is generated or configured during the installation wizard), and this can make writing queries in the query editor (part of the database tool) quite painful. 

PhpStorm provides database prefixes support and changes #__  on fly to the prefix that is defined in the $dbprefix field in the configuration.php file. You will also have completion working for your SQL queries.To have it working, make sure that SQL Dialect for project is equal to Database (Settings-> SQL Dialects).

Image Added

If you Ctrl+Click, you can navigate directly to the table from the query.

Image Added


Joomla! Doc-blocks standard support

Joomla! code styles have strict standards about docblocks - including which docblock annotations are required, which are optional, and which order they should be presented in. PhpStorm now ships with an inspection that will tell you exactly what is wrong with your docblocks, and why they don't meet Joomla! strict standards.

Image Added

Joomla! CodeSniffer

Joomla! provides a ruleset for use with PHP CodeSniffer, and PhpStorm comes with support for PHP CodeSniffer out of the box.

Image Added

Refer to the PHP Code Sniffer in PhpStorm tutorial with the Joomla! CodeSniffer standards to add more inspections for code style within PhpStorm.

Debugging and Profiling Joomla! Applications with PhpStorm

Joomla!-based projects can be debugged and profiled without any DrupalJoomla!-specific configuration.

Please proceed with standard PhpStorm debugging or profiling workflow. For additional details on debugger and profiler configuration, please check this tutorial or or relevant videos on the video tutorials page .

Video Tutorial on Drupal Development using PhpStorm

Youtube
height360
width640
videohxh4p-XJ52M


Download latest version of PhpStorm for your platform right now >>

...