Skip to content

Commit

Permalink
Add site dropdown to navigation index
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Aug 25, 2022
1 parent 57ccfa9 commit 56fa881
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
11 changes: 4 additions & 7 deletions src/controllers/NavsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,16 @@ class NavsController extends Controller

public function actionIndex(): Response
{
$navigations = Navigation::$plugin->getNavs()->getEditableNavs();
// Get the current site from the global query param
$siteHandle = Craft::$app->getRequest()->getParam('site', Craft::$app->getSites()->getPrimarySite()->handle);
$site = Craft::$app->getSites()->getSiteByHandle($siteHandle);

$siteHandles = [];

foreach (Craft::$app->getSites()->getEditableSites() as $site) {
$siteHandles[$site->id] = $site->handle;
}
$navigations = Navigation::$plugin->getNavs()->getEditableNavsForSite($site);

$editable = Craft::$app->getConfig()->getGeneral()->allowAdminChanges;

return $this->renderTemplate('navigation/navs/index', [
'navigations' => $navigations,
'siteHandles' => $siteHandles,
'editable' => $editable,
]);
}
Expand Down
17 changes: 17 additions & 0 deletions src/services/Navs.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ public function getEditableNavs(): array
}, true, true, false);
}

public function getEditableNavsForSite($site): array
{
if (Craft::$app->getRequest()->getIsConsoleRequest()) {
return $this->getAllNavs();
}

$user = Craft::$app->getUser()->getIdentity();

if (!$user) {
return [];
}

return ArrayHelper::where($this->getAllNavs(), function(NavModel $nav) use ($user, $site) {
return $user->can("navigation-manageNav:$nav->uid") && in_array($site->id, $nav->getSiteIds());
}, true, true, false);
}

public function getEditableNavIds(): array
{
return ArrayHelper::getColumn($this->getEditableNavs(), 'id');
Expand Down
30 changes: 19 additions & 11 deletions src/templates/navs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
{ label: 'Navigations' | t('navigation'), url: url('navigation/navs') },
] %}

{% set contextMenu %}
{% if craft.app.getIsMultiSite() %}
{% include '_elements/sitemenu' %}
{% endif %}
{% endset %}

{% block actionButton %}
{% if editable and currentUser.can('navigation-createNavs') %}
<div id="button-container">
Expand Down Expand Up @@ -77,18 +83,20 @@
tableData: {{ tableData | json_encode | raw }},
});

var sites = {{ siteHandles | json_encode | raw }};
// When changing the site select, navigate to the navigation index for that site.
var $siteMenuBtn = $('#header .sitemenubtn:first');

// Get the local storage and add the storedSiteid param to the URL's.
$('.nav-anchor').each(function() {
var $this = $(this);
var _href = $this.attr('href');
if (this.$siteMenuBtn.length) {
var siteMenu = $siteMenuBtn.menubtn().data('menubtn').menu;

var siteId = localStorage.getItem('Craft-' + Craft.systemUid + '.BaseElementIndex.siteId');

if (siteId !== null && sites[siteId]) {
$this.attr('href', _href + '/' + sites[siteId]);
}
});
siteMenu.on('optionselect', function(ev) {
siteMenu.$options.removeClass('sel');
var $option = $(ev.selectedOption).addClass('sel');
$siteMenuBtn.html($option.html());
Craft.cp.setSiteId($option.data('site-id'));

location.reload();
});
}

{% endjs %}

0 comments on commit 56fa881

Please sign in to comment.