Add rule assignment configuration
The rule assignment configuration is available from Shopware Version 6.4.8.0

Overview

You want to create a custom card in the rule assignment, where you can add or delete assignments? This guide gets you covered on this topic. Based on an example of the configuration of the Dynamic Access plugin, you will see how to write your configuration.

Prerequisites

This guide does not explain how to create a new plugin for Shopware 6. Head over to our Plugin base guide to learn how to create a plugin at first:

Creating the index.js file

The first step is creating a new directory like so <plugin root>/src/Resources/app/administration/src/module/sw-settings-rule/extension/sw-settings-rule-detail-assignments. Right afterwards, create a new file called index.js in there.
Your custom module directory isn't known to Shopware 6 yet. The entry point of your plugin is the main.js file. That's the file you need to change now, so that it loads your extended component. For this, simply add the following line to your main.js file:
<plugin root>/src/Resources/app/administration/src/main.js
1
import './module/sw-settings-rule/extension/sw-settings-rule-detail-assignments';
Copied!
Now your module's index.js will be executed.

Override the component

Your index.js is still empty now, so let's override the sw-settings-rule-detail-assignments component. This is technically done by calling the method override method of our ComponentFactory, which is available through our third party wrapper. This method expects a name, and a configuration for the component you want to override.
<plugin root>/src/Resources/app/administration/src/module/sw-settings-rule/extension/sw-settings-rule-detail-assignments/index.js
1
Component.override('sw-settings-rule-detail-assignments', {
2
// override configuration here
3
});
Copied!

Overriding the computed

Now your plugin is overriding the sw-settings-rule-detail-assignments component, but currently this has no effect. In the associationEntitiesConfig computed property the configuration of the rule assignment is built and returned to the method which initiates the component. Because of this, you have to override this computed property, get the computed property of the original component, add your own configuration of the rule assignment and return the whole configuration array.
<plugin root>/src/Resources/app/administration/src/module/sw-settings-rule/extension/sw-settings-rule-detail-assignments/index.js
1
Component.override('sw-settings-rule-detail-assignments', {
2
computed: {
3
associationEntitiesConfig() {
4
const associationEntitiesConfig = this.$super('associationEntitiesConfig');
5
associationEntitiesConfig.push(/* insert your configuration here */);
6
return associationEntitiesConfig;
7
},
8
}
9
});
Copied!

Adding the configuration

The configuration of the rule assignment is passed as an object and offers a wide range of options. Just have a look onto one example configuration item of the Dynamic Access plugin:
Example of a configuration item
1
getRuleAssignmentConfig()
2
{
3
return [
4
{
5
id: 'swagDynamicAccessProducts',
6
notAssignedDataTotal: 0,
7
entityName: 'product',
8
label: 'swag-dynamic-access.sw-settings-rule.detail.associations.productVisibility',
9
criteria: () => {
10
const criteria = new Criteria();
11
criteria.setLimit(this.associationLimit);
12
criteria.addFilter(Criteria.equals('swagDynamicAccessRules.id', this.rule.id));
13
criteria.addAssociation('options.group');
14
criteria.addAssociation('swagDynamicAccessRules');
15
16
return criteria;
17
},
18
api: () => {
19
const api = Object.assign({}, Context.api);
20
api.inheritance = true;
21
22
return api;
23
},
24
detailRoute: 'sw.product.detail.base',
25
gridColumns: [
26
{
27
property: 'name',
28
label: 'Name',
29
rawData: true,
30
sortable: true,
31
routerLink: 'sw.product.detail.prices',
32
allowEdit: false,
33
},
34
],
35
deleteContext: {
36
type: 'many-to-many',
37
entity: 'product',
38
column: 'extensions.swagDynamicAccessRules',
39
},
40
addContext: {
41
type: 'many-to-many',
42
entity: 'swag_dynamic_access_product_rule',
43
column: 'productId',
44
searchColumn: 'name',
45
criteria: () => {
46
const criteria = new Criteria();
47
criteria.addFilter(
48
Criteria.not('AND', [Criteria.equals('swagDynamicAccessRules.id', this.rule.id)]),
49
);
50
criteria.addAssociation('options.group');
51
52
return criteria;
53
},
54
gridColumns: [
55
{
56
property: 'name',
57
label: 'Name',
58
rawData: true,
59
sortable: true,
60
allowEdit: false,
61
},
62
// ...
63
],
64
},
65
},
66
];
67
}
Copied!
Let's go through the most important entries, how to configure your rule assignment:
Option
Description
id
Required identifier for the assignment, which is arbitrary but unique
entityName, criteria, api
Required for data loading of the assignment
gridColumns
To define the columns, which are shown in your assignment card. Have a look into the data grid component for more information.

Provide to delete an assignment

If you want to provide to delete an assignment, you have to define the deleteContext. There are two types of the deleteContext. The first one is the one-to-many type, which link to a column of the assignment entity like this:
Example of a one-to-many deleteContext
1
deleteContext: {
2
type: 'one-to-many',
3
entity: 'cms_block',
4
column: 'extensions.swagCmsExtensionsBlockRule.visibilityRuleId',
5
},
Copied!
The other type is many-to-many, which has to link to the ManyToManyAssociationField of the extension like this:
Example of a many-to-many deleteContext
1
deleteContext: {
2
type: 'many-to-many',
3
entity: 'category',
4
column: 'extensions.swagDynamicAccessRules',
5
},
Copied!

Provide to add an assignment

If you want to provide to add an assignment, you have to define the addContext. This context has the same two types as the deleteContext (see above), but the addContext has more options to fill out, because an add assignment modal has to be configured:
Example of a one-to-many addContext
1
addContext: {
2
type: 'one-to-many',
3
entity: 'shipping_method',
4
column: 'availabilityRuleId',
5
searchColumn: 'name',
6
criteria: () => {
7
const criteria = new Criteria();
8
criteria.addFilter(Criteria.not(
9
'AND',
10
[Criteria.equals('availabilityRuleId', ruleId)],
11
));
12
13
return criteria;
14
},
15
gridColumns: [
16
{
17
property: 'name',
18
label: 'Name',
19
rawData: true,
20
sortable: true,
21
allowEdit: false,
22
},
23
{
24
property: 'description',
25
label: 'Description',
26
rawData: true,
27
sortable: true,
28
allowEdit: false,
29
},
30
{
31
property: 'taxType',
32
label: 'Tax calculation',
33
rawData: true,
34
sortable: true,
35
allowEdit: false,
36
},
37
{
38
property: 'active',
39
label: 'Active',
40
rawData: true,
41
sortable: true,
42
allowEdit: false,
43
},
44
],
45
},
Copied!
The addContext needs a definition of the gridColumns, the entity and the criteria, like in the general configuration. Also, the context needs the column of the assignment and the searchColumn of the assigned entity.
A context of the many-to-many type would look like this:
Example of a many-to-many addContext
1
addContext: {
2
type: 'many-to-many',
3
entity: 'swag_dynamic_access_category_rule',
4
column: 'categoryId',
5
searchColumn: 'name',
6
association: 'swagDynamicAccessRules',
7
criteria: () => {
8
const criteria = new Criteria();
9
criteria.addFilter(Criteria.equals('parentId', null));
10
11
return criteria;
12
},
13
gridColumns: [
14
// Definition of columns
15
],
16
},
Copied!
Beside the properties of a one-to-many type you have to define the association with the name of the ManyToManyAssociationField.

Further reading