Skip to content

Commit b5386fb

Browse files
committed
feat(menu): add icon support
1 parent cd99434 commit b5386fb

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"key": "group_5f280ab299565",
3+
"title": "Menu",
4+
"fields": [
5+
{
6+
"key": "field_5f280ac5af076",
7+
"label": "Icon",
8+
"name": "icon",
9+
"aria-label": "",
10+
"type": "image",
11+
"instructions": "",
12+
"required": 0,
13+
"conditional_logic": 0,
14+
"wrapper": {
15+
"width": "",
16+
"class": "",
17+
"id": ""
18+
},
19+
"return_format": "id",
20+
"library": "all",
21+
"min_width": "",
22+
"min_height": "",
23+
"min_size": "",
24+
"max_width": "",
25+
"max_height": "",
26+
"max_size": "",
27+
"mime_types": "svg",
28+
"allow_in_bindings": 0,
29+
"preview_size": "medium"
30+
}
31+
],
32+
"location": [
33+
[
34+
{
35+
"param": "nav_menu_item",
36+
"operator": "==",
37+
"value": "all"
38+
}
39+
]
40+
],
41+
"menu_order": 0,
42+
"position": "normal",
43+
"style": "default",
44+
"label_placement": "left",
45+
"instruction_placement": "label",
46+
"hide_on_screen": "",
47+
"active": true,
48+
"description": "",
49+
"show_in_rest": 0,
50+
"inner_blocks": 0,
51+
"modified": 1750950886
52+
}

config/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
BlockSpacing\BlockSpacingHooks::class,
5454

5555
// Menu
56+
Menu\MenuHooks::class,
5657
Menu\MenuRegistrar::class,
5758

5859
// Post Type
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoWStarterTheme\Common\Menu;
6+
7+
use KepnerTregoe\Common\Contracts\Hookable;
8+
use KepnerTregoe\Common\Helpers\SVG;
9+
use KepnerTregoe\Deps\Illuminate\Support\Collection;
10+
use WP_Post;
11+
12+
/**
13+
* Menu class
14+
*/
15+
class MenuHooks implements Hookable
16+
{
17+
/**
18+
* Class constructor.
19+
*
20+
* @param SVG $svg SVG helper instance.
21+
*/
22+
public function __construct(
23+
private SVG $svg,
24+
) {
25+
}
26+
27+
/**
28+
* Adds icons to menu items.
29+
*
30+
* @filter wp_nav_menu_objects
31+
*
32+
* @param WP_Post[] $items Menu items.
33+
* @param array<string, mixed> $args object of wp_nav_menu() arguments.
34+
* @return WP_Post[] customized menu items with icons.
35+
*/
36+
public function addIcons(array $items) {
37+
return Collection::make($items)
38+
->map(function (WP_Post $item) {
39+
$icon = get_field('icon', $item);
40+
41+
if (!is_numeric($icon)) {
42+
return $item;
43+
}
44+
45+
$item->title = sprintf(
46+
'%s%s',
47+
$this->svg->getAttachment($icon),
48+
$item->title
49+
);
50+
51+
return $item;
52+
})
53+
->all();
54+
}
55+
}

0 commit comments

Comments
 (0)