Phpstorm Drupal

Posted on  by 



Skip to end of metadataGo to start of metadata

So for Drupal testing, you may want a little bit more. Make absolutely certain that drush and drupal are both contained in the same PHPstorm project. I often just clone drush into the drupal root (and of course don't check it into my project.). PHPStorm really wants to find all the code in the same project. JetBrains PhpStorm is a commercial PHP IDE that you can configure to work with your Pantheon sites. This guide explains how to use the Composer integration of JetBrains PhpStorm to install Drupal 8. Drupal Cloud and Marketing Cloud advances help organizations assemble packaged business capabilities into new digital experiences—quickly and flexibly—to gain first-mover advantage. We’re proud to announce updates to the Acquia Open Digital Experience Platform (DXP) designed to help marketers and developers architect the composable.

Overview

Navigating the codebase of a Drupal project can be a tricky task. Using an integrated development environment (IDE) like PhpStorm can really help simplify many of these common tasks. PhpStorm, in particular, has excellent support for Drupal projects including code completion, syntax checking, and integration with things like version. 1) Set up a project for Drupal 8 in PHPStorm. Open PHPStorm and select 'Create New Project from Existing Files'. In the next screen, select 'Web server is installed locally, source files are located under its document root.' And press 'Next'. In the next step, PHPStorm will ask you to select the root folder of the Drupal.

PhpStorm: A Drupal-friendly IDE

  • Easy to configure, especially when using Acquia Dev Desktop + XDebug
  • Download: http://www.jetbrains.com/phpstorm/
  • 30-day trial available
  • Similar to other JetBrains IDEs, e.g., RubyMine, IntelliJ: http://www.jetbrains.com/products.html

Configuration

  • Key Map: Mac OSX 10.5+
  • IDE theme: Darcula
  • Editor color/fonts: Darcula
  • Enable command line

Drupal Code Style

  • General: Code Style: 80 right margin; Tab size/indent/continuation: 2
  • PHP: Set from Predefined Style > Drupal; Tabs: All 2; Wrapping and Braces: check 'catch on new line' for 'try'

XDebug with Acquia Dev Desktop

Per https://dev.acquia.com/blog/debugging-drupal-8/debugging-drupal-8-in-phpstorm-part-2-local-webbased-debugging-in-mac-os-x-acquia-dev-desktop-2-and-xdebug/17/08/2016/16426:

Tip per BW: A potential gotcha you may encounter with the below setup: If you forget to hang up the phone in any of your PHPStorm windows you will find that PHP commands/scripts from you terminal sessions mysteriously hang. For that reason I only use the phone when debugging drush code.

Phone + Chrome extension Xdebug Helper approach (see also Manual below):

1. Uncomment path to xdebug.so in appropriate php.ini file (/Applications/DevDesktop/php5_6/bin/php.ini):

At the very bottom of php.ini, add following two lines:

Restart DevDesktop and check xDebug via php.info (Dev Desktop > Preference > Config or admin/reports/status/php for a particular site)

2. In PhpStorm, make sure phone button (“Start listening for PHP debug connections”) is enabled

3. Chrome setup
a. Install Chrome extension Xdebug Helper
b. Leave all its settings default, you will not need to change anything for PhpStorm
c. Switch the grey Xdebug icon on Google Chrome toolbar to green

4. Set breakpoints and Refresh browser with CMD+R

From https://dev.acquia.com/blog/debugging-drupal-8/debugging-drupal-8-in-phpstorm-part-2-local-webbased-debugging-in-mac-os-x-acquia-dev-desktop-2-and-xdebug/17/08/2016/16426:
In the “Frames” window at bottom-left side you can see the backtrace route of the script you are running. Similar to debug_print_backtrace() command, but with a significant difference: you can click on every trace and see actual variables when the function below was called. Incredibly helpful!
In the “Variables” window at bottom-right side you can see a list and values of variables currently available to the method you are inside. Objects and arrays are expandable, so you can jump inside. In addition to that:
You can see all PHP globals which are part of each PHP script
You can set values to simple variables like strings and continue running the code with changed variables
You can navigate forward in the script with the keyboard shortcuts or arrows near the Debugger tabs. You can step forward, step into method, step out, run to the cursor, set the list of methods to ignore and much more
You can not navigate backward in the script because of nature of the debugging. To navigate 'backward“ you need to stop debugging, set the breakpoint to the sooner state of the script and refresh the page.

