In a recent Magento 2 code drop into the develop branch on GitHub, support was added for allowing Composer installed modules to stay in the ‘vendor’ directory rather than be copied to ‘app/code’. This however caused problems with XSD references in XML files that used relative paths, as the module could be in different directories at different times. To overcome this problem, new URNs have been introduced for referencing XML Schema declarations. These need to be configured in PHP Storm in order for schema validation to work again. This note is a quick note describing how to do these steps. (Official documentation is coming really soon.)
Some examples of XSD URIs that have changed:
- “../../../../../lib/internal/Magento/Framework/Config/etc/theme.xsd” can now be replaced with “urn:magento:framework:Config/etc/theme.xsd”.
- “../../../../../Magento/Webapi/etc/webapi.xsd” can now be replaced with “urn:magento:module:Magento_Webapi:etc/webapi.xsd”.
The new URNs no longer care about the path relative to the other module (which gets messed up when you have some modules under ‘vendor’ and some locally developed modules still under ‘app/code’). Relative paths still work, but are not recommended any more.
If you are using PHP Storm however, it by default does not know how to resolve URNs. The URN resolution code is built into Magento directly. Luckily, it’s pretty easy to associate URNs with real files in your local site.
This is what you will see after loading up the new code. Notice the URN is in red because PHP Storm does not know how to resolve it.
Put the cursor in the URN then click on the red light bulb to the left that will appear and select “Manually Setup External Resource” from the list.
Scroll down the list of XSD files that PHP Storm has automatically located, and select the appropriate file. The red URN will now be shown in green, and XML validation is back in effect.
You could go out on a hunt and destroy mission to find all such references in advance – personally I plan to just register URNs incrementally if I start editing a file with a red URN in it. If you are not editing a file, having the URN not be resolved is not really a problem in PHP Storm.
Oct 30, 2015: Stop the presses! I just did a git pull and noticed a new CLI command “dev:urn-catalog:generate”. (Okay, you can see Eugene pointed it out in comments below!) You should probably save your current .idea/misc.xml file, but I just ran the following command from my project root directory
bin/magento dev:urn-catalog:generate .idea/misc.xml
I then restarted PHP Storm, and all the URNs started resolving! Cool bananas! (It took a little while to run, but it found all the schema files and worked out URNs for them all – well all the ones I found so far.)