Skip to content

Commit

Permalink
Menu Item data attributes (#564)
Browse files Browse the repository at this point in the history
* add DataFilter & update menuitem partials

* add DataFilter in default config

* update README.md for menu item data

* fix CI

* add test case

* fix cs

* Update DataFilter.php

* Update DataFilter.php

Co-authored-by: Florian Ressel <florian@genxtreme.de>
  • Loading branch information
REJack and resslinger authored Apr 27, 2020
1 parent 180633b commit 75a48e4
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 15 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,29 @@ This will ignored if the top navigation layout is enabled, all menu items will a

To get a dynamic item placing you can add the `key` attribute, with this you can add a unique identifier to a add before or after it new items.

To add data-attributes to your menu link your can simply add a associative array called `data`. Here a simple example:

```php
[
[
'header' => 'BLOG',
'url' => 'admin/blog',
'data' => [
'test' => 'content',
],
],
[
'text' => 'Add new post',
'url' => 'admin/blog/new',
'data' => [
'test-one' => 'content-one',
'test-two' => 'content-two',
],
],
]
```


Use the `can` attribute if you want conditionally show the menu item. This integrates with Laravel's `Gate` functionality. If you need to conditionally show headers as well, you need to wrap it in an array like other menu items, using the `header` attribute. You can add more `can` entries as array, see the second example:

```php
Expand Down
1 change: 1 addition & 0 deletions config/adminlte.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@
JeroenNoten\LaravelAdminLte\Menu\Filters\ClassesFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\GateFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\LangFilter::class,
JeroenNoten\LaravelAdminLte\Menu\Filters\DataFilter::class,
],

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<a class="dropdown-item @if (isset($item['submenu']))dropdown-toggle @endif" href="{{ $item['href'] }}"
@if (isset($item['submenu'])) data-toggle="dropdown" @endif
@if (isset($item['target'])) target="{{ $item['target'] }}" @endif
{!! $item['data-compiled'] ?? '' !!}
>
<i class="{{ $item['icon'] ?? 'far fa-fw fa-circle' }} {{ isset($item['icon_color']) ? 'text-' . $item['icon_color'] : '' }}"></i>
{{ $item['text'] }}
Expand Down
31 changes: 16 additions & 15 deletions resources/views/partials/menuitems/menu-item-top-nav.blade.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
@if (isset($item['search']) && $item['search'])
<form action="{{ $item['href'] }}" method="{{ $item['method'] }}" class="form-inline ml-2 mr-2">
<div class="input-group">
<input class="form-control form-control-navbar" type="search" name="{{ $item['input_name'] }}" placeholder="{{ $item['text'] }}" aria-label="{{ $item['aria-label'] ?? $item['text'] }}">
<div class="input-group-append">
<button class="btn btn-navbar" type="submit">
<i class="fas fa-search"></i>
</button>
</div>
</div>
</form>
<form action="{{ $item['href'] }}" method="{{ $item['method'] }}" class="form-inline ml-2 mr-2">
<div class="input-group">
<input class="form-control form-control-navbar" type="search" name="{{ $item['input_name'] }}" placeholder="{{ $item['text'] }}" aria-label="{{ $item['aria-label'] ?? $item['text'] }}">
<div class="input-group-append">
<button class="btn btn-navbar" type="submit">
<i class="fas fa-search"></i>
</button>
</div>
</div>
</form>
@elseif (is_array($item) && !isset($item['header']))
<li @if (isset($item['id'])) id="{{ $item['id'] }}" @endif class="nav-item {{ $item['top_nav_class'] }}">
<a class="nav-link @if (isset($item['submenu']))dropdown-item dropdown-toggle @endif" href="{{ $item['href'] }}"
@if (isset($item['submenu'])) data-toggle="dropdown" @endif
@if (isset($item['target'])) target="{{ $item['target'] }}" @endif
>
<i class="{{ $item['icon'] ?? 'far fa-fw fa-circle' }} {{ isset($item['icon_color']) ? 'text-' . $item['icon_color'] : '' }}"></i>
<a class="nav-link @if (isset($item['submenu']))dropdown-item dropdown-toggle @endif" href="{{ $item['href'] }}"
@if (isset($item['submenu'])) data-toggle="dropdown" @endif
@if (isset($item['target'])) target="{{ $item['target'] }}" @endif
{!! $item['data-compiled'] ?? '' !!}
>
<i class="{{ $item['icon'] ?? 'far fa-fw fa-circle' }} {{ isset($item['icon_color']) ? 'text-' . $item['icon_color'] : '' }}"></i>
{{ $item['text'] }}

@if (isset($item['label']))
Expand Down
1 change: 1 addition & 0 deletions resources/views/partials/menuitems/menu-item.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<li @if (isset($item['id'])) id="{{ $item['id'] }}" @endif class="nav-item @if (isset($item['submenu'])){{ $item['submenu_class'] }}@endif">
<a class="nav-link {{ $item['class'] }} @if(isset($item['shift'])) {{ $item['shift'] }} @endif" href="{{ $item['href'] }}"
@if (isset($item['target'])) target="{{ $item['target'] }}" @endif
{!! $item['data-compiled'] ?? '' !!}
>
<i class="{{ $item['icon'] ?? 'far fa-fw fa-circle' }} {{ isset($item['icon_color']) ? 'text-' . $item['icon_color'] : '' }}"></i>
<p>
Expand Down
28 changes: 28 additions & 0 deletions src/Menu/Filters/DataFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace JeroenNoten\LaravelAdminLte\Menu\Filters;

use JeroenNoten\LaravelAdminLte\Menu\Builder;

class DataFilter implements FilterInterface
{
public function transform($item, Builder $builder)
{
if (isset($item['data']) && is_array($item['data'])) {
$item['data-compiled'] = $this->compileData($item['data']);
}

return $item;
}

protected function compileData($dataArray)
{
$compiled = [];

foreach ($dataArray as $key => $value) {
$compiled[] = 'data-'.$key.'="'.$value.'"';
}

return implode(' ', $compiled);
}
}
15 changes: 15 additions & 0 deletions tests/Menu/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,4 +623,19 @@ public function testLangTranslate()
$this->assertEquals('Blog', $builder->menu[2]['text']);
$this->assertEquals('TEST', $builder->menu[3]['header']);
}

public function testDataAttributes()
{
$builder = $this->makeMenuBuilder();

$builder->add(['text' => 'About', 'data' => [
'test-one' => 'content-one',
'test-two' => 'content-two',
]]);

$this->assertEquals(
'data-test-one="content-one" data-test-two="content-two"',
$builder->menu[0]['data-compiled']
);
}
}
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use JeroenNoten\LaravelAdminLte\Menu\Builder;
use JeroenNoten\LaravelAdminLte\Menu\Filters\ActiveFilter;
use JeroenNoten\LaravelAdminLte\Menu\Filters\ClassesFilter;
use JeroenNoten\LaravelAdminLte\Menu\Filters\DataFilter;
use JeroenNoten\LaravelAdminLte\Menu\Filters\GateFilter;
use JeroenNoten\LaravelAdminLte\Menu\Filters\HrefFilter;
use JeroenNoten\LaravelAdminLte\Menu\Filters\LangFilter;
Expand All @@ -32,6 +33,7 @@ protected function makeMenuBuilder($uri = 'http://example.com', GateContract $ga
new ActiveFilter($this->makeActiveChecker($uri)),
new SubmenuFilter($this->makeActiveChecker($uri)),
new ClassesFilter(),
new DataFilter(),
new GateFilter($gate ?: $this->makeGate()),
new LangFilter($this->makeTranslator($locale)),
]);
Expand Down

0 comments on commit 75a48e4

Please sign in to comment.