Skip to content

Instantly share code, notes, and snippets.

@ylastapis
Last active September 1, 2016 07:51
Show Gist options
  • Save ylastapis/582d79d65225bb61998e6a313e4dbf24 to your computer and use it in GitHub Desktop.
Save ylastapis/582d79d65225bb61998e6a313e4dbf24 to your computer and use it in GitHub Desktop.
Add a Custom Entity with translations to Sylius
doctrine:
orm:
resolve_target_entities:
AppBundle\Entity\ProductCategoryInterface: AppBundle\Entity\ProductCategory
// src/AppBundle/Resources/config/doctrine/ProductCategory.orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<mapped-superclass name="AppBundle\Entity\ProductCategory" table="app_product_category" repository-class="AppBundle\Entity\Repository\ProductCategoryRepository">
<id name="id" column="id" type="integer">
<generator strategy="AUTO" />
</id>
</mapped-superclass>
</doctrine-mapping>
// src/AppBundle/Entity/ProductCategory.php
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Sylius\Component\Resource\Model\TranslatableTrait;
class ProductCategory implements ProductCategoryInterface
{
use TranslatableTrait {
__construct as private initializeTranslationsCollection;
}
public function __construct()
{
$this->initializeTranslationsCollection();
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->translate()->getName();
}
/**
* {@inheritdoc}
*/
public function setName($name)
{
$this->translate()->setName($name);
}
}
// src/AppBundle/Entity/ProductCategoryInterface.php
<?php
namespace AppBundle\Entity;
use Sylius\Component\Resource\Model\ResourceInterface;
interface ProductCategoryInterface extends
ResourceInterface,
ProductCategoryTranslationInterface
{
}
// src/AppBundle/Entity/Repository/ProductCategoryRepository.php
<?php
namespace AppBundle\Entity\Repository;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
class ProductCategoryRepository extends EntityRepository implements ProductCategoryRepositoryInterface
{}
// src/AppBundle/Entity/Repository/ProductCategoryRepositoryInterface.php
<?php
namespace AppBundle\Entity\Repository;
interface ProductCategoryRepositoryInterface
{}
// src/AppBundle/Resources/config/doctrine/ProductCategoryTranslation.orm.xml
<?xml version="1.0" encoding="UTF-8" ?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<mapped-superclass name="AppBundle\Entity\ProductCategoryTranslation" table="app_product_category_translation" >
<id name="id" column="id" type="integer">
<generator strategy="AUTO" />
</id>
<field name="name" column="name" type="string" />
</mapped-superclass>
</doctrine-mapping>
// src/AppBundle/Entity/ProductCategoryTranslation.php
<?php
namespace AppBundle\Entity;
use Sylius\Component\Resource\Model\AbstractTranslation;
class ProductCategoryTranslation extends AbstractTranslation implements ProductCategoryTranslationInterface
{
/**
* @var string
*/
protected $name;
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->name;
}
/**
* {@inheritdoc}
*/
public function setName($name)
{
$this->name = $name;
}
}
// src/AppBundle/Entity/ProductCategoryTranslationInterface.php
<?php
namespace AppBundle\Entity;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Model\SlugAwareInterface;
interface ProductCategoryTranslationInterface extends SlugAwareInterface, ResourceInterface
{
/**
* @return string
*/
public function getName();
/**
* @param string $name
*/
public function setName($name);
}
// src/AppBundle/Form/Type/ProductCategoryTranslationType.php
<?php
namespace AppBundle\Form\Type;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class ProductCategoryTranslationType extends AbstractResourceType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, [
'label' => 'sylius.form.product_category.name',
])
;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'sylius_product_category_translation';
}
}
// src/AppBundle/Form/Type/ProductCategoryType.php
<?php
namespace AppBundle\Form\Type;
use Sylius\Bundle\ResourceBundle\Form\EventSubscriber\AddCodeFormSubscriber;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceTranslationsType;
use Symfony\Component\Form\FormBuilderInterface;
class ProductCategoryType extends AbstractResourceType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('translations', ResourceTranslationsType::class, [
'type' => 'sylius_product_category_translation',
'label' => 'sylius.form.product_category.name',
])
->addEventSubscriber(new AddCodeFormSubscriber())
;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'sylius_product_category';
}
}
sylius_resource:
resources:
app.product_category:
classes:
model: AppBundle\Entity\ProductCategory
interface: AppBundle\Entity\ProductCategoryInterface
repository: AppBundle\Entity\Repository\ProductCategoryRepository
form:
default: AppBundle\Form\Type\ProductCategoryType
translation:
classes:
model: AppBundle\Entity\ProductCategoryTranslation
interface: AppBundle\Entity\ProductCategoryTranslationInterface
form:
default: AppBundle\Form\Type\ProductCategoryTranslationType
services:
app.form.type.product_category:
class: AppBundle\Form\Type\ProductCategoryType
arguments: [ "%app.model.product_category.class%" ]
tags:
- { name: form.type, alias: sylius_product_category }
app.form.type.product_category_translation:
class: AppBundle\Form\Type\ProductCategoryTranslationType
arguments: [ "%app.model.product_category_translation.class%" ]
tags:
- { name: form.type, alias: sylius_product_category_translation }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment