Menus

In Compass Core menu items can be used to create a menu structure for your application.

Creating a menu item

To add menu items...

Menu Item Badges

Menu Items Badges can be used to dynamically easily add a badge to a menu item. The badge will be updated automatically when the resource collection is updated.

Creating a service

To add a badge to a menu item, you will first need to add a service class that implements the Noardcode\Compass\Menu\Contracts\MenuItemWithBadgeInterface interface.

class TaskItemService implements MenuItemWithBadgeInterface

Implementing the interface

The interface has two methods that need to be implemented. The getBadgeCounter method is used to get the count of any number relating to the menu item in question. The getBadgeColor method is used to set the color of the badge. The color can be any of the predefined bootstrap colors or a hex color code.

    public function __construct(MenuItem $menuItem)
    {
        //
    }

    public function getBadgeContent(): ?string
    {
        return 0;
    }

    public function getBadgeColor(): ?string
    {
        return '#000000'; // Or: red, blue, green, yellow, orange, purple, pink, teal, cyan, white, gray, gray-dark, primary, secondary, success, info, warning, danger, light, dark
    }

If the counter is 0, the badge will not be shown. If a color is not set, the default color will be used.

Adding the service to the menu item

The service can be added to the menu item by adding the fully qualified class name to the column service_class in the MenuItem database table.

Troubleshooting

For a Menu Item Badge to work, the following rules apply:

  • The MenuItem Service Class must implement the Noardcode\Compass\Menu\Contracts\MenuItemWithBadgeInterface interface.
  • The MenuItem Service Class must be added to the service_class column in the MenuItem database table.
  • The badge needs to be more than 0 to be shown.
  • The color needs to be a valid bootstrap color or a hex color code.