Last active
February 3, 2022 17:59
-
-
Save joshuafredrickson/54e8ea2fac070bac2b378fea353ee61f to your computer and use it in GitHub Desktop.
Megamenu in Blade using Alpine JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\View\Composers; | |
use Roots\Acorn\View\Composer; | |
use Log1x\Navi\Facades\Navi; | |
use function App\get_parent; | |
use function acf_get_attachment; | |
class NavHeader extends Composer | |
{ | |
use \App\Concerns\NavOverride; | |
/** | |
* List of views served by this composer. | |
* | |
* @var array | |
*/ | |
protected static $views = [ | |
'partials.header', | |
'partials.nav-mobile', | |
]; | |
/** | |
* Data to be passed to view before rendering. | |
* | |
* @return array | |
*/ | |
public function with() | |
{ | |
$this->setNav(); | |
return [ | |
'navHeader' => $this->navHeader(), | |
'navOverride' => $this->navOverride, | |
'navLogo' => $this->navLogo(), | |
]; | |
} | |
/** | |
* Returns a log1x/navi object for the primary menu | |
* | |
* @return object | |
*/ | |
public function navHeader() | |
{ | |
// Is it empty? | |
if (Navi::build($this->navMenu)->isEmpty()) { | |
return false; | |
} | |
// Build it. | |
$nav = Navi::build($this->navMenu)->toArray(); | |
collect($nav)->map(function ($navItem) { | |
$navItem->mega_menu = get_field('mega_menu', $navItem->id); | |
if (! $navItem->children) { | |
return $navItem; | |
} | |
$navItem->defaultChild = current($navItem->children)->id; | |
/** | |
* Get children ACF | |
*/ | |
collect($navItem->children)->map(function ($child) { | |
$child->mega_menu_type = get_field('mega_menu_type', $child->id); | |
switch ($child->mega_menu_type) { | |
case 'description': | |
$child->mega_menu_description = get_field('mega_menu_description', $child->id); | |
if (! $child->mega_menu_description['image']) { | |
$child->mega_menu_description['image'] = null; | |
} | |
break; | |
case 'links': | |
$child->mega_menu_links = get_field('mega_menu_links', $child->id); | |
break; | |
case 'link': | |
default: | |
break; | |
} | |
return $child; | |
}); | |
return $navItem; | |
}); | |
return $nav; | |
} | |
/** | |
* Sets the nav logo | |
* | |
* @return array | |
*/ | |
public function navLogo() | |
{ | |
if (! $this->navOverride) { | |
return []; | |
} | |
global $post; | |
// Check for parent menu override first | |
// Using get_post_meta here due to issues with get_field and previewing posts | |
$logoSvg = get_post_meta(get_parent(), 'icon', true) ?: | |
get_post_meta($post->ID, 'icon', true) ?: | |
false; | |
$logoPermalink = get_the_permalink(get_parent()) ?: | |
get_the_permalink($post->ID) ?: | |
home_url('/'); | |
$logoTitle = get_the_title(get_parent()) ?: | |
get_the_title() ?: | |
''; | |
return [ | |
'logo' => acf_get_attachment($logoSvg), | |
'title' => $logoTitle, | |
'permalink' => $logoPermalink, | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment