0

I use KnpDoctrineBehaviors (Sluggable,Translatable, etc ). Field that I use for slug, I have only in MyClassTranslations. So when I add Sluggable for my translations class, I have for each i18n entry different slug. How to solve this? Thank you!

denys281
  • 2,004
  • 2
  • 19
  • 38

1 Answers1

1

You can override the getter for title in your entity, and just call the translate method from KnpDoctrineBehaviors on your entity. It will try to get the title translation in your current locale, or fallback to default locale translation.

<?php

namespace AppBundle\Entity;

use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use AppBundle\Entity\AppTrait\TranslatableEntity;

class SomeEntity
{
    use ORMBehaviors\Translatable\Translatable;
    use ORMBehaviors\Sluggable\Sluggable;

    public function getSluggableFields()
    {
        return ['title'];
    }

    public function getTitle()
    {
        return $this->translate(null,true)->getTitle();
    }

This implies that the slug might be updated often if you update the title with different locale, you might want to avoid this, and just use english title for slug generation:

    public function getTitle()
    {
        return $this->translate('en')->getTitle();
    }
Jean-Christophe Meillaud
  • 1,961
  • 1
  • 21
  • 27