Add shortcuts

Overview

Shortcuts in Shopware 6 are defined on a Component basis. This guide will show you how to add your own ones.

Prerequisites

All you need for this guide is a running Shopware 6 instance and full access to both the files and preferably a registered module and custom component. Of course you'll have to understand JavaScript, but that's a prerequisite for Shopware as a whole and will not be taught as part of this documentation.

Configuring the Shortcuts

The following code sample will show you how to register shortcuts in your components with help of the shortcuts attribute.
<plugin root>/src/Resources/app/administration/src/module/swag-example/index.js
1
const { Component } = Shopware;
2
3
Component.register('swag-basic-example', {
4
5
shortcuts: {
6
'SYSTEMKEY+S': {
7
active() {
8
return this.acl.can('product.editor');
9
},
10
method: 'myEditProductFunction'
11
},
12
ESCAPE: 'myCancelEditProductFunction'
13
},
14
15
16
methods: {
17
myEditProductFunction() {
18
console.log("myEditProductFunction")
19
},
20
myCancelEditProductFunction() {
21
console.log("myCancelEditProductFunction")
22
}
23
}
24
});
Copied!
The first keyboard shortcut reacts to the key combination of SYSTEMKEY+S, only if the user has the privilege product.editor, with the invocation of the component method with the name myEditProductFunction. The second keyboard shortcut defines that, upon the ESCAPE key being pressed, the function with the name myCancelEditProductFunction should be invoked.
The before mentioned SYSTEMKEY is CTRL on macOS and ALT on Windows, other system-keys like CTRL on Windows or on macOS are not supported.
Since ACL is used in the first keyboard shortcut you might want to learn more about ACL and how to add your own ACL rules here.

More interesting topics