Context decorators
This is an article for advanced users of Frontastic.
Context decorators are used to add additional information to the context and can be used to customize user locale handling. The context is available in the PHP and JavaScript stacks in many places and has information about the current customer, project, user, and other general configurations. An important part of the context is the user's locale which can explicitly be determined using a custom locale resolver.
Create a context decorator
You can use the default Symfony mechanism in the dependency injection container to register a context decorator. To do this, add the below lines to a services.xml
in one of your project bundles (if there isn't a bundle yet, you can create one using bin/console frontastic:create:bundle MyDecorators
(see the creating a backend bundle article for more information), for example):
<service id="Acme\MyDecorators\Domain\MyContextDecorator">
<!-- <argument type="service" id="some.service" /> -->
<tag name="context.decorator" />
</service>
The context decorator receives the context and can modify or add information to it. The result will be cached for the current request:
namespace Acme\MyDecorators\Domain;
use Frontastic\Catwalk\ApiCoreBundle\Domain\Context;
use Frontastic\Catwalk\ApiCoreBundle\Domain\ContextDecorator;
class MyContextDecorator implements ContextDecorator
{
public function decorate(Context $context): Context
{
$context->projectConfiguration['key'] = 42;
return $context;
}
}
In the example above, we added a new setting to the projectConfiguration
inside the context. Everything we set here in the backend (this could be based on the current user, for example) is available in all the Frontastic components using Tastify.
You could also add additional information here, for example, the user session, set feature flags, or embed any other global user-specific state.
Updated over 2 years ago