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.)
I created an experimental script to make adding the urn to file mapping a bit faster for phpstorm.
Still needs a bit quality work, but may help some people already.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
generate_magento_urn_to_xsd_mapping.php
hosted with ❤ by GitHub
Thought it makes sense to link it here 🙂
seems Iam not allowed to post an url here <.<
gist.github.com/Flyingmana
/c83a09288b666225b5d0
Great idea! We pushed it forward and generated a URN catalog for PhpStorm. Can you please take a look? http://magento.stackexchange.com/questions/88242/magento-2-xml-validation/88413#88413
Replied to your comment below.
Great idea! We moved it forward and generate complete URN catalog in the PhpStorm format. Can you please take a look? http://magento.stackexchange.com/questions/88242/magento-2-xml-validation/88413#88413
Also would be helpful to add generation for other IDEs.
Just install https://plugins.jetbrains.com/plugin/8024 go to plugin settings and click “Regenerate urn map” button.
For feature requests/additional information please refer to https://github.com/dkvashninbay/magento2plugin
Another tip for phpstorm full support autocomplete in Magento 2:
http://magento2developer.com/blog/create-class-autocomplete-with-get-in-object-manager-magento2-for-phpstorm.html