Skip to content

Commit

Permalink
IBX-4031: Forced published non-translatable field to be shown in curr…
Browse files Browse the repository at this point in the history
…ent field (#71)

* IBX-4031: Forced published non-translatable field to be shown in current field

* IBX-4031: Fixed Behat test

* IBX-4031: Made `ContentUpdateMapperTest` final
  • Loading branch information
barw4 authored Aug 10, 2023
1 parent 46b2b94 commit 51c3d92
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/lib/Behat/Context/UserRegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ public function addUserRegistrationConfiguration(PyStringNode $extraConfiguratio
{
$extraConfigurationString = str_replace(
'<userGroupContentId>',
$this->customUserGroup->id,
$extraConfigurationString
(string)$this->customUserGroup->id,
$extraConfigurationString->getRaw()
);

$this->yamlConfigurationContext->addConfiguration(Yaml::parse($extraConfigurationString));
Expand Down
22 changes: 14 additions & 8 deletions src/lib/Content/View/Filter/ContentEditViewFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,26 @@ public function handleContentEditForm(FilterViewBuilderParametersEvent $event)

$request = $event->getRequest();
$languageCode = $request->attributes->get('language');
$contentId = $request->attributes->getInt('contentId');
$contentDraft = $this->contentService->loadContent(
$request->attributes->getInt('contentId'),
$contentId,
[$languageCode], // @todo: rename to languageCode in 3.0
$request->attributes->getInt('versionNo')
);
$currentContent = $this->contentService->loadContent($contentId);
$currentFields = $currentContent->getFields();

$contentType = $this->contentTypeService->loadContentType(
$contentDraft->contentInfo->contentTypeId,
$this->languagePreferenceProvider->getPreferredLanguages()
);

$contentUpdate = $this->resolveContentEditData($contentDraft, $languageCode, $contentType);
$contentUpdate = $this->resolveContentEditData(
$contentDraft,
$languageCode,
$contentType,
$currentFields,
);
$form = $this->resolveContentEditForm(
$contentUpdate,
$languageCode,
Expand All @@ -99,22 +107,20 @@ public function handleContentEditForm(FilterViewBuilderParametersEvent $event)
}

/**
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
* @param string $languageCode
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
*
* @return \EzSystems\EzPlatformContentForms\Data\Content\ContentUpdateData
* @param array<\eZ\Publish\API\Repository\Values\Content\Field> $currentFields
*/
private function resolveContentEditData(
Content $content,
string $languageCode,
ContentType $contentType
ContentType $contentType,
array $currentFields
): ContentUpdateData {
$contentUpdateMapper = new ContentUpdateMapper();

return $contentUpdateMapper->mapToFormData($content, [
'languageCode' => $languageCode,
'contentType' => $contentType,
'currentFields' => $currentFields,
]);
}

Expand Down
10 changes: 8 additions & 2 deletions src/lib/Data/Mapper/ContentUpdateMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,23 @@ public function mapToFormData(ValueObject $contentDraft, array $params = [])

$params = $optionsResolver->resolve($params);
$languageCode = $params['languageCode'];
$currentFields = $params['currentFields'];
$mappedCurrentFields = array_column($currentFields, null, 'fieldDefIdentifier');

$data = new ContentUpdateData(['contentDraft' => $contentDraft]);
$data->initialLanguageCode = $languageCode;

$fields = $contentDraft->getFieldsByLanguage($languageCode);

foreach ($params['contentType']->fieldDefinitions as $fieldDef) {
$isNonTranslatable = $fieldDef->isTranslatable === false;
$field = $fields[$fieldDef->identifier];
$data->addFieldData(new FieldData([
'fieldDefinition' => $fieldDef,
'field' => $field,
'value' => $field->value,
'value' => $isNonTranslatable
? $mappedCurrentFields[$fieldDef->identifier]->value
: $field->value,
]));
}

Expand All @@ -51,7 +57,7 @@ public function mapToFormData(ValueObject $contentDraft, array $params = [])
private function configureOptions(OptionsResolver $optionsResolver)
{
$optionsResolver
->setRequired(['languageCode', 'contentType'])
->setRequired(['languageCode', 'contentType', 'currentFields'])
->setAllowedTypes('contentType', ContentType::class);
}
}
108 changes: 108 additions & 0 deletions tests/lib/Data/Mapper/ContentUpdateMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\ContentForms\Data\Mapper;

use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Field as APIField;
use eZ\Publish\API\Repository\Values\Content\Language;
use eZ\Publish\API\Repository\Values\Content\Section;
use eZ\Publish\Core\Repository\Values\Content\Content;
use eZ\Publish\Core\Repository\Values\Content\Location as APILocation;
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
use eZ\Publish\Core\Repository\Values\ContentType\ContentType;
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition;
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinitionCollection;
use EzSystems\EzPlatformContentForms\Data\Mapper\ContentUpdateMapper;
use PHPUnit\Framework\TestCase;

final class ContentUpdateMapperTest extends TestCase
{
public function testMapToFormData(): void
{
$currentFields = [
new APIField([
'fieldDefIdentifier' => 'name',
'fieldTypeIdentifier' => 'ezstring',
'languageCode' => 'eng-GB',
'value' => 'Name',
]),
new APIField([
'fieldDefIdentifier' => 'short_name',
'fieldTypeIdentifier' => 'ezstring',
'languageCode' => 'eng-DE',
'value' => $expectedShortName = 'Nontranslateable short name',
]),
];
$content = new Content([
'versionInfo' => new VersionInfo([
'contentInfo' => new ContentInfo([
'remoteId' => 'foo',
'mainLanguage' => new Language([
'languageCode' => 'eng-GB',
]),
'alwaysAvailable' => true,
'sectionId' => 2,
'section' => new Section([
'id' => 2,
'identifier' => 'foo_section_identifier',
]),
'publishedDate' => new \DateTime('2020-10-30T11:40:09+00:00'),
'modificationDate' => new \DateTime('2020-10-30T11:40:09+00:00'),
'mainLocation' => new APILocation([
'parentLocationId' => 1,
'hidden' => true,
'sortField' => 9,
'sortOrder' => 0,
'priority' => 1,
]),
]),
]),
'contentType' => $contentType = new ContentType([
'identifier' => 'folder',
'fieldDefinitions' => new FieldDefinitionCollection([
new FieldDefinition([
'identifier' => 'name',
'isTranslatable' => true,
'defaultValue' => '',
]),
new FieldDefinition([
'identifier' => 'short_name',
'isTranslatable' => false,
'defaultValue' => '',
]),
]),
]),
'internalFields' => [
new APIField([
'fieldDefIdentifier' => 'name',
'fieldTypeIdentifier' => 'ezstring',
'languageCode' => 'ger-DE',
'value' => $expectedName = 'GER name',
]),
new APIField([
'fieldDefIdentifier' => 'short_name',
'fieldTypeIdentifier' => 'ezstring',
'languageCode' => 'ger-DE',
'value' => '',
]),
],
]);

$data = (new ContentUpdateMapper())->mapToFormData($content, [
'languageCode' => 'ger-DE',
'contentType' => $contentType,
'currentFields' => $currentFields,
]);

$fieldsData = $data->fieldsData;

self::assertSame($expectedName, $fieldsData['name']->value);
self::assertSame($expectedShortName, $fieldsData['short_name']->value);
}
}

0 comments on commit 51c3d92

Please sign in to comment.