SnowdogApps / magento2-menu

Provides powerful menu editor to replace category based menus in Magento 2
MIT License
298 stars 106 forks source link

Dx improvments #340

Open mokabiwd opened 2 months ago

mokabiwd commented 2 months ago

Hello,

first of all, thank you for this very complete module. We have started to integrate it in our magento 2 and we have 2 minor proposals that can greatly improve the developer experience.

For the context, we had to add 2 fields to the menu nodes. To do this, we had to override Node, which was done without a hitch, but we also had to copy/paste the following classes entirely:

but also the following template:

\Snowdog_MenuBlock\AdminhtmlEdit\Tab\Nodes

So, for this class, would it be possible to modify it slightly by doing something like :

class Nodes
{
    // prefix template with module name. Allow us to load the original template even if we defined a preference.
    protected $_template = 'Snowdog_Menu::menu/nodes.phtml';

    /**
     * @var Registry
     */
    protected $registry;

    /**
     * @var NodeRepositoryInterface
     */
    protected $nodeRepository;

    /**
     * @var ImageFile
     */
    protected $imageFile;
    /**
     * @var NodeTypeProvider
     */
    protected $nodeTypeProvider;

    /**
     * @var VueProvider
     */
    protected $vueProvider;

    /**
     * @var CustomerGroupsProvider
     */
    protected $customerGroupsProvider;

   // ...

    private function renderNodeList($level, $parent, $data)
    {
        if ($parent === null) {
            $parent = 0;
        }
        if (empty($data[$level])) {
            return;
        }
        if (empty($data[$level][$parent])) {
            return;
        }
        $nodes = $data[$level][$parent];
        $menu = [];
        foreach ($nodes as $node) {
            $menu[] = $this->renderNode($node, $parent, $level, $data);
        }
        return $menu;
    }

   protected function renderNode($node, $parent, $level, $data)
  {
            return [
                'is_active' => $node->getIsActive(),
                'is_stored' => true,
                'type' => $node->getType(),
                'content' => $node->getContent(),
                'classes' => $node->getClasses(),
                'target' => $node->getTarget(),
                'node_template' => $node->getNodeTemplate(),
                'submenu_template' => $node->getSubmenuTemplate(),
                'id' => $node->getId(),
                'title' => $node->getTitle(),
                'image' => $node->getImage(),
                'image_url' => $node->getImage() ? $this->imageFile->getUrl($node->getImage()) : null,
                'image_alt_text' => $node->getImageAltText(),
                'image_width' => $node->getImageWidth(),
                'image_height' => $node->getImageHeight(),
                'columns' => $this->renderNodeList($level + 1, $node->getId(), $data) ?: [],
                'selected_item_id' => $node->getSelectedItemId(),
                'customer_groups' => $node->getCustomerGroups()
            ];
  }
}

This would make it easy to override node serialization via a preference.

\Snowdog\Menu\ServiceMenu\SaveRequestProcessor

For this class, would it be possible to make the properties and processNodeObject method protected ? This would make it easy to override the entity's valuation via a preference.

andrzejborkowski commented 2 months ago

hi @mokabiwd,

we'd appreciate if you can create a PR and then our dev team will review it and if everything is clear we can include this improvement to the one of the next releases. Thanks in advance!

Regards, Andrzej