Add data to CMS element

Overview

When creating custom CMS elements, you sometimes want to use more complex data types than text or boolean values, e.g. other entities such as media or products. In those cases you can implement a custom CmsElementResolver to resolve the configuration data.

Prerequisites

This guide will not explain how to create custom CMS elements in general, so head over to the official guide about Adding a custom CMS element to learn this first.

Create a data resolver

To manipulate the data of these elements during the loading of the configuration, we create a DailyMotionCmsElementResolver resolver in our plugin.
<plugin root>/src/DataResolver/DailyMotionCmsElementResolver.php
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\DataResolver;
4
5
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
6
use Shopware\Core\Content\Cms\DataResolver\Element\AbstractCmsElementResolver;
7
use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
8
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
9
use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
10
11
class DailyMotionCmsElementResolver extends AbstractCmsElementResolver
12
{
13
public function getType(): string
14
{
15
return 'dailymotion';
16
}
17
18
public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
19
{
20
return null;
21
}
22
23
public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
24
{
25
26
}
27
}
Copied!
Our custom resolver extends from the AbstractCmsElementResolver which forces us to implement the methods getType, collect and enrich.
In the previous example we added a cms element with the name dailymotion. As you can see the getType method of our custom resolver reflects that name by returning the dailymotion string. This resolver is called every time for an element of the type dailymotion.
To register our custom resolver to the service container we have to register it in the services.xml file in our plugin.
<plugin root>/src/Resources/config/services.xml
1
<?xml version="1.0" ?>
2
3
<container xmlns="http://symfony.com/schema/dic/services"
4
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6
7
<services>
8
<service id="Swag\BasicExample\DataResolver\DailyMotionCmsElementResolver">
9
<tag name="shopware.cms.data_resolver" />
10
</service>
11
</services>
12
</container>
Copied!

Collect data

The collect method prepares the criteria object. This is useful if, for example, you have a media entity ID stored in your configuration. As in the following example, you can retrieve the configuration for the current cms element with the call $slot->getFieldConfig() and then have access to the individual fields. In this case we read out myCustomMedia field which may contain a mediaId. If a mediaId exists, we create a new CriteriaCollection for it. Now we are able to use this media-object later on.
<plugin root>/src/DataResolver/DailyMotionCmsElementResolver.php
1
<?php declare(strict_types=1);
2
3
// ...
4
use Shopware\Core\Content\Media\MediaDefinition;
5
use Shopware\Core\Content\Media\MediaEntity;
6
// ...
7
8
public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
9
{
10
$config = $slot->getFieldConfig();
11
$myCustomMedia = $config->get('myCustomMedia');
12
13
if (!$myCustomMedia) {
14
return null;
15
}
16
17
$mediaId = $myCustomMedia->getValue();
18
19
$criteria = new Criteria([$mediaId]);
20
21
$criteriaCollection = new CriteriaCollection();
22
$criteriaCollection->add('media_' . $slot->getUniqueIdentifier(), MediaDefinition::class, $criteria);
23
24
return $criteriaCollection;
25
}
26
27
// ...
Copied!

Enrich data

Inside the enrich you can perform additional logic on the data that has been resolved. Like in the collect method, we have access to our configuration fields and their values. Imagine you have stored some information in the element configuration and want to perform an external Api call to fetch some additional data. After that you can add the response information to the current slot data by calling $slot->setData().
This could be a possible solution for that:
<plugin root>/src/DataResolver/DailyMotionCmsElementResolver.php
1
<?php declare(strict_types=1);
2
// ...
3
4
public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
5
{
6
$config = $slot->getFieldConfig();
7
$myCustomApiPayload = $config->get('myCustomApiPayload');
8
9
// perform some external api call with the payload `myCustomApiPayload`
10
$myCustomAPI = new MyCustomAPI();
11
12
$response = $myCustomAPI->query($myCustomApiPayload);
13
14
if ($response) {
15
$slot->setData($response);
16
}
17
}
18
19
// ...
Copied!
Last modified 18d ago