Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows modules with underscores in name to add blocks to layout via XML #11765

Merged
merged 1 commit into from
Oct 27, 2017
Merged

Allows modules with underscores in name to add blocks to layout via XML #11765

merged 1 commit into from
Oct 27, 2017

Conversation

bentideswell
Copy link
Contributor

Allow modules with underscores in the name to add blocks to the layout using XML.

Description

Magento 2 allows module names to include the underscore character (Vendor_Module_Name, which is app/code/Vendor/Module_Name) but doesn't then allow the module to add a block to the layout via XML.

This only occurs in developer mode and produces the following error:

1 exception(s):
Exception #0 (Magento\Framework\Config\Dom\ValidationException): Element 'block', attribute 'class': [facet 'pattern'] The value 'Vendor\Module_Name\Block\OpenGraph' is not accepted by the pattern '[A-Z][a-zA-Z\d]*(\\[A-Z][a-zA-Z\d]*)*'.
Line: 632

Element 'block', attribute 'class': 'Vendor\Module_Name\Block\OpenGraph' is not a valid value of the atomic type 'blockClassType'.
Line: 632


Exception #0 (Magento\Framework\Config\Dom\ValidationException): Element 'block', attribute 'class': [facet 'pattern'] The value 'Vendor\Module_Name\Block\OpenGraph' is not accepted by the pattern '[A-Z][a-zA-Z\d]*(\\[A-Z][a-zA-Z\d]*)*'.
Line: 632

Element 'block', attribute 'class': 'Vendor\Module_Name\Block\OpenGraph' is not a valid value of the atomic type 'blockClassType'.
Line: 632

The facet pattern is defined in lib/internal/Magento/Framework/View/Layout/etc/elements.xsd:

<xs:simpleType name="blockClassType">
    <xs:restriction base="xs:string">
        <xs:pattern value="[A-Z][a-zA-Z\d]*(\\[A-Z][a-zA-Z\d]*)*"/>
    </xs:restriction>
</xs:simpleType>

This pattern does not allow the underscore character to appear in the block class type attribute. I have fixed this by changing the pattern to:

<xs:pattern value="[A-Z][a-zA-Z\d]*(\\[A-Z][_a-zA-Z\d]*)*"/>

This allows an underscore to be used in the module name. This could be further to changed to allow an underscore to be used in the vendor name by changing it to:

<xs:pattern value="[A-Z][_a-zA-Z\d]*(\\[A-Z][_a-zA-Z\d]*)*"/>

Manual testing scenarios

  1. Enable developer mode using php bin/magento deploy:mode:set developer
  2. Create a module at app/code/Vendor/Some_Module
  3. Add a frontend layout XML file to the module and include a block from the module
  4. Flush the cache and view the frontend

@@ -138,7 +138,7 @@

<xs:simpleType name="blockClassType">
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][a-zA-Z\d]*(\\[A-Z][a-zA-Z\d]*)*"/>
<xs:pattern value="[A-Z][a-zA-Z\d]*(\\[A-Z][_a-zA-Z\d]*)*"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with other places, like https://github.com/magento/magento2/pull/11650/files, were underscore was allowed previously, please allow underscore in vendor name also, perform an amend commit and force push into the same branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made this change. Now both Vendor and Module allow underscores.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good now. Just squash all changes into single commit and do a force push to the same branch please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I have done this but let me know if I've made a mistake or you need anything else.

Amended to allow underscore in Vendor name as well as module name
@bentideswell
Copy link
Contributor Author

Just a quick update but I have found the same issue in the XSD file that handles the system.xml configuration options. I have created a new pull request that supplies the same fix as this (adding the underscore to the regular expression).

The new pull request can be seen at #14397

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants