Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OP-544: Page teaser translatable #531

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions spec/Twig/Runtime/TranslationFormReduceRuntimeSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

declare(strict_types=1);

namespace spec\Sylius\CmsPlugin\Twig\Runtime;

use PhpSpec\ObjectBehavior;
use Sylius\CmsPlugin\Twig\Runtime\TranslationFormReduceRuntime;
use Symfony\Component\Form\FormView;

class TranslationFormReduceRuntimeSpec extends ObjectBehavior
{
public function it_is_initializable(): void
{
$this->shouldHaveType(TranslationFormReduceRuntime::class);
}

public function it_reduces_form_to_specified_fields(
FormView $form,
FormView $localeForm,
FormView $slugForm,
FormView $titleForm
): void {
$form->children = [
'en_US' => $localeForm
];

$localeForm->children = [
'slug' => $slugForm,
'title' => $titleForm,
'metaDescription' => new FormView(),
];

$result = $this->reduceTranslationForm($form, ['slug', 'title']);
$result->shouldHaveKey('en_US');
$result['en_US']->shouldHaveKey('slug');
$result['en_US']->shouldHaveKey('title');
$result['en_US']->shouldNotHaveKey('metaDescription');
}

public function it_throws_exception_if_field_is_not_found(FormView $form, FormView $localeForm): void
{
$form->children = [
'en_US' => $localeForm,
];

$localeForm->children = [
'metaDescription' => new FormView(),
];

$this->shouldThrow(\InvalidArgumentException::class)->during('reduceTranslationForm', [$form, ['slug', 'title']]);
}

public function it_handles_multiple_locales(
FormView $form,
FormView $enLocale,
FormView $deLocale,
FormView $slugForm,
FormView $titleForm
): void {
$form->children = [
'en_US' => $enLocale,
'de_DE' => $deLocale,
];

$enLocale->children = [
'slug' => $slugForm,
'title' => $titleForm,
];

$deLocale->children = [
'slug' => $slugForm,
'title' => $titleForm,
];

$result = $this->reduceTranslationForm($form, ['slug', 'title']);
$result->shouldHaveCount(2);
$result['en_US']->shouldHaveKey('slug');
$result['en_US']->shouldHaveKey('title');
$result['de_DE']->shouldHaveKey('slug');
$result['de_DE']->shouldHaveKey('title');
}

public function it_throws_exception_if_field_is_not_present_in_multiple_locales(
FormView $form,
FormView $enLocale,
FormView $deLocale,
FormView $slugForm
): void {
$form->children = [
'en_US' => $enLocale,
'de_DE' => $deLocale,
];

$enLocale->children = [
'slug' => $slugForm,
];

$deLocale->children = [
'slug' => $slugForm,
// 'title' is missing in de_DE
];

$this->shouldThrow(\InvalidArgumentException::class)->during('reduceTranslationForm', [$form, ['slug', 'title']]);
}

public function it_handles_empty_field_array(
FormView $form,
FormView $localeForm,
FormView $slugForm,
FormView $titleForm
): void {
$form->children = [
'en_US' => $localeForm,
];

$localeForm->children = [
'slug' => $slugForm,
'title' => $titleForm,
];

$result = $this->reduceTranslationForm($form, []);
$result->shouldHaveCount(0);
}
}
47 changes: 45 additions & 2 deletions src/Entity/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Sylius\CmsPlugin\Entity\Trait\ChannelsAwareTrait;
use Sylius\CmsPlugin\Entity\Trait\CollectibleTrait;
use Sylius\CmsPlugin\Entity\Trait\ContentElementsAwareTrait;
use Sylius\CmsPlugin\Entity\Trait\TeaserTrait;
use Sylius\Component\Resource\Model\TimestampableTrait;
use Sylius\Component\Resource\Model\ToggleableTrait;
use Sylius\Component\Resource\Model\TranslatableTrait;
Expand All @@ -20,7 +19,6 @@ class Page implements PageInterface
use TimestampableTrait;
use ChannelsAwareTrait;
use ContentElementsAwareTrait;
use TeaserTrait;
use TranslatableTrait {
__construct as protected initializeTranslationsCollection;
}
Expand Down Expand Up @@ -108,6 +106,51 @@ public function setMetaDescription(?string $metaDescription): void
$pageTranslationInterface->setMetaDescription($metaDescription);
}

public function getTeaserTitle(): ?string
{
/** @var PageTranslationInterface $pageTranslationInterface */
$pageTranslationInterface = $this->getPageTranslation();

return $pageTranslationInterface->getTeaserTitle();
}

public function setTeaserTitle(?string $teaserTitle): void
{
/** @var PageTranslationInterface $pageTranslationInterface */
$pageTranslationInterface = $this->getPageTranslation();
$pageTranslationInterface->setTeaserTitle($teaserTitle);
}

