<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
locastic / api-platform-translation-bundle example snippets
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
use Symfony\Component\Serializer\Attribute\Groups;
#[ORM\Entity]
#[ApiResource(
operations: [
new Get(),
new GetCollection(),
new Post(normalizationContext: ['groups' => ['translations']]),
new Patch(normalizationContext: ['groups' => ['translations']]),
// PUT replaces the resource; standard_put must be off so it edits the
// managed entity instead of building a new one (see the notes below).
new Put(
normalizationContext: ['groups' => ['translations']],
extraProperties: ['standard_put' => false],
),
],
normalizationContext: ['groups' => ['article_read']],
denormalizationContext: ['groups' => ['article_write']],
filters: ['translation.groups'],
)]
class Article extends AbstractTranslatable
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\OneToMany(
targetEntity: ArticleTranslation::class,
mappedBy: 'translatable',
fetch: 'EXTRA_LAZY',
indexBy: 'locale',
cascade: ['persist'],
orphanRemoval: true,
)]
#[Groups(['article_write', 'translations'])]
protected Collection $translations;
public function getId(): ?int
{
return $this->id;
}
#[Groups(['article_read'])]
public function getTitle(): ?string
{
return $this->getTranslation()->getTitle();
}
public function setTitle(string $title): void
{
$this->getOrCreateTranslation()->setTitle($title);
}
protected function createTranslation(): TranslationInterface
{
return new ArticleTranslation();
}
}
use Doctrine\ORM\Mapping as ORM;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslation;
use Locastic\ApiPlatformTranslationBundle\Model\TranslatableInterface;
use Symfony\Component\Serializer\Attribute\Groups;
#[ORM\Entity]
class ArticleTranslation extends AbstractTranslation
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['translations'])]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: Article::class, inversedBy: 'translations')]
protected ?TranslatableInterface $translatable = null;
#[ORM\Column]
#[Groups(['article_read', 'article_write', 'translations'])]
private ?string $title = null;
#[ORM\Column]
#[Groups(['article_write', 'translations'])]
protected ?string $locale = null;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): void
{
$this->title = $title;
}
}
GET /articles/1?locale=de
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.