Giter Site home page Giter Site logo

menuitemtypemaker's Introduction

Menu Item Type Maker

Register menu item type with object oriented PHP.

Menu Item Type Maker provides an object oriented API to register menu item type with the Menu Item Types plugin.

Installation

Require this package, with Composer, in the root directory of your project.

composer require amphibee/menu-item-type-maker

Download the Menu Item Types plugin and put it in either the plugins or mu-plugins directory. Visit the WordPress dashboard and activate the plugin.

Register a menu item type

The method way

Use the MenuItemType::make() function to register a new menu item type. Below you'll find an example of a menu item registration. Currently, this wrapper supports classic PHP, twig and blade views.

With the default ACF plugin and WordPress classic template

use AmphiBee\MenuItemTypeMaker\Facades\MenuItemType;

MenuItemType::make('Card', 'card')
            ->setIcon('path/to/my/icon.svg')
            ->setView('parts/nav/item-types/card.php')
            ->setFieldGroup('path/to/acf/field-group.php');

With Extended ACF and a Blade view

use AmphiBee\MenuItemTypeMaker\Facades\MenuItemType;
use Extended\ACF\Fields\Image;
use Extended\ACF\Fields\Text;

MenuItemType::make('Card', 'card')
            ->setIcon('<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
        <path stroke-linecap="round" stroke-linejoin="round" d="M2.25 15.75l5.159-5.159a2.25 2.25 0 013.182 0l5.159 5.159m-1.5-1.5l1.409-1.409a2.25 2.25 0 013.182 0l2.909 2.909m-18 3.75h16.5a1.5 1.5 0 001.5-1.5V6a1.5 1.5 0 00-1.5-1.5H3.75A1.5 1.5 0 002.25 6v12a1.5 1.5 0 001.5 1.5zm10.5-11.25h.008v.008h-.008V8.25zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z" />
    </svg>')
            ->setView('parts/nav/item-types/card')
            ->setFields([
                Image::make('Image'),
                Text::make('Title'),
            ]);

The extended way

use AmphiBee\MenuItemTypeMaker\Contracts\MenuItemAbstract;
use Extended\ACF\Fields\Image;
use Extended\ACF\Fields\Text;

class CardMenuProvider extends MenuItemAbstract
{
    protected $label = 'Card';
    protected $slug = 'card';
    protected $view = 'parts/nav/item-types/card.twig';
    protected $icon = '<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
        <path stroke-linecap="round" stroke-linejoin="round" d="M2.25 15.75l5.159-5.159a2.25 2.25 0 013.182 0l5.159 5.159m-1.5-1.5l1.409-1.409a2.25 2.25 0 013.182 0l2.909 2.909m-18 3.75h16.5a1.5 1.5 0 001.5-1.5V6a1.5 1.5 0 00-1.5-1.5H3.75A1.5 1.5 0 002.25 6v12a1.5 1.5 0 001.5 1.5zm10.5-11.25h.008v.008h-.008V8.25zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z" />
    </svg>';

    protected function fields(): array
    {
        return [
            Image::make('Image'),
            Text::make('Title'),
        ];
    }
}

menuitemtypemaker's People

Contributors

ogorzalka avatar renovate[bot] avatar lhall-amphibee avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

menuitemtypemaker's Issues

Give more data to the renderer

``In MenuItemAbstract.php, the `viewArgs()` method could receive more arguments to offer more flexibility.

Exemple :

            'render_callback' => function ($item, string $custom_item_type, object $args, int $depth) {
                $viewArgs = array_merge([
                    'item' => $item,
                    'depth' => $depth,
                    'args' => $args,
                    'custom_item_type' => $custom_item_type
                ], $this->viewArgs($item, $custom_item_type, $args, $depth));
                return $this->render($this->getView(), $viewArgs);
            },

Make Extended ACF version easier to use

The Extended ACF version doesn't work right away because the fields() method is never called.

Workaround is to use this :

    protected function getFields(): array
    {
        return array_merge($this->fields, [
            Text::make(__('ACF Field'))
        ]);
    }

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.