Skip to content

Commit

Permalink
Use Laravel satandard for blade directive
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony BOTALLA committed Oct 5, 2020
1 parent c65a849 commit 7c459bc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
14 changes: 13 additions & 1 deletion src/Helpers/MenuHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,23 @@ protected static function linkableRoute(string $routeName, string $translation):
}

/**
* Display menu from its slug
* Fallback to menu with current application locale
*
* You can append '|no-locale-fallback' to slug if you want to skip the default fallback
*
* @param $slug
* @return string
*/
public static function displayMenu(string $slug, $localeFallback = true): string
public static function displayMenu(string $slug): string
{
$args = explode('|', $slug, 2);
$localeFallback = true;
if (isset($args[1]) && $args[1] === 'no-locale-fallback') {
$localeFallback = false;
}
$slug = $args[0];

$locale = app()->getLocale();
$menu = Menu::query()
->where('slug', (string) $slug)
Expand Down
12 changes: 2 additions & 10 deletions src/LaravelNovaMenuServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Laravel\Nova\Events\ServingNova;
use Laravel\Nova\Nova;
use Novius\LaravelNovaMenu\Observers\ItemObserver;
use Novius\LaravelNovaMenu\Resources\MenuItem;
use Novius\LaravelNovaMenu\Resources\Menu;
use Novius\LaravelNovaMenu\Resources\MenuItem;

class LaravelNovaMenuServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -48,15 +48,7 @@ public function boot()
$this->publishes([__DIR__.'/../resources/lang' => resource_path('lang/vendor/laravel-nova-menu')], 'lang');

Blade::directive('menu', function ($expression) {
$expression = trim($expression, '\'"');
$args = explode('|', $expression, 2);
$localeFallback = 'true';
if (isset($args[1]) && $args[1] === 'no-locale-fallback') {
$localeFallback = 'false';
}
$expression = '"'.array_shift($args).'"'; // reformat the slug with quotes

return "<?php echo Novius\LaravelNovaMenu\Helpers\MenuHelper::displayMenu($expression, $localeFallback) ?>";
return "<?php echo Novius\LaravelNovaMenu\Helpers\MenuHelper::displayMenu($expression) ?>";
});

\Novius\LaravelNovaMenu\Models\MenuItem::observe(ItemObserver::class);
Expand Down

0 comments on commit 7c459bc

Please sign in to comment.