Skip to content

Commit

Permalink
[SonataImportBundle] Support for translation
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Apr 23, 2024
1 parent db27d1a commit c06293c
Show file tree
Hide file tree
Showing 30 changed files with 1,076 additions and 578 deletions.
50 changes: 50 additions & 0 deletions app/migrations/Version20240419194259.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240419194259 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE tag_translation (
id INT AUTO_INCREMENT NOT NULL,
translatable_id BIGINT DEFAULT NULL,
label VARCHAR(255) NOT NULL,
locale VARCHAR(5) NOT NULL,
INDEX IDX_A8A03F8F2C2AC5D3 (translatable_id),
UNIQUE INDEX tag_translation_unique_translation (translatable_id, locale),
PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE
tag_translation
ADD
CONSTRAINT FK_A8A03F8F2C2AC5D3 FOREIGN KEY (translatable_id) REFERENCES draw_acme__tag (id) ON DELETE CASCADE');
$this->addSql('DROP INDEX UNIQ_C052A2E4EA750E8 ON draw_acme__tag');
$this->addSql('ALTER TABLE draw_acme__tag CHANGE label name VARCHAR(255) NOT NULL');
$this->addSql('CREATE UNIQUE INDEX UNIQ_C052A2E45E237E06 ON draw_acme__tag (name)');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE tag_translation DROP FOREIGN KEY FK_A8A03F8F2C2AC5D3');
$this->addSql('DROP TABLE tag_translation');
$this->addSql('DROP INDEX UNIQ_C052A2E45E237E06 ON draw_acme__tag');
$this->addSql('ALTER TABLE draw_acme__tag CHANGE name label VARCHAR(255) NOT NULL');
$this->addSql('CREATE UNIQUE INDEX UNIQ_C052A2E4EA750E8 ON draw_acme__tag (label)');
}
}
26 changes: 22 additions & 4 deletions app/src/DataFixtures/AppFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,32 @@ public function load(ObjectManager $manager): void
private function loadTags(): iterable
{
yield 'admin' => (new Tag())
->setLabel('Admin');
->setName('admin')
->translate('en')
->setLabel('Admin')
->getTranslatable()
->translate('fr')
->setLabel('Administrateur')
->getTranslatable();

yield 'inactive' => (new Tag())
->setLabel('Inactive')
->setActive(false);
->setName('inactive')
->setActive(false)
->translate('en')
->setLabel('Inactive')
->getTranslatable()
->translate('fr')
->setLabel('Inactif')
->getTranslatable();

yield 'not-use' => (new Tag())
->setLabel('NotUse');
->setName('not-use')
->translate('en')
->setLabel('NotUse')
->getTranslatable()
->translate('fr')
->setLabel('Non Utilisé')
->getTranslatable();
}

private function loadUsers(): iterable
Expand Down
44 changes: 33 additions & 11 deletions app/src/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
use Doctrine\ORM\Mapping as ORM;
use Draw\Bundle\SonataExtraBundle\PreventDelete\PreventDelete;
use JMS\Serializer\Annotation as Serializer;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;

#[
ORM\Entity(repositoryClass: 'App\Repository\TagRepository'),
ORM\Table(name: 'draw_acme__tag'),
ORM\HasLifecycleCallbacks
]
#[UniqueEntity(fields: ['label'])]
#[UniqueEntity(fields: ['name'])]
#[PreventDelete(
relatedClass: User::class,
path: 'userTags.tag',
Expand All @@ -21,24 +24,25 @@
'path_label' => 'User tags',
]
)]
class Tag implements \Stringable
class Tag implements \Stringable, TranslatableInterface
{
use TranslatableTrait;
#[
ORM\Id,
ORM\GeneratedValue,
ORM\Column(name: 'id', type: 'bigint')
]
private ?int $id = null;

#[ORM\Column(name: 'active', type: 'boolean', options: ['default' => 1])]
private bool $active = true;

