outl1ne / nova-menu-builder by allantatter

This Laravel Nova package allows you to create and manage menus and menu items.
332,006
242
10
Package Data
Maintainer Username: allantatter
Maintainer Contact: tarvoreinpalu@gmail.com (Tarvo Reinpalu)
Package Create Date: 2019-03-11
Package Last Update: 2024-11-07
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-09 15:07:57
Package Statistics
Total Downloads: 332,006
Monthly Downloads: 2,374
Daily Downloads: 36
Total Stars: 242
Total Watchers: 10
Total Forks: 86
Total Open Issues: 4

Nova Menu Builder

Latest Version on Packagist Total Downloads

This Laravel Nova package allows you to create and manage menus and menu items.

Continuation of Infinety/Nova-Menu-Builder. As opposed to the menu builder by InfinityEs, this package is mainly targeted towards the creation of headless content management systems and expects data to be returned through an API.

Features

  • Managing menus (w/ customizable locales) and menu items
  • Nesting and re-ordering of menu-items
  • Menu items collapse and duplicate options in the UI
  • Creation of custom link options (ie links to models such as Page or Product)
  • Support for different languages
  • Optional nova-lang support

Screenshots

Menu List

View Menu

Menu Item Edit

Installation

Install the package in a Laravel Nova project via Composer, run migrations and edit the config file:

# Install the package
composer require optimistdigital/nova-menu-builder

# Run automatically loaded migrations
php artisan migrate

# Publish the configuration file and edit it to your preference
php artisan vendor:publish --tag=nova-menu-builder-config

Register the tool with Nova in the tools() method of the NovaServiceProvider:

// in app/Providers/NovaServiceProvider.php

public function tools()
{
    return [
        // ...
        new \OptimistDigital\MenuBuilder\MenuBuilder,
    ];
}

Optionally publish views/migrations

This is only useful if you want to overwrite either the views or migrations. If you wish to use the menu builder as it comes out of the box, you don't need these.

# Publish views (optional)
php artisan vendor:publish --tag=nova-menu-builder-views

# Publish migrations to overwrite them (optional)
php artisan vendor:publish --tag=nova-menu-builder-migrations

Usage

Menu locale options

You can define the locales for the menus in the config file.

// in config/nova-menu.php

return [
  // ...
  'locales' => [
    'en_US' => 'English',
    'et_EE' => 'Estonian',
  ],

  // or using a closure:

  'locales' => function() {
    return nova_lang_get_locales();
  }

  // or if you want to use a function, but still be able to cache it:

  'locales' => '\App\Configuration\NovaMenuConfiguration@getLocales',

  // or

  'locales' => 'nova_lang_get_locales',
  // ...
];

Custom MenuLinkable classes

Nova menu builder allows you to create a select field for custom models (ie Pages or Products).

Create a class that extends the OptimistDigital\MenuBuilder\Classes\MenuLinkable class and register it in the config file.

// in config/nova-menu.php

return [
  // ...
  'linkable_models' => [
    \App\Classes\CustomMenuLinkable::class,
  ],
  // ...
];

In the created class, overwrite the following methods:

/**
 * Get the menu link identifier that can be used to tell different custom
 * links apart (ie 'page' or 'product').
 *
 * @return string
 **/
public static function getIdentifier(): string {
    // Example usecase
    // return 'page';
    return '';
}


/**
 * Get menu link name shown in  a dropdown in CMS when selecting link type
 * ie ('Product Link').
 *
 * @return string
 **/
public static function getName(): string {
    // Example usecase
    // return 'Page Link';
    return '';
}


/**
 * Get list of options shown in a select dropdown.
 *
 * Should be a map of [key => value, ...], where key is a unique identifier
 * and value is the displayed string.
 *
 * @return array
 **/
public static function getOptions($locale): array {
    // Example usecase
    // return Page::all()->pluck('name', 'id')->toArray();
    return [];
}

/**
 * Get the subtitle value shown in CMS menu items list.
 *
 * @param string $value
 * @param array $parameters The JSON parameters added to the item.
 * @return string
 **/
public static function getDisplayValue($value = null, array $parameters = null) {
    // Example usecase
    // return 'Page: ' . Page::find($value)->name;
    return $value;
}

/**
 * Get the value of the link visible to the front-end.
 *
 * Can be anything. It is up to you how you will handle parsing it.
 *
 * This will only be called when using the nova_get_menu()
 * and nova_get_menus() helpers or when you call formatForAPI()
 * on the Menu model.
 *
 * @param string $value The key from options list that was selected.
 * @param array $parameters The JSON parameters added to the item.
 * @return any
 **/
public static function getValue($value = null, array $parameters = null) {
    // Example usecase
    // return Page::find($value);
    return $value;
}

Custom menu resource

You can customize the resource controller through the config file.

To avoid controller double-loading nova-issues #1928 create it outside of App\Nova directory:

// Create app/Menus/MenuResource.php:

namespace App\Menus;

use OptimistDigital\MenuBuilder\Http\Resources\MenuResource as BaseMenuResource;

class MenuResource extends BaseMenuResource {
  //
}

// in config/nova-menu.php:

use App\Menus\MenuResource;

return [
    // ...
    'resource' => MenuResource::class,
    // ...
];

Returning the menus in a JSON API

nova_get_menus()

A helper function nova_get_menus is globally registered in this package which returns all the menus including their menu items in an API friendly format.

public function getMenus(Request $request) {
    $menusResponse = nova_get_menus();
    return response()->json($menusResponse);
}

nova_get_menu($menuSlug, $locale = null)

To get a single menu, you can use the helper function nova_get_menu('slug', 'en'). Returns null if no menu with the slug is found or returns the menu if it is found. If no locale is passed, the helper will automatically choose the first configured locale.

Credits

License

Nova Menu Builder is open-sourced software licensed under the MIT license.