Skip to content

Commit

Permalink
Fixed parent menu item showing dropdown icon and empty dropdown if al…
Browse files Browse the repository at this point in the history
…l children disabled (#2968, #2997)
  • Loading branch information
mahagr committed Feb 15, 2022
1 parent 70dd3f4 commit 1c478ea
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Added Joomla 4.1 support
2. [](#bugfix)
- Fixed platform check to be PHP >= 5.6.20 (#2998)
- Fixed parent menu item showing dropdown icon and empty dropdown if all children disabled (#2968, #2997)
1. [WordPress](#wordpress)
1. [](#bugfix)
- Fixed platform check to be PHP >= 5.6.20 (#2998)
Expand Down
10 changes: 5 additions & 5 deletions engines/common/nucleus/particles/menu.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
{% set label = item.icon_only and (item.image or item.icon) ? ' aria-label="' ~ item.title|e ~'"' %}
{% set active = menu.isActive(item) ? ' active' %}
{% set dropdown = item.level == start_level ? ' g-' ~ item.getDropdown() %}
{% set parent = item.children|length ? ' g-parent' %}
{% set parent = item.hasChildren() and not item.dropdown_hide ? ' g-parent' %}
{% set target = (item.target != '_self' or context.particle.forceTarget) ? ' target="' ~ item.target|e ~ '"' %}
{% set rel = item.rel %}

Expand Down Expand Up @@ -92,7 +92,7 @@

{% set rel = rel ? ' rel="' ~ rel|e('html_attr') ~ '"' %}

<li class="g-menu-item g-menu-item-type-{{ item.type }} g-menu-item-{{ item.id }}{% if not item.dropdown_hide %}{{ parent }}{% endif %}{{ active }}{{ dropdown }} {% if item.url and item.children|length %}{% if not item.dropdown_hide %}g-menu-item-link-parent{% endif %}{% endif %} {{ item.class|default('') }}"
<li class="g-menu-item g-menu-item-type-{{ item.type }} g-menu-item-{{ item.id }}{% if not item.dropdown_hide %}{{ parent }}{% endif %}{{ active }}{{ dropdown }} {% if item.url and parent %}{% if not item.dropdown_hide %}g-menu-item-link-parent{% endif %}{% endif %} {{ item.class|default('') }}"
{{- self.getCustomWidth(item, menu, 'item', dropdown) }}
{%- if context.particle.renderTitles|default(0) %} title="{{ item.title }}"{% endif %}{{listAttributes|raw}}>
{% if item.url %}
Expand All @@ -108,7 +108,7 @@
<span class="g-menu-item-content">
{{ self.displayTitle(item) }}
</span>
{% if item.children|length and not item.dropdown_hide -%}
{% if parent and not item.dropdown_hide -%}
<span class="g-menu-parent-indicator" data-g-menuparent=""></span>
{%- endif %}
{% else %}
Expand All @@ -119,13 +119,13 @@
{% else %}
<span class="g-separator g-menu-item-content"{{ title|raw }}>{{ self.displayTitle(item) }}</span>
{% endif %}
{% if item.children|length and not item.dropdown_hide -%}
{% if parent and not item.dropdown_hide -%}
<span class="g-menu-parent-indicator"></span>
{%- endif %}
{% endif %}
{% if item.url %}</a>
{% else %}</div>{% endif %}
{% if item.children|length -%}
{% if parent -%}
{{ self.displaySubmenu(item, menu, context, dropdown_type, start_level) }}
{%- endif %}
</li>
Expand Down
38 changes: 28 additions & 10 deletions src/classes/Gantry/Component/Menu/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,25 +259,35 @@ public function columnWidth($column)
*/
public function groups()
{
$children = $this->children();

// Grouped by column counts.
if ($this->items['columns_count']) {
$children = $this->children;

$i = 0; $start = 0;
$list = [];
foreach ($this->items['columns_count'] as $i => $count) {
$list[$i] = array_slice($children, $start, $count);
$list[$i] = array_slice($children, $start, $count, true);
$start += $count;
}
// Add missing items into the end of the list.
if (count($children) > $start) {
$list[$i] = array_merge($list[$i], array_slice($children, $start));
$list[$i] = array_merge($list[$i], array_slice($children, $start, null, true));
}

foreach ($list as &$items) {
foreach ($items as $id => &$item) {
$item = $this->menu()[$id];
}
unset($item);

$items = array_filter($items);
}
unset($items);

return $list;
}

// Grouped by explisit list.
// Grouped by explicit list.
if ($this->groups) {
$list = [];
foreach ($this->groups as $i => $group) {
Expand All @@ -293,8 +303,8 @@ public function groups()
return $list;
}

// No grouping (use first group).
return [$children];
// No grouping.
return [$this->children()];
}

/**
Expand All @@ -315,7 +325,7 @@ public function children()
*/
public function hasChildren()
{
return !empty($this->children);
return !empty($this->children());
}

/**
Expand Down Expand Up @@ -502,7 +512,11 @@ public function key()
#[\ReturnTypeWillChange]
public function next()
{
next($this->children);
while (false !== next($this->children)) {
if ($this->current()) {
break;
}
}
}

/**
Expand All @@ -514,6 +528,10 @@ public function next()
public function rewind()
{
reset($this->children);
$current = key($this->children);
if (!$this->menu()[$current]) {
$this->next();
}
}

/**
Expand All @@ -524,7 +542,7 @@ public function rewind()
#[\ReturnTypeWillChange]
public function count()
{
return count($this->children);
return count($this->children());
}

/**
Expand Down
38 changes: 36 additions & 2 deletions src/platforms/joomla/classes/Gantry/Framework/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Menu extends AbstractMenu
{
use GantryTrait;

/** @var bool */
protected $isAdmin;
/** @var CMSApplication */
protected $application;
/** @var \Joomla\CMS\Menu\AbstractMenu */
Expand All @@ -41,8 +43,10 @@ public function __construct()
/** @var CMSApplication $app */
$app = Factory::getApplication();
if ($app->isClient('administrator')) {
$this->isAdmin = true;
$this->application = CMSApplication::getInstance('site');
} else {
$this->isAdmin = false;
$this->application = $app;
}

Expand All @@ -58,6 +62,31 @@ public function __construct()
$this->active = $this->menu->getActive();
}

/**
* @param string|int $offset
* @return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return $this->offsetGet($offset) !== null;
}

/**
* @param string|int $offset
* @return Item|null
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
$item = isset($this->items[$offset]) ? $this->items[$offset] : null;
if (!$this->isAdmin) {
return $item && $item->enabled ? $item : null;
}

return $item;
}

/**
* @param array $params
*/
Expand Down Expand Up @@ -273,7 +302,12 @@ protected function getItemsFromPlatform($params)
$attributes = ['menutype'];
$values = [$params['menu']];

$items = array_merge($this->getMenuItemIds($params['menu']), $this->menu->getItems($attributes, $values));
$items = [];
foreach ($this->menu->getItems($attributes, $values) as $item) {
$items[$item->id] = $item;
}

$items = array_replace($this->getMenuItemIds($params['menu']), $items);
}

return $items;
Expand Down Expand Up @@ -497,7 +531,7 @@ public static function decodeJParams($params)

/**
* @param array $data
* @param MenuItem|null $menuItem
* @param MenuItem|object|null $menuItem
* @return Item
*/
protected function createMenuItem($data, $menuItem = null)
Expand Down

0 comments on commit 1c478ea

Please sign in to comment.