#[ORM\Column(name: 'label', type: 'string', length: 255, unique: true, nullable: false)]
#[ORM\Column(unique: true)]
#[
Assert\NotNull,
Assert\Length(min: 3, max: 255)
]
private ?string $label = null;
private ?string $name = null;

#[ORM\Column(name: 'active', type: 'boolean', options: ['default' => 1])]
private bool $active = true;

public function getId(): ?int
{
Expand All @@ -52,14 +56,14 @@ public function setId(int $id): self
return $this;
}

public function getLabel(): ?string
public function getName(): ?string
{
return $this->label;
return $this->name;
}

public function setLabel(string $label): static
public function setName(?string $name): static
{
$this->label = $label;
$this->name = $name;

return $this;
}
Expand All @@ -76,6 +80,18 @@ public function setActive(bool $active): static
return $this;
}

public function getLabel(): ?string
{
return $this->translate(fallbackToDefault: false)->getLabel();
}

public function setLabel(?string $label): static
{
$this->translate(fallbackToDefault: false)->setLabel($label);

return $this;
}

#[
Serializer\VirtualProperty,
Serializer\SerializedName('virtualProperty')
Expand All @@ -97,6 +113,12 @@ public function getVirtualPropertyArray(): array
return [1];
}

#[ORM\PreFlush]
public function preFlush(): void
{
$this->mergeNewTranslations();
}

public function __toString(): string
{
return (string) $this->getLabel();
Expand Down
55 changes: 55 additions & 0 deletions app/src/Entity/TagTranslation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslationTrait;
use Symfony\Component\Validator\Constraints as Assert;

#[
ORM\Entity,
ORM\Table(name: 'tag_translation')
]
class TagTranslation implements TranslationInterface
{
use TranslationTrait;

#[
ORM\Id,
ORM\GeneratedValue,
ORM\Column
]
private ?int $id = null;

#[ORM\Column]
#[
Assert\NotNull,
Assert\Length(min: 3, max: 255)
]
private ?string $label = null;

public function getId(): ?int
{
return $this->id;
}

public function setId(?int $id): static
{
$this->id = $id;

return $this;
}

public function getLabel(): ?string
{
return $this->label;
}

public function setLabel(?string $label): self
{
$this->label = $label;

return $this;
}
}
4 changes: 4 additions & 0 deletions app/src/Sonata/Admin/TagAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protected function configureListFields(ListMapper $list): void
{
$list
->addIdentifier('id')
->add('name')
->add('label')
->add('active');
}
Expand All @@ -32,13 +33,15 @@ protected function configureShowFields(ShowMapper $show): void
{
$show
->add('id')
->add('name')
->add('label')
->add('active');
}

protected function configureFormFields(FormMapper $form): void
{
$form
->add('name')
->add('label')
->add('active');
}
Expand All @@ -49,6 +52,7 @@ public function configureGridFields(array $fields): array
$fields,
[
'id' => [],
'name' => [],
'label' => [],
'active' => [],
'actions' => [
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"friendsofphp/php-cs-fixer": "^3.13",
"nesbot/carbon": "^2.0",
"doctrine/mongodb-odm-bundle": "*",
"fidry/cpu-core-counter": "^1.1"
"fidry/cpu-core-counter": "^1.1",
"knplabs/doctrine-behaviors": "*"
},
"require-dev": {
"ext-pcntl": "*",
Expand Down Expand Up @@ -120,7 +121,8 @@
"scheb/2fa-email": "^6.0",
"colinodell/psr-testlogger": "^1.1",
"cweagans/composer-patches": "^1.7",
"symfony/notifier": "^6.4.0"
"symfony/notifier": "^6.4.0",
"symfony/maker-bundle": "^1.58"
},
"replace": {
"draw/application": "self.version",
Expand Down
Loading

0 comments on commit c06293c

Please sign in to comment.