Coder Social home page Coder Social logo

naska-it / nova-menu-builder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from outl1ne/nova-menu-builder

0.0 1.0 0.0 1.56 MB

A menu builder for Laravel Nova, with support for picking custom models (ie Pages, Products).

License: MIT License

JavaScript 3.19% Vue 41.76% CSS 0.03% HTML 2.18% PHP 52.84%

nova-menu-builder's Introduction

Nova Menu Builder

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
  • 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 you can publish menu template:

php artisan vendor:publish --tag=nova-menu-builder-views

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
 * @return string
 **/
public static function getDisplayValue($value = 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)

To get a single menu, you can use the helper function nova_get_menu('slug'). Returns null if no menu with the slug is found or returns the menu if it is found.

Credits

License

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.