Skip to content
Permalink
Browse files

EZP-29757: As an administrator, I want to be able to translate a cont…

…ent type (AdminUI frontend) (#263)

* EZP-29757: As an administrator, I want to be able to translate a content type (AdminUI frontend)

* EZP-29757: unit tests fixed

* Update lib/Data/ContentTypeData.php

Co-Authored-By: ViniTou <vinniczek@gmail.com>

* EZP-29757: Code review fix

* EZP-29757: trailing whitespaces

* EZP-29757: Change how fields are disabled when translating content type

* EZP-29757: cs fixes
  • Loading branch information...
ViniTou authored and lserwatka committed Dec 14, 2018
1 parent c803137 commit b9d291ea5602872de9c9370b97c35c5766da2678
Showing with 324 additions and 78 deletions.
  1. +4 −4 bundle/Resources/config/validation.yml
  2. +4 −3 lib/Content/View/Builder/ContentEditViewBuilder.php
  3. +19 −0 lib/Data/ContentTypeData.php
  4. +43 −2 lib/Data/Mapper/ContentTypeDraftMapper.php
  5. +5 −2 lib/FieldType/Mapper/AuthorFormMapper.php
  6. +5 −2 lib/FieldType/Mapper/BinaryFileFormMapper.php
  7. +5 −2 lib/FieldType/Mapper/CheckboxFormMapper.php
  8. +6 −2 lib/FieldType/Mapper/CountryFormMapper.php
  9. +5 −2 lib/FieldType/Mapper/DateFormMapper.php
  10. +7 −2 lib/FieldType/Mapper/DateTimeFormMapper.php
  11. +7 −2 lib/FieldType/Mapper/FloatFormMapper.php
  12. +6 −2 lib/FieldType/Mapper/FormTypeBasedFieldValueFormMapper.php
  13. +6 −2 lib/FieldType/Mapper/ISBNFormMapper.php
  14. +3 −2 lib/FieldType/Mapper/ImageAssetFormMapper.php
  15. +5 −2 lib/FieldType/Mapper/ImageFormMapper.php
  16. +6 −2 lib/FieldType/Mapper/IntegerFormMapper.php
  17. +3 −2 lib/FieldType/Mapper/KeywordFormMapper.php
  18. +3 −2 lib/FieldType/Mapper/MapLocationFormMapper.php
  19. +6 −2 lib/FieldType/Mapper/MediaFormMapper.php
  20. +2 −0 lib/FieldType/Mapper/PageFormMapper.php
  21. +5 −2 lib/FieldType/Mapper/RelationFormMapper.php
  22. +7 −2 lib/FieldType/Mapper/RelationListFormMapper.php
  23. +3 −2 lib/FieldType/Mapper/RichTextFormMapper.php
  24. +6 −2 lib/FieldType/Mapper/SelectionFormMapper.php
  25. +5 −2 lib/FieldType/Mapper/TextBlockFormMapper.php
  26. +7 −2 lib/FieldType/Mapper/TextLineFormMapper.php
  27. +6 −2 lib/FieldType/Mapper/TimeFormMapper.php
  28. +3 −2 lib/FieldType/Mapper/UrlFormMapper.php
  29. +2 −1 lib/FieldType/Mapper/UserAccountFieldValueFormMapper.php
  30. +9 −0 lib/Form/Processor/ContentTypeFormProcessor.php
  31. +41 −9 lib/Form/Type/ContentType/ContentTypeUpdateType.php
  32. +42 −9 lib/Form/Type/FieldDefinition/FieldDefinitionType.php
  33. +1 −0 tests/RepositoryForms/Data/Mapper/ContentTypeDraftMapperTest.php
  34. +0 −4 tests/RepositoryForms/FieldType/Mapper/BaseMapperTest.php
  35. +12 −0 tests/RepositoryForms/FieldType/Mapper/FormTypeBasedFieldValueFormMapperTest.php
  36. +12 −0 tests/RepositoryForms/FieldType/Mapper/UserAccountFieldValueFormMapperTest.php
  37. +13 −0 tests/RepositoryForms/Form/Processor/ContentTypeFormProcessorTest.php
@@ -25,11 +25,11 @@ EzSystems\RepositoryForms\Data\ContentTypeData:
choices: [0, 1]
names:
- Expression:
expression: "value and value[this.mainLanguageCode] matches '/^.{1,255}$/'"
expression: "value and value[this.languageCode] matches '/^.{1,255}$/'"
message: "ez.content_type.names"
descriptions:
- Expression:
expression: "!value or value[this.mainLanguageCode] matches '/^.{0,255}$/'"
expression: "!value or value[this.languageCode] matches '/^.{0,255}$/'"
message: "ez.content_type.descriptions"
fieldDefinitionsData:
- Valid: ~
@@ -50,11 +50,11 @@ EzSystems\RepositoryForms\Data\FieldDefinitionData:
message: "ez.content_type.identifier.pattern"
names:
- Expression:
expression: "value and value[this.contentTypeData.mainLanguageCode] matches '/^.{1,255}$/'"
expression: "value and value[this.contentTypeData.languageCode] matches '/^.{1,255}$/'"
message: "ez.field_definition.names"
descriptions:
- Expression:
expression: "!value or value[this.contentTypeData.mainLanguageCode] matches '/^.{0,255}$/'"
expression: "!value or value[this.contentTypeData.languageCode] matches '/^.{0,255}$/'"
message: "ez.field_definition.descriptions"
position:
- Type:
@@ -82,7 +82,7 @@ public function buildView(array $parameters)
$location = $this->resolveLocation($parameters);
$content = $this->resolveContent($parameters, $location, $language);
$contentInfo = $content->contentInfo;
$contentType = $this->loadContentType((int) $contentInfo->contentTypeId);
$contentType = $this->loadContentType((int) $contentInfo->contentTypeId, $language->languageCode);
$form = $parameters['form'];
$isPublished = null !== $contentInfo->mainLocationId && $contentInfo->published;
@@ -178,14 +178,15 @@ private function loadLanguage(string $languageCode): Language
* Loads ContentType with id $contentTypeId.
*
* @param int $contentTypeId
* @param string $languageCode
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentType
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
private function loadContentType(int $contentTypeId): ContentType
private function loadContentType(int $contentTypeId, string $languageCode): ContentType
{
return $this->repository->getContentTypeService()->loadContentType($contentTypeId);
return $this->repository->getContentTypeService()->loadContentType($contentTypeId, [$languageCode]);
}
/**
@@ -33,6 +33,13 @@ class ContentTypeData extends ContentTypeUpdateStruct implements NewnessCheckabl
*/
protected $fieldDefinitionsData = [];
/**
* Language Code of currently edited contentTypeDraft.
*
* @var string|null
*/
public $languageCode = null;
protected function getIdentifierValue()
{
return $this->contentTypeDraft->identifier;
@@ -43,6 +50,18 @@ public function addFieldDefinitionData(FieldDefinitionData $fieldDefinitionData)
$this->fieldDefinitionsData[] = $fieldDefinitionData;
}
public function replaceFieldDefinitionData(string $fieldDefinitionIdentifier, FieldDefinitionData $fieldDefinitionData)
{
$currentFieldDefinition = array_filter(
$this->fieldDefinitionsData,
function (FieldDefinitionData $fieldDefinitionData) use ($fieldDefinitionIdentifier) {
return $fieldDefinitionIdentifier === $fieldDefinitionData->identifier;
}
);
$this->fieldDefinitionsData[key($currentFieldDefinition)] = $fieldDefinitionData;
}
/**
* Sort $this->fieldDefinitionsData first by position, then by identifier.
*/
@@ -8,9 +8,11 @@
*/
namespace EzSystems\RepositoryForms\Data\Mapper;
use eZ\Publish\API\Repository\Values\Content\Language;
use eZ\Publish\API\Repository\Values\ValueObject;
use EzSystems\RepositoryForms\Data\ContentTypeData;
use EzSystems\RepositoryForms\Data\FieldDefinitionData;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ContentTypeDraftMapper implements FormDataMapperInterface
{
@@ -24,6 +26,16 @@ class ContentTypeDraftMapper implements FormDataMapperInterface
*/
public function mapToFormData(ValueObject $contentTypeDraft, array $params = [])
{
$optionsResolver = new OptionsResolver();
$this->configureOptions($optionsResolver);
$params = $optionsResolver->resolve($params);
/** @var \eZ\Publish\API\Repository\Values\Content\Language $language */
$language = $params['language'] ?? null;
/** @var \eZ\Publish\API\Repository\Values\Content\Language|null $baseLanguage */
$baseLanguage = $params['baseLanguage'] ?? null;
$contentTypeData = new ContentTypeData(['contentTypeDraft' => $contentTypeDraft]);
if (!$contentTypeData->isNew()) {
$contentTypeData->identifier = $contentTypeDraft->identifier;
@@ -40,13 +52,27 @@ public function mapToFormData(ValueObject $contentTypeDraft, array $params = [])
$contentTypeData->names = $contentTypeDraft->getNames();
$contentTypeData->descriptions = $contentTypeDraft->getDescriptions();
$contentTypeData->languageCode = $language ? $language->languageCode : $contentTypeDraft->mainLanguageCode;
if ($baseLanguage && $language) {
$contentTypeData->names[$language->languageCode] = $contentTypeDraft->getName($baseLanguage->languageCode);
$contentTypeData->descriptions[$language->languageCode] = $contentTypeDraft->getDescription($baseLanguage->languageCode);
}
foreach ($contentTypeDraft->fieldDefinitions as $fieldDef) {
$names = $fieldDef->getNames();
$descriptions = $fieldDef->getDescriptions();
if ($baseLanguage && $language) {
$names[$language->languageCode] = $fieldDef->getName($baseLanguage->languageCode);
$descriptions[$language->languageCode] = $fieldDef->getDescription($baseLanguage->languageCode);
}
$contentTypeData->addFieldDefinitionData(new FieldDefinitionData([
'fieldDefinition' => $fieldDef,
'contentTypeData' => $contentTypeData,
'identifier' => $fieldDef->identifier,
'names' => $fieldDef->getNames(),
'descriptions' => $fieldDef->getDescriptions(),
'names' => $names,
'descriptions' => $descriptions,
'fieldGroup' => $fieldDef->fieldGroup,
'position' => $fieldDef->position,
'isTranslatable' => $fieldDef->isTranslatable,
@@ -62,4 +88,19 @@ public function mapToFormData(ValueObject $contentTypeDraft, array $params = [])
return $contentTypeData;
}
/**
* @param \Symfony\Component\OptionsResolver\OptionsResolver $optionsResolver
*
* @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
* @throws \Symfony\Component\OptionsResolver\Exception\AccessException
*/
private function configureOptions(OptionsResolver $optionsResolver)
{
$optionsResolver
->setDefined(['language'])
->setDefined(['baseLanguage'])
->setAllowedTypes('baseLanguage', ['null', Language::class])
->setAllowedTypes('language', Language::class);
}
}
@@ -29,6 +29,7 @@ class AuthorFormMapper implements FieldDefinitionFormMapperInterface, FieldValue
*/
public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data)
{
$isTranslation = $data->contentTypeData->languageCode !== $data->contentTypeData->mainLanguageCode;
$fieldDefinitionForm
->add(
'defaultAuthor',
@@ -44,6 +45,7 @@ public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, Field
'property_path' => 'fieldSettings[defaultAuthor]',
'label' => 'field_definition.ezauthor.default_author',
'translation_domain' => 'ezrepoforms_content_type',
'disabled' => $isTranslation,
]
);
}
@@ -58,15 +60,16 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
$fieldSettings = $fieldDefinition->getFieldSettings();
$formConfig = $fieldForm->getConfig();
$names = $fieldDefinition->getNames();
$label = $fieldDefinition->getName($formConfig->getOption('mainLanguageCode')) ?: reset($names);
$label = $fieldDefinition->getName($formConfig->getOption('languageCode'))
?: $fieldDefinition->getName($formConfig->getOption('mainLanguageCode'));
$fieldForm
->add(
$formConfig->getFormFactory()->createBuilder()
->create('value', AuthorFieldType::class, [
'default_author' => $fieldSettings['defaultAuthor'],
'required' => $fieldDefinition->isRequired,
'label' => $label,
'label' => $label ?? reset($names),
])
->setAutoInitialize(false)
->getForm()
@@ -35,6 +35,7 @@ public function __construct(FieldTypeService $fieldTypeService, MaxUploadSize $m
public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data)
{
$isTranslation = $data->contentTypeData->languageCode !== $data->contentTypeData->mainLanguageCode;
$fieldDefinitionForm
->add('maxSize', IntegerType::class, [
'required' => false,
@@ -51,6 +52,7 @@ public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, Field
'min' => 0,
'max' => $this->maxUploadSize->get(MaxUploadSize::MEGABYTES),
],
'disabled' => $isTranslation,
]);
}
@@ -60,7 +62,8 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
$formConfig = $fieldForm->getConfig();
$fieldType = $this->fieldTypeService->getFieldType($fieldDefinition->fieldTypeIdentifier);
$names = $fieldDefinition->getNames();
$label = $fieldDefinition->getName($formConfig->getOption('mainLanguageCode')) ?: reset($names);
$label = $fieldDefinition->getName($formConfig->getOption('languageCode'))
?: $fieldDefinition->getName($formConfig->getOption('mainLanguageCode'));
$fieldForm
->add(
@@ -70,7 +73,7 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
BinaryFileFieldType::class,
[
'required' => $fieldDefinition->isRequired,
'label' => $label,
'label' => $label ?? reset($names),
]
)
->addModelTransformer(new BinaryFileValueTransformer($fieldType, $data->value, Value::class))
@@ -23,13 +23,15 @@ class CheckboxFormMapper implements FieldDefinitionFormMapperInterface, FieldVal
{
public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $fieldDefinition)
{
$isTranslation = $fieldDefinition->contentTypeData->languageCode !== $fieldDefinition->contentTypeData->mainLanguageCode;
$defaultValueForm = $fieldDefinitionForm
->getConfig()
->getFormFactory()
->createBuilder()
->create('defaultValue', CheckboxFieldType::class, [
'required' => false,
'label' => /** @Desc("Default value") */'field_definition.ezboolean.default_value',
'disabled' => $isTranslation,
])
->setAutoInitialize(false)
->getForm();
@@ -42,7 +44,8 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
$fieldDefinition = $data->fieldDefinition;
$formConfig = $fieldForm->getConfig();
$names = $fieldDefinition->getNames();
$label = $fieldDefinition->getName($formConfig->getOption('mainLanguageCode')) ?: reset($names);
$label = $fieldDefinition->getName($formConfig->getOption('languageCode'))
?: $fieldDefinition->getName($formConfig->getOption('mainLanguageCode'));
$fieldForm
->add(
@@ -52,7 +55,7 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
CheckboxFieldType::class,
[
'required' => $fieldDefinition->isRequired,
'label' => $label,
'label' => $label ?? reset($names),
]
)
->setAutoInitialize(false)
@@ -21,13 +21,15 @@ class CountryFormMapper implements FieldDefinitionFormMapperInterface, FieldValu
{
public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data)
{
$isTranslation = $data->contentTypeData->languageCode !== $data->contentTypeData->mainLanguageCode;
$fieldDefinitionForm
->add(
'isMultiple',
CheckboxType::class, [
'required' => false,
'property_path' => 'fieldSettings[isMultiple]',
'label' => 'field_definition.ezcountry.is_multiple',
'disabled' => $isTranslation,
]
)
->add(
@@ -41,6 +43,7 @@ public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, Field
'expanded' => false,
'required' => false,
'label' => 'field_definition.ezcountry.default_value',
'disabled' => $isTranslation,
]
)
// Deactivate auto-initialize as we're not on the root form.
@@ -54,15 +57,16 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
$fieldSettings = $fieldDefinition->getFieldSettings();
$formConfig = $fieldForm->getConfig();
$names = $fieldDefinition->getNames();
$label = $fieldDefinition->getName($formConfig->getOption('mainLanguageCode')) ?: reset($names);
$label = $fieldDefinition->getName($formConfig->getOption('languageCode'))
?: $fieldDefinition->getName($formConfig->getOption('mainLanguageCode'));
$fieldForm
->add(
$formConfig->getFormFactory()->createBuilder()
->create('value', CountryFieldType::class, [
'multiple' => $fieldSettings['isMultiple'],
'required' => $fieldDefinition->isRequired,
'label' => $label,
'label' => $label ?? reset($names),
])
->setAutoInitialize(false)
->getForm()
@@ -25,6 +25,7 @@ class DateFormMapper implements FieldDefinitionFormMapperInterface, FieldValueFo
{
public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data)
{
$isTranslation = $data->contentTypeData->languageCode !== $data->contentTypeData->mainLanguageCode;
$fieldDefinitionForm
->add(
'defaultType',
@@ -40,6 +41,7 @@ public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, Field
'property_path' => 'fieldSettings[defaultType]',
'label' => 'field_definition.ezdate.default_type',
'translation_domain' => 'ezrepoforms_content_type',
'disabled' => $isTranslation,
]
);
}
@@ -49,7 +51,8 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
$fieldDefinition = $data->fieldDefinition;
$formConfig = $fieldForm->getConfig();
$names = $fieldDefinition->getNames();
$label = $fieldDefinition->getName($formConfig->getOption('mainLanguageCode')) ?: reset($names);
$label = $fieldDefinition->getName($formConfig->getOption('languageCode'))
?: $fieldDefinition->getName($formConfig->getOption('mainLanguageCode'));
$fieldForm
->add(
@@ -59,7 +62,7 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
DateFieldType::class,
[
'required' => $fieldDefinition->isRequired,
'label' => $label,
'label' => $label ?? reset($names),
]
)
->setAutoInitialize(false)
@@ -27,11 +27,13 @@ class DateTimeFormMapper implements FieldDefinitionFormMapperInterface, FieldVal
{
public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, FieldDefinitionData $data)
{
$isTranslation = $data->contentTypeData->languageCode !== $data->contentTypeData->mainLanguageCode;
$fieldDefinitionForm
->add('useSeconds', CheckboxType::class, [
'required' => false,
'property_path' => 'fieldSettings[useSeconds]',
'label' => 'field_definition.ezdatetime.use_seconds',
'disabled' => $isTranslation,
])
->add('defaultType', ChoiceType::class, [
'choices' => [
@@ -44,11 +46,13 @@ public function mapFieldDefinitionForm(FormInterface $fieldDefinitionForm, Field
'required' => true,
'property_path' => 'fieldSettings[defaultType]',
'label' => 'field_definition.ezdatetime.default_type',
'disabled' => $isTranslation,
])
->add('dateInterval', DateTimeIntervalType::class, [
'required' => false,
'property_path' => 'fieldSettings[dateInterval]',
'label' => 'field_definition.ezdatetime.date_interval',
'disabled' => $isTranslation,
]);
}
@@ -58,7 +62,8 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
$fieldSettings = $fieldDefinition->getFieldSettings();
$formConfig = $fieldForm->getConfig();
$names = $fieldDefinition->getNames();
$label = $fieldDefinition->getName($formConfig->getOption('mainLanguageCode')) ?: reset($names);
$label = $fieldDefinition->getName($formConfig->getOption('languageCode'))
?: $fieldDefinition->getName($formConfig->getOption('mainLanguageCode'));
$fieldForm
->add(
@@ -69,7 +74,7 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
[
'with_seconds' => $fieldSettings['useSeconds'],
'required' => $fieldDefinition->isRequired,
'label' => $label,
'label' => $label ?? reset($names),
]
)
->setAutoInitialize(false)
Oops, something went wrong.

0 comments on commit b9d291e

Please sign in to comment.
You can’t perform that action at this time.