Skip to content

Commit

Permalink
Merge pull request #33702 from M0rgan01/improvement/33604
Browse files Browse the repository at this point in the history
[twig component] Make navigation bar management independent
  • Loading branch information
M0rgan01 committed Sep 1, 2023
2 parents 6285d72 + bc4509c commit 2bcb9a7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 35 deletions.
2 changes: 0 additions & 2 deletions admin-dev/themes/new-theme/template/layout.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@
table=$table
help_link=$help_link
enableSidebar=$enableSidebar
current_tab_level=$current_tab_level
tabs=$tabs
use_regular_h1_structure=$use_regular_h1_structure
}
{/if}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,30 +147,22 @@
</div>
{% endif %}

{% if currentTabLevel is same as 3 %}
{% if currentTabLevel >= 3 %}
<div class="page-head-tabs" id="head_tabs">
<ul class="nav nav-pills">
{% for level_1 in tabs %}
{% for level_2 in level_1.sub_tabs %}
{% for level_3 in level_2.sub_tabs %}
{% if level_3.current %}
{% for level_4 in level_3.sub_tabs %}
{% if level_4.active %}
<li class="nav-item">
<a href="{{ level_4.href }}" id="subtab-{{ level_4.class_name }}"
class="nav-link tab {% if level_4.current %}active current{% endif %}"
data-submenu="{{ level_4.id_tab }}">
{{ level_4.name }}
<span class="notification-container">
<span class="notification-counter"></span>
</span>
</a>
</li>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
{% for tab in navigationTabs %}
{% if tab.attributes.active %}
<li class="nav-item">
<a href="{{ tab.href }}" id="subtab-{{ tab.attributes.class_name }}"
class="nav-link tab {% if tab.attributes.current %}active current{% endif %}"
data-submenu="{{ tab.attributes.id_tab }}">
{{ tab.name }}
<span class="notification-container">
<span class="notification-counter"></span>
</span>
</a>
</li>
{% endif %}
{% endfor %}
</ul>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,5 @@
table: table,
helpLink: help_link,
enableSidebar: enableSidebar,
currentTabLevel: current_tab_level,
tabs: tabs,
useRegularH1Structure: use_regular_h1_structure
}) }}
9 changes: 7 additions & 2 deletions src/PrestaShopBundle/Twig/Component/Toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class Toolbar
public string $table;
public bool|string $helpLink;
public bool $enableSidebar;
public int $currentTabLevel;
public array $tabs;
public int $currentTabLevel = 0;
public array $navigationTabs = [];
public array $breadcrumbs;
public bool $useRegularH1Structure = true;

Expand All @@ -63,6 +63,11 @@ public function mount(): void
$ancestorsTab = $this->menuBuilder->getAncestorsTab($tab->getId());
if (!empty($ancestorsTab)) {
$tabs[] = $ancestorsTab;
$this->currentTabLevel = count($ancestorsTab);

if ($this->currentTabLevel >= 3) {
$this->navigationTabs = $this->menuBuilder->buildNavigationTabs($tab);
}
}

$this->breadcrumbs = $this->menuBuilder->convertTabsToBreadcrumbLinks($tab, $ancestorsTab);
Expand Down
46 changes: 39 additions & 7 deletions src/PrestaShopBundle/Twig/Layout/MenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,40 @@ public function convertTabsToBreadcrumbLinks(Tab $currentTab, array $tabAncestor

private function convertTabToMenuLink(Tab $tab): MenuLink
{
if (!empty($tab->getRouteName())) {
$href = $this->urlGenerator->generate($tab->getRouteName());
} else {
$href = $this->context->getAdminLink($tab->getClassName());
}

return new MenuLink(
name: $this->getBreadcrumbLabel($tab),
href: $href,
href: $this->getLinkFromTab($tab),
icon: 'icon-' . $tab->getClassName(),
);
}

/**
* @return array<int, MenuLink>
*/
public function buildNavigationTabs(Tab $tab): array
{
$currentLevelTabs = $this->tabRepository->findByParentId($tab->getIdParent());
$navigationTabs = [];

/* @var $currentLevelTab Tab */
foreach ($currentLevelTabs as $currentLevelTab) {
$tabLang = $currentLevelTab->getTabLangByLanguageId($this->getContextLanguageId());
$menuLink = new MenuLink(
name: $tabLang ? $tabLang->getName() : $currentLevelTab->getWording(),
href: $this->getLinkFromTab($currentLevelTab),
attributes: [
'id_tab' => $currentLevelTab->getId(),
'class_name' => $currentLevelTab->getClassName(),
'current' => $currentLevelTab->getId() == $tab->getId(),
'active' => $currentLevelTab->getActive(),
]
);
$navigationTabs[] = $menuLink;
}

return $navigationTabs;
}

private function getBreadcrumbLabel(Tab $tab): string
{
if (null !== $tab->getWording() && null !== $tab->getWordingDomain()) {
Expand Down Expand Up @@ -198,4 +219,15 @@ private function getLegacyControllerClassName(): ?string
{
return $this->requestStack->getMainRequest()->attributes->get('_legacy_controller');
}

private function getLinkFromTab(Tab $tab): string
{
if (!empty($tab->getRouteName())) {
$href = $this->urlGenerator->generate($tab->getRouteName());
} else {
$href = $this->context->getAdminLink($tab->getClassName());
}

return $href;
}
}

0 comments on commit 2bcb9a7

Please sign in to comment.