Tanmar
Tanmar

Reputation: 79

Shopware 6 Plugin - Read Theme Config in Plugin Subscriber

In twig I can access my theme config values via theme_config.

I am looking for a way to do this cleanly in a subscriber.

I tried to get them via the Shopware\Storefront\Theme\ThemeConfigValueAccessor:

$salesChannelContext = $event->getSalesChannelContext();
$this->themeConfigAccessor->get('theme-config-value-key', $salesChannelContext, null);

But I need the $themeId as a third parameter and I did not find a way to get the Theme of a current SalesChannel, without reading from the DB.

Does someone have a solution for this case? Is there a best practice way?

Upvotes: 2

Views: 469

Answers (1)

j_elfering
j_elfering

Reputation: 3190

You can get the the current theme directly from the request, no need to go to the DB for that:

$themeId = $request->attributes->get(SalesChannelRequest::ATTRIBUTE_THEME_ID);

Keep in mind that the attribute may not be set, e.g. if the request is no storefront request. So you should add a null check for $themeId.

You can get the current request either from the event you subscribe to directly if the event provides that. If not you can inject the request_stack service into your subscriber and get the current request from there.

Upvotes: 3

Related Questions