Userfunction menu¶
Attention
It is still possible to create a menu with special = userfunction
for backward compatibility reasons.
However we would advise you to write a customized MenuProcessor and style the output with Fluid instead.
Calls a user function/method in class which should return an array with page records for the menu.
Properties¶
special.userFunc¶
Property
special.userFunc
Data type
string
Description
Name of the function
[tsref:(cObject).HMENU.special = userfunction]
Example¶
Example: Creating hierarchical menus of custom links¶
By default the HMENU object is designed to create menus from pages in TYPO3. Such pages are represented by their page-record contents. Usually the "title" field is used for the title and the "uid" field is used to create a link to that page in the menu.
However the HMENU and sub-menu objects are so powerful that it would be very useful to use these objects for creating menus of links which do not relate to pages in TYPO3 by their ids. This could be a menu reflecting a menu structure of a plugin where each link might link to the same page id in TYPO3 but where the difference would be in some parameter value.
First, this listing creates a menu in three levels where the first two are graphical items:
1# ************************
2# CUSTOM MENU
3# ************************
4lib.custommenu = HMENU
5lib.custommenu {
6 special = userfunction
7 special.userFunc = Vendor\MyExtension\Userfuncs\CustomMenu->makeMenuArray
8
9 1 = TMENU
10 1.wrap = <ul class="level-1">|</ul>
11 1.NO = 1
12 1.NO {
13 wrapItemAndSub = <li>|</li>
14 }
15
16 2 = TMENU
17 2.wrap = <ul class="level-2">|</ul>
18 2.NO = 1
19 2.NO {
20 wrapItemAndSub = <li>|</li>
21 }
22
23 3 = TMENU
24 3.wrap = <ul class="level-3">|</ul>
25 3.NO = 1
26 3.NO {
27 wrapItemAndSub = <li>|</li>
28 }
29}
The menu looks like this on a web page:

The TypoScript code above generates this menu, but the items do not link straight to pages as usual. This is because the whole menu is generated from this array, which was returned from the function "makeMenuArray" called in TypoScript line 5+6 :
1<?php
2
3declare(strict_types=1);
4
5namespace Vendor\MyExtension\Userfuncs;
6
7class CustomMenu
8{
9
10 public function makeMenuArray(array $content, array $conf) : array
11 {
12 return [
13 [
14 'title' => 'Contact',
15 '_OVERRIDE_HREF' => 'index.php?id=10',
16 '_SUB_MENU' => [
17 [
18 'title' => 'Offices',
19 '_OVERRIDE_HREF' => 'index.php?id=11',
20 '_OVERRIDE_TARGET' => '_top',
21 'ITEM_STATE' => 'ACT',
22 '_SUB_MENU' => [
23 [
24 'title' => 'Copenhagen Office',
25 '_OVERRIDE_HREF' => 'index.php?id=11&officeId=cph',
26 ],
27 [
28 'title' => 'Paris Office',
29 '_OVERRIDE_HREF' => 'index.php?id=11&officeId=paris',
30 ],
31 [
32 'title' => 'New York Office',
33 '_OVERRIDE_HREF' => 'https://example.org',
34 '_OVERRIDE_TARGET' => '_blank',
35 ]
36 ]
37 ],
38 [
39 'title' => 'Form',
40 '_OVERRIDE_HREF' => 'index.php?id=10&cmd=showform',
41 ],
42 [
43 'title' => 'Thank you',
44 '_OVERRIDE_HREF' => 'index.php?id=10&cmd=thankyou',
45 ],
46 ],
47 ],
48 [
49 'title' => 'Products',
50 '_OVERRIDE_HREF' => 'index.php?id=14',
51 ]
52 ];
53 }
54
55}
Notice how the array contains "fake" page-records which has no uid field, only a "title" and "_OVERRIDE_HREF" as required and some other fields as it fits.
The first level with items "Contact" and "Products" contains "title" and "_OVERRIDE_HREF" fields, but "Contact" extends this by a "_SUB_MENU" array which contains a similar array of items.
The first item on the second level, "Offices", contains a field called "_OVERRIDE_TARGET". Further the item has its state set to "ACT" which means it will render as an "active" item (you will have to calculate such stuff manually when you are not rendering a menu of real pages!). Finally there is even another sub-level of menu items.