Skip to content

Document registering a custom Menu in Config/services.php (Mautic 8) - #627

Open
promptless-for-oss wants to merge 1 commit into
mautic:7.2from
Promptless:promptless/pr-17116-menus-services-php
Open

Document registering a custom Menu in Config/services.php (Mautic 8)#627
promptless-for-oss wants to merge 1 commit into
mautic:7.2from
Promptless:promptless/pr-17116-menus-services-php

Conversation

@promptless-for-oss

Copy link
Copy Markdown
Contributor

Open in Promptless

Mautic 8 (mautic/mautic PR #17116) removed the ServicePass compiler pass and the services > menus array in a bundle's Config/config.php. A Plugin that registered its own top-level Menu must now declare two services explicitly in Config/services.php: a Knp\Menu\MenuItem tagged knp_menu.menu, and a Mautic\CoreBundle\Menu\MenuRenderer tagged knp_menu.renderer, paired by a matching alias tag argument.

This adds a "Registering a custom Menu" section to docs/plugins/config.rst documenting this previously-undocumented extension point and its migration path — a before (config.php) / after (services.php) example, the multi-menu loop form, and a note that the menu builder must be referenced by class (Mautic\CoreBundle\Menu\MenuBuilder::class) because the mautic.menu.builder string alias was removed. A forward cross-reference from the "Available menus" list points readers to the new section.

Trigger Events

Mautic 8 (mautic/mautic PR #17116) removed the ServicePass compiler pass
and the 'services > menus' array in bundle Config/config.php. Add a
'Registering a custom Menu' section to plugins/config.rst documenting the
knp_menu.menu MenuItem and knp_menu.renderer MenuRenderer services a Plugin
now declares in Config/services.php, with a before/after migration example
and a forward cross-reference from 'Available menus'.
Comment thread docs/plugins/config.rst

The :ref:`plugins/config:Menu config items` section adds items to Mautic's four built-in menus through the ``menu`` config array. This section covers the opposite direction: registering a Plugin's own top-level Menu, with its own template and renderer.

.. note::

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Confirms Mautic 8 (mautic/mautic PR #17116, merged into 8.x at commit 8da44fa2) deleted the ServicePass compiler pass and removed its registration from MauticCoreBundle::build(); the services > menus array in Config/config.php is no longer processed.

Source: https://github.com/mautic/mautic/blob/8da44fa2ce8d9d4756b8cd620225bba51b07ea8f/app/bundles/CoreBundle/MauticCoreBundle.php#L12-L23

Comment thread docs/plugins/config.rst

Mautic 8 removed the ``ServicePass`` compiler pass and the ``services > menus`` array in ``Config/config.php``. A Plugin now declares its Menu item and renderer explicitly in ``Config/services.php``.

Registering a custom Menu takes two services in your Plugin's ``Config/services.php``:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

CoreBundle's own Config/services.php registers each menu as a Knp\Menu\MenuItem tagged knp_menu.menu and a Mautic\CoreBundle\Menu\MenuRenderer tagged knp_menu.renderer, each with ['alias' => ] — the pattern documented for plugin bundles.

Source: https://github.com/mautic/mautic/blob/8da44fa2ce8d9d4756b8cd620225bba51b07ea8f/app/bundles/CoreBundle/Config/services.php#L239-L247

Comment thread docs/plugins/config.rst
use Mautic\CoreBundle\Menu\MenuBuilder;
use Mautic\CoreBundle\Menu\MenuRenderer;

use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

MenuBuilder::__call() strips the trailing "Menu" from the invoked method name and builds the menu by that alias, confirming the documented 'Menu' factory-method pattern (e.g. mybundleMenu) works for any custom alias, not just CoreBundle's built-in menus.

Source: https://github.com/mautic/mautic/blob/8da44fa2ce8d9d4756b8cd620225bba51b07ea8f/app/bundles/CoreBundle/Menu/MenuBuilder.php#L27-L32

Comment thread docs/plugins/config.rst

// ... inside the configurator closure, using the same $services

$services->set('mautic.menu.mybundle', MenuItem::class)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

MenuRenderer::__construct(MatcherInterface $matcher, Environment $twig, array $defaultOptions = []) matches the documented ->args([service('knp_menu.matcher'), service('twig'), $options]) pattern.

Source: https://github.com/mautic/mautic/blob/8da44fa2ce8d9d4756b8cd620225bba51b07ea8f/app/bundles/CoreBundle/Menu/MenuRenderer.php#L14-L18

Comment thread docs/plugins/config.rst
$services->set('mautic.menu.mybundle', MenuItem::class)
->factory([service(MenuBuilder::class), 'mybundleMenu'])
->tag('knp_menu.menu', ['alias' => 'mybundle']);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Merge commit for mautic/mautic PR #17116 (merged into 8.x) removes the line $services->alias('mautic.menu.builder', Mautic\CoreBundle\Menu\MenuBuilder::class); from CoreBundle/Config/services.php; the alias is absent from the merged 8.x file, confirming service(MenuBuilder::class) must be referenced by class.

Source: mautic/mautic@8da44fa

@promptless-for-oss

Copy link
Copy Markdown
Contributor Author

I noticed that some CI checks failed for this PR. I'm investigating whether the failures are caused by this suggestion. If they're unrelated or pre-existing, I'll leave this suggestion unchanged and create a separate suggestion if a standalone docs fix is needed.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant