Add your Own Entity, for Email Templating in Oro Commerce
To make entity available for the email template you need to make it configurable with the Config Annotation.
To make the SellerReview entity available for templating, simply import the @Config annotation and use it in the class.
<?php
namespace Webkul\MarketplaceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\Config;
/**
* SellerReview.
*
* @ORM\Entity
* @ORM\Table(name="wk_mp_seller_reviews")
* @Config()
*/
class SellerReview
{
/**
* @var int
*/
private $id;
/**
* @var string
*/
private $heading;
/**
* @var string
*/
private $text;
/**
* @var \DateTime
*/
private $createdAt;
You can also change the default value of each configurable option using the default values argument:
<?php
namespace MarketplaceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\Config;
use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\ConfigField;
/**
* SellerReview.
*
* @ORM\Entity
* @ORM\Table(name="wk_mp_seller_reviews")
* @Config(
* defaultValues={
* "wk_mp_seller_reviews"={
* "comment"="reviews"
* }
* }
* )
*/
class SellerReview
{
/**
* @var int
*/
private $id;
/**
* @var string
*/
private $heading;
/**
* @var string
*/
private $text;
/**
* @var \DateTime
*/
private $createdAt;
/**
* @var bool
*/
Then you need to register this function in the Email templates using the Twig environment.
<?php
namespace Webkul\MarketplaceBundle\Twig;
use Twig\Extension\AbstractExtension;
use Webkul\MarketplaceBundle\Entity\Transaction;
use Twig\TwigFunction;
class EmailTwigExtension extends AbstractExtension
{
public function getFunctions()
{
return [new TwigFunction('getseller', [$this, 'getSomeVariableValue'])];
}
public function getSomeVariableValue(Transaction $entity): array
{
$result[] = [
'seller' => $entity->getAmount(),
];
return $result;
}
}
Register the Twig extension in the DI container:
wk_seller_product.twig.my_extension:
class: Webkul\MarketplaceBundle\Twig\EmailTwigExtension
public: false
tags:
- { name: twig.extension }
Create a DI compiler pass to register the created extension and function in the Email Twig Environment:
<?php
namespace Webkul\MarketplaceBundle\DependencyInjection\CompilerPass;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class TwigSandboxConfigurationPass implements CompilerPassInterface
{
const EMAIL_TEMPLATE_SANDBOX_SECURITY_POLICY_SERVICE_KEY = 'oro_email.twig.email_security_policy';
const EMAIL_TEMPLATE_RENDERER_SERVICE_KEY = 'oro_email.email_renderer';
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$securityPolicyDef = $container->getDefinition('oro_email.twig.email_security_policy');
$securityPolicyDef->replaceArgument(
4,
array_merge($securityPolicyDef->getArgument(4), ['getseller'])
);
$container->getDefinition('oro_email.email_renderer')
->addMethodCall('addExtension', [new Reference('wk_seller_product.twig.my_extension')]);
}
}
Register the created compiler pass:
<?php
namespace Webkul\MarketplaceBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Webkul\MarketplaceBundle\DependencyInjection\CompilerPass\TwigSandboxConfigurationPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class WebkulMarketplaceBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new TwigSandboxConfigurationPass());
parent::build($container);
}
}
Once you complete these steps, “your entity function” becomes available in Email templates.
I have explored these things while creating module for OroCommerce Multi-Vendor Marketplace.
Thanks for reading me. I hope you’ll get an idea of how you can use your entity for email templating in Oro-Commerce, please share your reviews on this, that will support me to write more.