Skip to content

Commit

Permalink
Merge pull request #182 from epfl-idevelop/wp-more-menu
Browse files Browse the repository at this point in the history
"Phantom" menu entry (for labs at the root)
  • Loading branch information
jdelasoie authored Feb 26, 2019
2 parents af40a9c + 37b01e6 commit 8bac7a4
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions header.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@


/**
* A subclass of @link Walker_Nav_Menu to customize the root menu
* Custom nav menu walker for the top bar
*
* + Remove so-called "phantom" top-level navigation entries such as "Labs",
* that only exist as placeholders
*
* + Substitute locally-defined nav menu items with the same set_title.
* This lets e.g. school-level sites redirect their "Education", "Research" etc.
* pages
*/
class EPFL_Theme2018_Root_Menu_Walker extends Walker_Nav_Menu {
function start_el (&$output, $item, $depth = 0, $args = array(), $id = 0) {
Expand All @@ -38,6 +43,44 @@ private function _get_surrogate_menu () {
}
return $this->_surrogate_menu;
}

/**
* Overridden to discard "phantom" root menu entries; that is,
* entries whose only child is an ExternalMenuItem (as determined
* by the EPFL plug-in setting a ->epfl_external_menu_children_count
* annotation for us).
*/
public function walk( $elements, $max_depth ) {
$self = $this;
$filtered_elements = array_filter($elements,
function($element) use ($self, $elements) {
return ! $this->_is_phantom_node($elements, $element);
});
return parent::walk($filtered_elements, $max_depth);
}

function _is_phantom_node ($elements, $element) {
$self = $this;
if (property_exists($element, 'epfl_external_menu_children_count') &&
$element->epfl_external_menu_children_count == 1) {
$other_children_count = count(array_filter($elements,
function($child) use ($self, $element) {
return ($child->menu_item_parent == $element->ID &&
$child->epfl_soa !== $self->_get_our_soa());
}));
if ($other_children_count === 0) return true;
}
}

function _get_our_soa () {
if (! $this->_our_soa) {
$this->_our_soa = site_url();
if (! preg_match('#/$#', $this->_our_soa)) {
$this->_our_soa .= '/';
}
}
return $this->_our_soa;
}
}

?>
Expand Down Expand Up @@ -66,7 +109,7 @@ private function _get_surrogate_menu () {
<a class="sr-only" href="#content"><?php esc_html_e( 'Skip to content', 'epfl' ); ?></a>

<header role="banner" class="header">

<div class="site-branding">
<a class="logo" href="<?php echo get_epfl_home_url(); ?>">
<img src="<?php bloginfo('template_url'); ?>/assets/svg/epfl-logo.svg" alt="Logo EPFL, École polytechnique fédérale de Lausanne" class="img-fluid">
Expand Down

0 comments on commit 8bac7a4

Please sign in to comment.