Manual (not using Chrome extension Xdebug Helper:

/Applications/DevDesktop/php5_6/ext/xdebug.so

Edit php.ini, uncomment zend_extension

At the very bottom of php.ini, add following two lines:

Restart Acquia Drupal Stack, visit phpinfo() and confirm xdebug

Visit php.info via Dev Desktop > Preference > Config or
Visit phpinfo via admin/reports/status/php

PhpStorm: Run > Edit Configurations: Add Php Web Application
Add Server: e.g., Host: d8-theming.dd, Port: 8083, Debugger: XDebug
If working, will open default browser (can set breakpoints to test)

If you get error: 'PHP Interpreter is not configured':

  • Click 'configure PHP interpreter'
  • Click '+' sign and add path to php
  • /usr/local/bin/php
  • Should select Php 5.6.14

Set breakpoint(s), then

Debug 'd8-theming.dd' will launch:

Troubleshooting

If your debugger is not being triggered, PHPStorm has a validation script you can use to detect problems in your configuration.

In my case, PHPStorm was loading the php.ini for ADD PHP version 7.1 even though the site was running PHP 5.6. I edited the php.ini in the 7_1 folder to include the three necessary lines and then debugging was triggered. Not sure if there are going to be problems debugging a 5.6 site with PHP 7.1.

Older 2013 approach

  • If using Acquia Dev Desktop, likely already has xdebug extension (/Applications/acquia-drupal/php5_3/ext/xdebug.so)
  • Edit php.ini file:


  • Restart Acquia Drupal Stack, visit phpinfo() and confirm xdebug
  • PhpStorm: Run > Edit Configurations: Add Php Web Application
  • Add Server: e.g., Host: mysite.localhost, Port: 8082, Debugger: XDebug
  • If working, will open default browser (can set breakpoints to test), e.g.: http://mysite.localhost:8082/?XDEBUG_SESSION_START=14430

XDebug with brew-installed PHP

Setup the homebrew-php tap

Add xdebug for the phpversion(s) that you've installed:

Phpstorm Drupal

brew will add the zend_extension line to a conf.d/ file like /usr/local/etc/php/5.5/conf.d/ext-xdebug.ini:

The lines below that are the xdebug config that I use. 'max_nesting_level' is the most important one.


xDebug configuration for debugging Drush commands

If you are using path mappings in your project you should set this variable [ref|https://blog.jetbrains.com/phpstorm/2012/03/new-in-4-0-easier-debugging-of-remote-php-command-line-scripts/]:

To tell PhpStorm which path mapping configuration it should use for a connection from a certain machine you need to set the value of the “PHP_IDE_CONFIG” environment variable to “serverName=SomeName”, where ‘SomeName’ is the name of the server configured in ‘Project Settings | PHP | Servers’.

Now for the magical setting that makes drush work ([Thanks Moshe|https://github.com/drush-ops/drush/issues/1938#issuecomment-175226384].): phpStorm Preferences > Languages > PHP > Debug: Set Max simultaneous connections to something like 19!

Now you should be able to set a break point in a hook_update_N and break at it when you run drush updb -y.

Connect to Database(s)

  • From Database panel, create new data source with command+N
  • Select 'MySQL Connector/J-5.1.22' for driver files
  • Download files (mysql-connector-java-5.1.22-bin.jar and mysql-connector-license.txt) - add to .gitignore if saving to repo
  • Enter DatabaseURL (jdbc:mysql://[host][,failoverhost...][:port]/[database]), user, password
    • Get Pantheon database connection info from Apollo dashboards
    • Use Console button for sql command line

    • In 'Schemas & Tables' tab, specify schema to select tables for retrieval and/or set default:
      http://www.jetbrains.com/phpstorm/webhelp/db-data-source-properties.html#d97128e696

Resolving Conflicts

  • Select VCS > Git > Resolve Conflicts
  • If you know you want to 'Accept Yours' or 'Accept Theirs' for ALL modifications, you can do so from this screen.
  • Otherwise, click the 'Merge' button to get a 3 panel comparison screen that will allow you to walk through each modification and decide which version to keep (click the double arrows from the left or right panel to accept the modification, which will show up in the center panel).
  • When finished, you'll need to commit your changes per your normal process.

Enable Live Templates for Drupal Hook Completion

Before doing the 'push' command above:

Start phpStorm and Cmd-, then search for Keymap. Then search keymap for 'template.' For me ^J is 'insert live template.'

Type hook_menu_alt^J[Enter]

and see:

Major Version Upgrades

When upgrading from v8 to v9 I found that I couldn't just use phpStorm > Check for Updates. Instead:

  1. Download the latest .dmg for the new version
  2. In Finder visit Applications and rename your current phpStorm.app
  3. Drag the new phpStorm.app to Applications.
  4. Launch the new app
    1. You will be prompted to import your settings (which live in ~/.Webide*/)
    2. You'll be prompted to apply our license. I do this with the 'login to JetBrains' option.


Optional

  • PhpStorm Quickstart: http://www.jetbrains.com/phpstorm/quickstart/index.html
  • PHP Code Sniffer: https://drupal.org/node/1419988
  • Drupal Plugin for Drupal-specific features (part of Php Storm 7 Early Access Program beginning July 2013): http://blog.jetbrains.com/phpstorm/2013/07/drupal-development-using-phpstorm/

Tips

Compare Git Branches

  • Checkout specific branch
  • VCS > Git > Compare with Branch...
  • Select branch to compare with:
  • PHP Storm will show files that have changed (in blue by default)


  • Double-click the file to see the side-by-side comparison

Show Changes View

  • VCS > Show Changes View to display recent local changes (e.g., pending commit)
  • Can then select a specific file and then select the icon for 'Show diff' to display side-by-side comparison

Debugging cli scripts that call exec()

(This probably applies to other functions like fork() as well.)

Our automation scripts use exec() to call drush and terminus. If you place a breakpoint after the first exec() in the code the debugger will hang and you have to hit ^C (or the stop button on the phpStorm debug pane). I asked David Snopeck if there was a way to debug through exec()s and he said:

So, the way xdebug works is that your PHP process attempts to connect to your debugger process (usually the IDE) at a particular host/port which is configured in php.ini (xdebug.remote_host and xdebug.remote_port). If you could have a 2nd debugger running on a different port, and somehow exec() the 2nd PHP process with a different php.ini (via: 'php --php-ini other-php.ini actual-php-script.php' or something) which points at that port, then the 2nd debugger *should* debug the 2nd process.

Today (whille using phpStorm 9) I discovered a workaround:

  1. Place a breakpoint after an exec()
  2. Select Run > Break at first line in PHP scripts. (Optional: Helps you verify that the debugger has fired up.)
  3. Enable 'Start listening for debug connections'
  4. Execute the script from the terminal.
  5. Hit F9 to continue when it breaks at the first line.
  6. The debugger will hang. I wait a reasonable period for the exec() code to return then I disable 'listening for debug connections' and the script hit's the break point after the exec and I can continue debugging.

I'm not sure what happens if you hit another exec(). Maybe as soon as debugging resumes, you could re-enable 'listening for debug connections' and follow the same pattern.

Resources

Drupal

The phpStorm Blog is interesting to follow.

The phpStorm Video Tutorials are nice too.

Known Issues

Phpstorm Drupal 9 Support

PhpStorm 7.1.3: Git merge freezes after resolving conflicts

Phpstorm Php Version

Description: When using VCS > Git > Resolve Conflicts after resolving the last conflict, PhpStorm will crash (must Force Quit to get out of application). This is a known issue.

Phpstorm Wordpress Development

  • Workaround: If you press the <return> key immediately after resolving the last conflict the keyboard focus is still on the hidden dialog and it applies the merge (and presumably the <esc> key will cancel it).

Solution: Instead of using the Workaround, this can be fixed via the following steps:


  1. Press Cmd+Q: If you see the dialog at the top as a 'sheet' you likely have the bug
  2. Press Cmd+Alt+Shift+/ and choose 'Registry' from the popup
    I think it's Ctrl+Alt+Shift+/ (not CMD)
  3. In the registry dialog find the key: ide.mac.message.dialogs.as.sheets and uncheck it.
  4. Close the window (The ticket says 'Use the cross button in the top left corner of the window. Not the Close button' but closing via button seemed to work fine for me too.)
  5. Press Cmd+Q: Once fixed, the dialog should show up as a normal prompt


Note: Setting 'ide.mac.message.dialogs.as.sheets=false' to /Applications/PhpStorm/bin/idea.properties sounds like it works too, but the above seems like an easier way to keep track of registry values.

Phpstorm Php Interpreter

When using OSX and XDebug, unable to see long string values

Update: Appears to be fixed with Php 7.1.3

'Click to see the full value' link does not work.

Phpstorm Drupal 8

Open JetBrains ticket: http://youtrack.jetbrains.com/issue/WI-17166#

Workaround: Right-click the variable and select 'Copy Value' then paste to a new text file.

Phpstorm Drupal 9






Coments are closed