Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events for hooking #109

Closed
eboye opened this issue Apr 15, 2021 · 5 comments
Closed

Events for hooking #109

eboye opened this issue Apr 15, 2021 · 5 comments

Comments

@eboye
Copy link

eboye commented Apr 15, 2021

Are there any events that are emitted when menu item is saved? It would be great if there is one as I'm caching the menu like this:

$categoriesMenu = Cache::rememberForever( 'categoriesMenu', function () {
      return nova_get_menu_by_slug( 'categories' );
} );

I would like to clear that cache when the item saved like this:

Cache::forget('categories');

I'm not sure if there are some events and if the standard observers would work.

@Tarpsvo
Copy link
Collaborator

Tarpsvo commented May 14, 2021

You can register all kinds of observers yourself. Events would be an overkill, as they're already there from Laravel core. :)

https://laravel.com/docs/8.x/eloquent#observers

@Tarpsvo Tarpsvo closed this as completed May 14, 2021
@duckzland
Copy link

duckzland commented Jul 16, 2021

got this problem too.. hooking to menu works but not to menu items

tried with observer attached to MenuItem class but not firing

use OptimistDigital\MenuBuilder\Models\MenuItem as OptimistDigitalMenuItem;

OptimistDigitalMenuItem::observe(MenuItemObserver::class);

EDIT:
Events is firing correctly..

@kevariable
Copy link

kevariable commented Oct 8, 2021

Anyone have a problem with firing observer at Menu?

@Tarpsvo
Copy link
Collaborator

Tarpsvo commented Oct 29, 2021

Hi! I just tested the events and both Menu and MenuItem events seemed to work as intended. I added additional fixes for MenuItem updates in version 6.0.5. Are you sure you are not observing Menu while doing updates to MenuItems instead?

@eboye
Copy link
Author

eboye commented Oct 29, 2021

Yup, "found them". What I ended up doing, if anyone is interested is next:

App\Providers\AppServiceProvider

    ...
    public function boot() {
        ....
        Menu::observe( MenuObserver::class );
        MenuItem::observe( MenuObserver::class );
    }
    ...

App\Observers\MenuObserver

<?php

namespace App\Observers;

use Illuminate\Support\Facades\Cache;

class MenuObserver {
    /**
     * Handle the MenuItem "created" event.
     * @return void
     */
    public function created() {
        $this->forgetMenus();
    }

    /**
     * Forget menus method
     */
    public function forgetMenus() {
        Cache::forget( 'headerMenu' );
        Cache::forget( 'headerMobileMenu' );
        Cache::forget( 'categoriesMenu' );
        Cache::forget( 'firstFooterMenu' );
        Cache::forget( 'secondFooterMenu' );
    }

    /**
     * Handle the MenuItem "updated" event.
     * @return void
     */
    public function updated() {
        $this->forgetMenus();
    }

    /**
     * Handle the MenuItem "deleted" event.
     * @return void
     */
    public function deleted() {
        $this->forgetMenus();
    }

    /**
     * Handle the MenuItem "restored" event.
     * @return void
     */
    public function restored() {
        $this->forgetMenus();
    }

    /**
     * Handle the MenuItem "force deleted" event.
     * @return void
     */
    public function forceDeleted() {
        $this->forgetMenus();
    }

    /**
     * Handle the MenuItem "saving" event.
     * @return void
     */
    public function saving() {
        $this->forgetMenus();
    }
}

and manually caching menus when I call them:

        $headerMenu = Cache::rememberForever( 'headerMenu', function () {
            return nova_get_menu_by_slug( 'header' );
        } );
        $headerMobileMenu = Cache::rememberForever( 'headerMobileMenu', function () {
            return nova_get_menu_by_slug( 'header_mobile' );
        } );
        $categoriesMenu   = Cache::rememberForever( 'categoriesMenu', function () {
            return nova_get_menu_by_slug( 'categories' );
        } );
        $firstFooterMenu  = Cache::rememberForever( 'firstFooterMenu', function () {
            return nova_get_menu_by_slug( 'footer_first' );
        } );
        $secondFooterMenu = Cache::rememberForever( 'secondFooterMenu', function () {
            return nova_get_menu_by_slug( 'footer_second' );
        } );

So, I guess, yes, the events are firing. Not sure why they

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants