Add shortcuts to Craft's control panel
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.
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:
use Craft;
use craft\web\twig\variables\Cp;
use craft\events\RegisterCpNavItemsEvent;
Event::on(Cp::class, Cp::EVENT_REGISTER_CP_NAV_ITEMS, function (RegisterCpNavItemsEvent $event) {
if (Craft::$app->getConfig()->getGeneral()->allowAdminChanges) {
$event->navItems[] = [
'url' => 'settings/fields',
'label' => 'Fields',
'icon' => '@appicons/field.svg',
];
}
});
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.
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 /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.