{
"path": "/articles/2023-add-shortcuts-to-crafts-control-panel",
"site": "at://did:plc:kq2tuvvnlen4jjcqiw4oprjm/site.standard.publication/3mhyhqc3kcaw7",
"tags": [
"Craft CMS",
"Tips & tricks"
],
"$type": "site.standard.document",
"title": "Add shortcuts to Craft's control panel",
"content": {
"html": "<div><h2></h2><p>Only prerequisite for this is that you have a Yii module in the project you're building. Then, in the Module's <code>init()</code> function, we'll use the <code>EVENT_REGISTER_CP_NAV_ITEMS</code> event to add one or more links:</p>\n</div><div><h2></h2><pre><code>use Craft;\nuse craft\\web\\twig\\variables\\Cp;\nuse craft\\events\\RegisterCpNavItemsEvent;\n\n Event::on(Cp::class, Cp::EVENT_REGISTER_CP_NAV_ITEMS, function (RegisterCpNavItemsEvent $event) {\n if (Craft::$app->getConfig()->getGeneral()->allowAdminChanges) {\n $event->navItems[] = [\n 'url' => 'settings/fields',\n 'label' => 'Fields',\n 'icon' => '@appicons/field.svg',\n ];\n }\n });</code></pre></div><div><h2></h2><p>Each item we add needs to have 3 properties: a url, a label and an icon. Url & label speak for themselves, for the icon you can reference an svg icon of your own - or you can piggy-back of the icons that come with Craft. </p>\n<p>This is the way I prefer to set things up, as keeping the icons for fields/sections/sites etc the same makes it easier recognise. In <code>/craftcms/cms/src/icons</code>, you can find all the SVG icons that come with Craft CMS, and you can use them through the <code>@appicons</code> alias, like in the example above.</p>\n</div>",
"$type": "org.wordpress.html"
},
"updatedAt": "2024-01-04T17:30:27+01:00",
"description": "When building out a site, I often find find myself clicking through to fields, sections and sites repeatedly. To make this a tiny bit easier, I like to add shortcuts to the Control Panel sidebar for quick access to the things I need most often.",
"publishedAt": "2023-12-30T11:04:00+01:00",
"textContent": "Only prerequisite for this is that you have a Yii module in the project you're building. Then, in the Module's init() function, we'll use the EVENT_REGISTER_CP_NAV_ITEMS event to add one or more links:\nuse Craft;\nuse craft\\web\\twig\\variables\\Cp;\nuse craft\\events\\RegisterCpNavItemsEvent;\n\n Event::on(Cp::class, Cp::EVENT_REGISTER_CP_NAV_ITEMS, function (RegisterCpNavItemsEvent $event) {\n if (Craft::$app->getConfig()->getGeneral()->allowAdminChanges) {\n $event->navItems[] = [\n 'url' => 'settings/fields',\n 'label' => 'Fields',\n 'icon' => '@appicons/field.svg',\n ];\n }\n });Each item we add needs to have 3 properties: a url, a label and an icon. Url & label speak for themselves, for the icon you can reference an svg icon of your own - or you can piggy-back of the icons that come with Craft. \nThis is the way I prefer to set things up, as keeping the icons for fields/sections/sites etc the same makes it easier recognise. In /craftcms/cms/src/icons, you can find all the SVG icons that come with Craft CMS, and you can use them through the @appicons alias, like in the example above.\n"
}