diff --git a/core/Controller/Admin/Crud/CrudController.php b/core/Controller/Admin/Crud/CrudController.php index dd9d7d7..6e2a5bc 100644 --- a/core/Controller/Admin/Crud/CrudController.php +++ b/core/Controller/Admin/Crud/CrudController.php @@ -46,6 +46,9 @@ abstract class CrudController extends AdminController protected function doNew(EntityInterface $entity, EntityManager $entityManager, Request $request, callable $beforeCreate = null): Response { $configuration = $this->getConfiguration(); + + $this->prepareEntity($entity); + $form = $this->createForm($configuration->getForm('new'), $entity); if ($request->isMethod('POST')) { @@ -86,6 +89,9 @@ abstract class CrudController extends AdminController protected function doEdit(EntityInterface $entity, EntityManager $entityManager, Request $request, callable $beforeUpdate = null): Response { $configuration = $this->getConfiguration(); + + $this->prepareEntity($entity); + $form = $this->createForm($configuration->getForm('edit'), $entity); if ($request->isMethod('POST')) { @@ -181,4 +187,15 @@ abstract class CrudController extends AdminController $session->set($form->getName(), $filters); } } + + protected function prepareEntity(EntityInterface $entity) + { + $configuration = $this->getConfiguration(); + + if ($configuration->isI18n()) { + foreach ($configuration->getLocales() as $locale) { + $entity->addTranslation($entity->translate($locale, false)); + } + } + } } diff --git a/core/Crud/CrudConfiguration.php b/core/Crud/CrudConfiguration.php index 51f3ab5..4af2e4b 100644 --- a/core/Crud/CrudConfiguration.php +++ b/core/Crud/CrudConfiguration.php @@ -19,6 +19,8 @@ class CrudConfiguration protected array $viewDatas = []; protected array $fields = []; protected array $maxPerPage = []; + protected array $locales = []; + protected ?string $defaultLocale = null; protected static $self; @@ -198,4 +200,29 @@ class CrudConfiguration { return $this->maxPerPage[$page] ?? $default; } + + /* -- */ + + public function setI18n(array $locales, string $defaultLocale): self + { + $this->locales = $locales; + $this->defaultLocale = $defaultLocale; + + return $this; + } + + public function getLocales(): array + { + return $this->locales; + } + + public function getDefaultLocale(): ?string + { + return $this->defaultLocale; + } + + public function isI18n(): bool + { + return !empty($this->locales); + } } diff --git a/core/Crud/Field/Field.php b/core/Crud/Field/Field.php index aaac0eb..16513e9 100644 --- a/core/Crud/Field/Field.php +++ b/core/Crud/Field/Field.php @@ -2,8 +2,8 @@ namespace App\Core\Crud\Field; -use App\Core\Crud\Exception\CrudConfigurationException; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; use Symfony\Component\PropertyAccess\PropertyAccess; use Twig\Environment; @@ -14,11 +14,11 @@ use Twig\Environment; */ abstract class Field { - public function buildView(Environment $twig, $entity, array $options) + public function buildView(Environment $twig, $entity, array $options, ?string $locale = null) { return $twig->render($this->getView($options), [ 'entity' => $entity, - 'value' => $this->getValue($entity, $options), + 'value' => $this->getValue($entity, $options, $locale), 'options' => $options, ]); } @@ -43,12 +43,20 @@ abstract class Field return $resolver; } - protected function getValue($entity, array $options) + protected function getValue($entity, array $options, ?string $locale = null) { if (null !== $options['property']) { $propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()->getPropertyAccessor(); - $value = $propertyAccessor->getValue($entity, $options['property']); + try { + $value = $propertyAccessor->getValue($entity, $options['property']); + } catch (NoSuchPropertyException $e) { + if (null !== $locale) { + $value = $propertyAccessor->getValue($entity->translate($locale), $options['property']); + } else { + throw $e; + } + } } elseif (null !== $options['property_builder']) { $value = call_user_func($options['property_builder'], $entity, $options); } else { diff --git a/core/Crud/Field/ImageField.php b/core/Crud/Field/ImageField.php new file mode 100644 index 0000000..bbecab8 --- /dev/null +++ b/core/Crud/Field/ImageField.php @@ -0,0 +1,27 @@ + + */ +class ImageField extends Field +{ + public function configureOptions(OptionsResolver $resolver): OptionsResolver + { + parent::configureOptions($resolver); + + $resolver->setDefaults([ + 'view' => '@Core/admin/crud/field/image.html.twig', + 'image_attr' => [], + ]); + + $resolver->setAllowedTypes('image_attr', ['array']); + + return $resolver; + } +} diff --git a/core/Manager/TranslatableEntityManager.php b/core/Manager/TranslatableEntityManager.php new file mode 100644 index 0000000..2cb87a0 --- /dev/null +++ b/core/Manager/TranslatableEntityManager.php @@ -0,0 +1,20 @@ + + */ +class TranslatableEntityManager extends EntityManager +{ + protected function persist(EntityInterface $entity) + { + $this->entityManager->persist($entity); + $entity->mergeNewTranslations(); + $this->flush(); + } +} diff --git a/core/Resources/views/admin/crud/_form.html.twig b/core/Resources/views/admin/crud/_form.html.twig index 796d8a5..4733513 100644 --- a/core/Resources/views/admin/crud/_form.html.twig +++ b/core/Resources/views/admin/crud/_form.html.twig @@ -1,6 +1,12 @@