hyva-themes / magento2-hyva-admin

This module aims to make creating grids and forms in the Magento 2 adminhtml area joyful and fast.
https://hyva-themes.github.io/magento2-hyva-admin/
BSD 3-Clause "New" or "Revised" License
168 stars 39 forks source link

If grid_name contains hyphen '-', some events won't work #31

Closed paugnu closed 3 years ago

paugnu commented 3 years ago

Hi,

I created a new grid with the name product-grid. Afterwards, I wanted to create an event to modify the columns.

As per explained here: https://github.com/hyva-themes/magento2-hyva-admin/blob/main/doc/2.%20API%20Reference/Event%20Reference/Grid%20Column%20Definition%20Build%20After%20Event.md

My event name would be: hyva_grid_column_definition_build_after_product-grid

But if I try to use hyva_grid_column_definition_build_after_product-grid as a event name:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="hyva_grid_column_definition_build_after_product-grid">
        <observer name="hide_user_defined_columns" instance="Catgento\AdminGrids\Observer\HideColumnsObserver" />
    </event>
</config>

This will throw an error: Element 'event', attribute 'name': [facet 'pattern'] The value 'hyva_grid_column_definition_build_after_product-grid' is not accepted by the pattern '[a-zA-Z0-9_]+'. Element 'event', attribute 'name': 'hyva_grid_column_definition_build_after_product-grid' is not a valid value of the atomic type 'eventName'.

Some ideas...

Vinai commented 3 years ago

The method that changes the grid name into an event name suffix takes care of that:

private function getGridNameEventSuffix(string $gridName): string
{
    return strtolower(preg_replace('/[^[:alpha:]]+/', '_', $gridName));
}

That will replace - with _ In your case the PHP event observer should be for the event hyva_grid_column_definition_build_after_product_grid

Vinai commented 3 years ago

PS: Thanks for opening the issue, I've updated the docs with this information.