public function getTeaserContent(): ?string
{
/** @var PageTranslationInterface $pageTranslationInterface */
$pageTranslationInterface = $this->getPageTranslation();

return $pageTranslationInterface->getTeaserContent();
}

public function setTeaserContent(?string $teaserContent): void
{
/** @var PageTranslationInterface $pageTranslationInterface */
$pageTranslationInterface = $this->getPageTranslation();
$pageTranslationInterface->setTeaserContent($teaserContent);
}

public function getTeaserImage(): ?MediaInterface
{
/** @var PageTranslationInterface $pageTranslationInterface */
$pageTranslationInterface = $this->getPageTranslation();

return $pageTranslationInterface->getTeaserImage();
}

public function setTeaserImage(?MediaInterface $teaserImage): void
{
/** @var PageTranslationInterface $pageTranslationInterface */
$pageTranslationInterface = $this->getPageTranslation();
$pageTranslationInterface->setTeaserImage($teaserImage);
}

public function getName(): ?string
{
return $this->name;
Expand Down
3 changes: 3 additions & 0 deletions src/Entity/PageTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace Sylius\CmsPlugin\Entity;

use Sylius\CmsPlugin\Entity\Trait\TeaserTrait;
use Sylius\Component\Resource\Model\AbstractTranslation;

class PageTranslation extends AbstractTranslation implements PageTranslationInterface
{
use TeaserTrait;

protected ?int $id;

protected ?string $slug = null;
Expand Down
8 changes: 7 additions & 1 deletion src/Entity/PageTranslationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Model\TranslationInterface;

interface PageTranslationInterface extends ResourceInterface, TranslationInterface
interface PageTranslationInterface extends ResourceInterface, TranslationInterface, TeaserInterface
{
public function getSlug(): ?string;

Expand All @@ -24,4 +24,10 @@ public function setMetaDescription(?string $metaDescription): void;
public function getTitle(): ?string;

public function setTitle(?string $title): void;

public function getTeaserTitle(): ?string;

public function getTeaserContent(): ?string;

public function getTeaserImage(): ?MediaInterface;
}
17 changes: 8 additions & 9 deletions src/Fixture/Factory/PageFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@ private function createPage(
$page->setName($pageData['name']);
$page->setEnabled($pageData['enabled']);

/** @var MediaInterface|null $mediaImage */
$mediaImage = $this->mediaRepository->findOneBy(['code' => $pageData['teaser_image']]);
if ($mediaImage) {
$page->setTeaserImage($mediaImage);
}

$page->setTeaserTitle($pageData['teaser_title']);
$page->setTeaserContent($pageData['teaser_content']);

foreach ($pageData['translations'] as $localeCode => $translation) {
/** @var PageTranslationInterface $pageTranslation */
$pageTranslation = $this->pageTranslationFactory->createNew();
Expand All @@ -75,6 +66,14 @@ private function createPage(
$pageTranslation->setTitle($translation['meta_title']);
$pageTranslation->setMetaKeywords($translation['meta_keywords']);
$pageTranslation->setMetaDescription($translation['meta_description']);
$pageTranslation->setTeaserTitle($translation['teaser_title']);
$pageTranslation->setTeaserContent($translation['teaser_content']);

/** @var MediaInterface|null $mediaImage */
$mediaImage = $this->mediaRepository->findOneBy(['code' => $translation['teaser_image']]);
if ($mediaImage) {
$pageTranslation->setTeaserImage($mediaImage);
}

$page->addTranslation($pageTranslation);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Fixture/PageFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->scalarNode('name')->end()
->arrayNode('collections')->scalarPrototype()->end()->end()
->arrayNode('channels')->scalarPrototype()->end()->end()
->scalarNode('teaser_title')->defaultNull()->end()
->scalarNode('teaser_content')->defaultNull()->end()
->scalarNode('teaser_image')->defaultNull()->end()
->arrayNode('translations')
->prototype('array')
->children()
->scalarNode('slug')->defaultNull()->end()
->scalarNode('meta_title')->defaultNull()->end()
->scalarNode('meta_keywords')->defaultNull()->end()
->scalarNode('meta_description')->defaultNull()->end()
->scalarNode('teaser_title')->defaultNull()->end()
->scalarNode('teaser_content')->defaultNull()->end()
->scalarNode('teaser_image')->defaultNull()->end()
->end()
->end()
->end()
Expand Down
12 changes: 0 additions & 12 deletions src/Form/Type/PageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => false,
'mapped' => false,
])
->add('teaserTitle', TextType::class, [
'label' => 'sylius_cms.ui.teaser.title',
'required' => false,
])
->add('teaserContent', WysiwygType::class, [
'label' => 'sylius_cms.ui.teaser.content',
'required' => false,
])
->add('teaserImage', MediaImageAutocompleteChoiceType::class, [
'label' => 'sylius_cms.ui.teaser.image',
'required' => false,
])
;
}

Expand Down
14 changes: 14 additions & 0 deletions src/Form/Type/Translation/PageTranslationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Sylius\CmsPlugin\Form\Type\Translation;

use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\CmsPlugin\Form\Type\MediaImageAutocompleteChoiceType;
use Sylius\CmsPlugin\Form\Type\WysiwygType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
Expand All @@ -29,6 +31,18 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => 'sylius_cms.ui.meta_description',
'required' => false,
])
->add('teaserTitle', TextType::class, [
'label' => 'sylius_cms.ui.teaser.title',
'required' => false,
])
->add('teaserContent', WysiwygType::class, [
'label' => 'sylius_cms.ui.teaser.content',
'required' => false,
])
->add('teaserImage', MediaImageAutocompleteChoiceType::class, [
'label' => 'sylius_cms.ui.teaser.image',
'required' => false,
])
;
}

