Skip to content

Commit 523692f

Browse files
committed
update cookie consent docs
1 parent d8b4145 commit 523692f

1 file changed

Lines changed: 20 additions & 34 deletions

File tree

guides/plugins/plugins/storefront/add-cookie-to-manager.md

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This guide is built upon the [Plugin base guide](../plugin-base-guide), so take
2121

2222
## Extend the cookie consent manager
2323

24-
Adding custom cookies requires you to listen to the `CookieGroupsCollectEvent` and add your custom cookies to the collection.
24+
Adding custom cookies requires you to listen to the `CookieGroupCollectEvent` and add your custom cookies to the collection.
2525

2626
::: tip
2727
It is recommended to use an event listener if you're listening to a single event. If you need to react to multiple events, an event subscriber is the better choice.
@@ -41,7 +41,7 @@ Start with creating the `services.xml` and registering your event listener.
4141

4242
<services>
4343
<service id="PluginName\Listener\CookieListener">
44-
<tag name="kernel.event_listener" event="Shopware\Storefront\Framework\Cookie\CookieGroupsCollectEvent"/>
44+
<tag name="kernel.event_listener" event="Shopware\Core\Content\Cookie\Event\CookieGroupCollectEvent"/>
4545
</service>
4646
</services>
4747
</container>
@@ -51,7 +51,7 @@ In the next step we'll create the actual listener class.
5151

5252
### Creating the listener
5353

54-
We need to create a class called `CookieListener` with an `__invoke` method. This method will be executed once the `CookieGroupsCollectEvent` is dispatched.
54+
We need to create a class called `CookieListener` with an `__invoke` method. This method will be executed once the `CookieGroupCollectEvent` is dispatched.
5555

5656
The event object that is passed to our listener method contains the cookie groups collection, which we can use to add our custom cookies.
5757

@@ -67,47 +67,33 @@ Let's have a look at an example:
6767

6868
namespace PluginName\Listener;
6969

70-
use Shopware\Storefront\Framework\Cookie\CookieGroupsCollectEvent;
71-
use Shopware\Core\Framework\Cookie\CookieEntry;
72-
use Shopware\Core\Framework\Cookie\CookieGroup;
70+
use Shopware\Core\Content\Cookie\Event\CookieGroupCollectEvent;
71+
use Shopware\Core\Content\Cookie\Struct\CookieEntry;
72+
use Shopware\Core\Content\Cookie\Struct\CookieGroup;
7373

7474
class CookieListener
7575
{
76-
public function __invoke(CookieGroupsCollectEvent $event): void
76+
public function __invoke(CookieGroupCollectEvent $event): void
7777
{
78-
$cookieGroups = $event->getCookieGroups();
78+
$cookieGroups = $event->cookieGroupCollection;
7979

8080
// Create a single cookie
81-
$singleCookie = new CookieEntry(
82-
'cookie.name',
83-
'cookie-key',
84-
'cookie value',
85-
30,
86-
'cookie.description'
87-
);
81+
$singleCookie = new CookieEntry('cookie.name');
82+
$singleCookie->setCookieKey('cookie-key');
83+
$singleCookie->setValue('cookie value');
84+
$singleCookie->setExpiration(30);
85+
$singleCookie->setDescription('cookie.description');
8886

8987
// Create entries collection for cookie group
9088
$groupEntries = [
91-
new CookieEntry(
92-
'cookie.first_child_name',
93-
'cookie-key-1',
94-
'cookie value',
95-
30
96-
),
97-
new CookieEntry(
98-
'cookie.second_child_name',
99-
'cookie-key-2',
100-
'cookie value',
101-
60
102-
)
89+
(new CookieEntry('cookie.first_child_name'))->setCookieKey('cookie-key-1')->setValue('cookie value')->setExpiration(30),
90+
(new CookieEntry('cookie.second_child_name'))->setCookieKey('cookie-key-2')->setValue('cookie value')->setExpiration(60)
10391
];
10492

10593
// Create a cookie group with multiple cookies
106-
$cookieGroup = new CookieGroup(
107-
'cookie.group_name',
108-
$groupEntries,
109-
'cookie.group_description'
110-
);
94+
$cookieGroup = new CookieGroup('cookie.group_name');
95+
$cookieGroup->setEntries($groupEntries);
96+
$cookieGroup->setDescription('cookie.group_description');
11197

11298
$cookieGroups->add($cookieGroup);
11399
$cookieGroups->add($singleCookie);
@@ -132,7 +118,7 @@ Cookie groups should not have the `cookie`, `value`, `expiration`, or `isRequire
132118

133119
## Migrating from CookieProviderInterface (Shopware 6.7.2 and earlier)
134120

135-
If you are upgrading from an older version, you might have used the `CookieProviderInterface` to add custom cookies. This interface is now deprecated and should be replaced with the `CookieGroupsCollectEvent`.
121+
If you are upgrading from an older version, you might have used the `CookieProviderInterface` to add custom cookies. This interface is now deprecated and should be replaced with the `CookieGroupCollectEvent`.
136122

137123
For backward compatibility, you can still use the `CookieProviderInterface` to provide cookies in the old array syntax. However, it is highly recommended to use the new event-based system to provide the new object structure.
138124

@@ -148,7 +134,7 @@ While this feature helps with GDPR compliance, shop owners are responsible for e
148134

149135
### How it works
150136

151-
1. Your plugin adds/modifies cookies via the `CookieGroupsCollectEvent`
137+
1. Your plugin adds/modifies cookies via the `CookieGroupCollectEvent`
152138
2. Shopware calculates a hash of the entire cookie configuration
153139
3. The hash is stored in the user's browser
154140
4. On the next visit, if the hash differs, the consent banner appears again

0 commit comments

Comments
 (0)