Creating Elements On The Fly
There are two methods to make sure that related items exist during your imports. The appropriate method for you will depend on your preference and your specific data set.
- Import the related items first, then use the
related
key to match them based on a known attribute. SeeResolving Relationships for more info on this method. - Try to match related items using the
related
key, and dynamically create the related items if no match is found using thecreateIfNotFound
andnewElements
keys.
In this example, we first try to match related categories by slug, and if we don't find them, we also define the elements so they can be created on the fly using the createIfNotFound
and newElements
keys.
[
{
"@model": "barrelstrength\\sproutbaseimport\\importers\\elements\\Entry",
"attributes": { ... },
"content": {
"title": "In quaerat maiores sit est rerum iusto.",
"fields": { ... },
"related": {
"categoryHandle": {
"@model": "barrelstrength\\sproutbaseimport\\importers\\elements\\Category",
"params": {
"slug": ["san-francisco", "london"]
},
"createIfNotFound": true,
"newElements": [
{
"@model": "barrelstrength\\sproutbaseimport\\importers\\elements\\Category",
"attributes": {
"groupId": 1,
"enabled": true
},
"content": {
"title": "San Francisco"
}
},
{
"@model": "barrelstrength\\sproutbaseimport\\importers\\elements\\Category",
"attributes": {
"groupId": 1,
"enabled": true
},
"content": {
"title": "London"
}
}
]
}
}
}
}
]
This behavior requires two keys within the related
tag.
- createIfNotFound must be set to
true
, to trigger this behavior - newElements must provide one or more model definitions of an Element that matches the type of Element allowed by the Relation. Each item defined within the
newElements
key, follows the same Craft JSON Format syntax as importing an element on its own.