Expand Down
36 changes: 36 additions & 0 deletions src/Migrations/Version20240910113936.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Sylius\CmsPlugin\Migrations;

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

final class Version20240910113936 extends AbstractMigration
{
public function getDescription(): string
{
return 'This migration removes teaser fields from the Page entity and adds them to the PageTranslation entity';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE sylius_cms_page DROP FOREIGN KEY FK_2C2740B2F56F16CF');
$this->addSql('DROP INDEX IDX_2C2740B2F56F16CF ON sylius_cms_page');
$this->addSql('ALTER TABLE sylius_cms_page DROP teaser_image_id, DROP teaser_title, DROP teaser_content');
$this->addSql('ALTER TABLE sylius_cms_page_translation ADD teaser_image_id INT DEFAULT NULL, ADD teaser_title VARCHAR(255) DEFAULT NULL, ADD teaser_content LONGTEXT DEFAULT NULL');
$this->addSql('ALTER TABLE sylius_cms_page_translation ADD CONSTRAINT FK_6D0D401BF56F16CF FOREIGN KEY (teaser_image_id) REFERENCES sylius_cms_media (id) ON DELETE SET NULL');
$this->addSql('CREATE INDEX IDX_6D0D401BF56F16CF ON sylius_cms_page_translation (teaser_image_id)');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE sylius_cms_page ADD teaser_image_id INT DEFAULT NULL, ADD teaser_title VARCHAR(255) DEFAULT NULL, ADD teaser_content LONGTEXT DEFAULT NULL');
$this->addSql('ALTER TABLE sylius_cms_page ADD CONSTRAINT FK_2C2740B2F56F16CF FOREIGN KEY (teaser_image_id) REFERENCES sylius_cms_media (id) ON DELETE SET NULL');
$this->addSql('CREATE INDEX IDX_2C2740B2F56F16CF ON sylius_cms_page (teaser_image_id)');
$this->addSql('ALTER TABLE sylius_cms_page_translation DROP FOREIGN KEY FK_6D0D401BF56F16CF');
$this->addSql('DROP INDEX IDX_6D0D401BF56F16CF ON sylius_cms_page_translation');
$this->addSql('ALTER TABLE sylius_cms_page_translation DROP teaser_image_id, DROP teaser_title, DROP teaser_content');
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/doctrine/Media.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</join-table>
</many-to-many>

<one-to-many field="pages" target-entity="Sylius\CmsPlugin\Entity\PageInterface" mapped-by="teaserImage"/>
<one-to-many field="pages" target-entity="Sylius\CmsPlugin\Entity\PageTranslationInterface" mapped-by="teaserImage"/>

</mapped-superclass>
</doctrine-mapping>
8 changes: 0 additions & 8 deletions src/Resources/config/doctrine/Page.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,5 @@
</order-by>
</one-to-many>

<field name="teaserTitle" column="teaser_title" type="string" nullable="true"/>

<field name="teaserContent" column="teaser_content" type="text" nullable="true"/>

<many-to-one field="teaserImage" target-entity="Sylius\CmsPlugin\Entity\MediaInterface" inversed-by="pages">
<join-column name="teaser_image_id" referenced-column-name="id" nullable="true" on-delete="SET NULL"/>
</many-to-one>

</mapped-superclass>
</doctrine-mapping>
8 changes: 8 additions & 0 deletions src/Resources/config/doctrine/PageTranslation.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@
<field name="metaDescription" column="meta_description" type="string" length="5000" nullable="true"/>

<field name="title" column="title" type="string" nullable="true"/>

<field name="teaserTitle" column="teaser_title" type="string" nullable="true"/>

<field name="teaserContent" column="teaser_content" type="text" nullable="true"/>

<many-to-one field="teaserImage" target-entity="Sylius\CmsPlugin\Entity\MediaInterface" inversed-by="pages">
<join-column name="teaser_image_id" referenced-column-name="id" nullable="true" on-delete="SET NULL"/>
</many-to-one>
</mapped-superclass>
</doctrine-mapping>
Loading
Loading