Skip to content

Instantly share code, notes, and snippets.

@mattbloomfield
Created December 5, 2024 16:29
Show Gist options
  • Save mattbloomfield/9f78ef95d1b9f53394ef397b8f899b3e to your computer and use it in GitHub Desktop.
Save mattbloomfield/9f78ef95d1b9f53394ef397b8f899b3e to your computer and use it in GitHub Desktop.
/** Return your navigation in this array */
public function getCpNavItem(): ?array
{
$subNavs = [];
$navItem = [
'label' => 'AI Content',
'url' => 'ai',
'icon' => $this->cpNavIconPath(),
];
$currentUser = Craft::$app->getUser()->getIdentity();
$subNavs['dashboard'] = [
'label' => 'Dashboard',
'url' => 'ai/dashboard',
];
if ($currentUser->can('ai:digest')) {
$subNavs['digest'] = [
'label' => 'Email Digest',
'url' => 'ai/digest',
];
}
if ($currentUser->can('ai:digest:prompts')) {
$subNavs['digestPrompts'] = [
'label' => 'Digest Prompts',
'url' => 'ai/digest/prompts',
];
}
if ($currentUser->can('ai:field-generation:view') || $currentUser->can('ai:field-generation:edit')) {
$subNavs['prompts'] = [
'label' => 'Field Generation',
'url' => 'ai/field-generation',
];
}
if (empty($subNavs)) {
return null;
}
// A single sub nav item is redundant
if (count($subNavs) === 1) {
$subNavs = [];
}
$navItem = array_merge($navItem, [
'subnav' => $subNavs,
]);
return $navItem;
}
/** Add you custom routes here */
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_CP_URL_RULES,
function (RegisterUrlRulesEvent $event) {
Craft::debug(
'UrlManager::EVENT_REGISTER_CP_URL_RULES',
__METHOD__
);
// Register our Control Panel routes
$event->rules = array_merge(
$event->rules,
$this->customAdminCpRoutes()
);
}
);
/** Configure custom routes
You can see I'm relying on a few controllers to handle
the view of specific pages */
protected function customAdminCpRoutes(): array
{
return [
'ai' => [
'template' => '_ai-content/cp/dashboard',
],
'ai/field-generation' => [
'route' => '_ai-content/field-generation/index'
],
'ai/digest' => [
'route' => '_ai-content/digest/index'
],
'ai/digest/prompts' => [
'route' => '_ai-content/digest/prompts'
],
'ai/dashboard' => [
'template' => '_ai-content/cp/dashboard',
],
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment