Permalink
Browse files

Merge pull request #164 from damianz5/content_create_multilang

Multilang support for content create/update
  • Loading branch information...
gggeek committed Sep 13, 2018
2 parents a2595a9 + 493663d commit 87316f8566bff8186a527646c8c280bde614968c
View
@@ -48,7 +48,7 @@ before_install:
#- sudo apt-get install -qq -y --force-yes apache2 libapache2-mod-fastcgi
# For php 5.6, Composer needs humongous amounts of ram - which we don't have. Enable swap as workaround
- if [ "$TRAVIS_PHP_VERSION" = "5.6" ]; then sudo fallocate -l 6G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile; fi
- if [ "$TRAVIS_PHP_VERSION" = "5.6" ]; then sudo fallocate -l 10G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile; fi
install:
# Latest version of composer breaks downloading eZ, so we can not run self-update. See https://github.com/composer/composer/issues/4582
@@ -595,33 +595,137 @@ public function generateMigration(array $matchCondition, $mode, array $context =
*/
protected function setFields($createOrUpdateStruct, array $fields, ContentType $contentType, $step)
{
$fields = $this->convertFields($fields);
if ($this->hasLanguageCodesAsKeys($fields)) {
$fieldsList = $this->parseMultiLangFields($fields);
} else {
$fieldsList = $this->parseSingleLangFields($fields, $this->getLanguageCode($step));
}
foreach ($fieldsList as list($fieldIdentifier, $fieldValue, $language)) {
if (!isset($contentType->fieldDefinitionsByIdentifier[$fieldIdentifier])) {
throw new \Exception("Field '$fieldIdentifier' is not present in content type '{$contentType->identifier}'");
}
$fieldDefinition = $contentType->fieldDefinitionsByIdentifier[$fieldIdentifier];
$fieldValue = $this->getFieldValue($fieldValue, $fieldDefinition, $contentType->identifier, $step->context);
$createOrUpdateStruct->setField($fieldIdentifier, $fieldValue, $language);
}
}
/**
* @param $fields
* @return array
*/
private function convertFields($fields)
{
$convertedFields = [];
$i = 0;
// the 'easy' yml: key = field name, value = value
// deprecated: the 'legacy' yml: key = numerical index, value = array ( field name => value )
foreach ($fields as $key => $field) {
if ($key === $i && is_array($field) && count($field) == 1) {
// each $field is one key value pair
// eg.: $field = array($fieldIdentifier => $fieldValue)
reset($field);
$fieldIdentifier = key($field);
$fieldValue = $field[$fieldIdentifier];
$convertedFields[$fieldIdentifier] = $fieldValue;
} else {
$fieldIdentifier = $key;
$fieldValue = $field;
$convertedFields[$key] = $field;
}
$i++;
}
if (!isset($contentType->fieldDefinitionsByIdentifier[$fieldIdentifier])) {
throw new \Exception("Field '$fieldIdentifier' is not present in content type '{$contentType->identifier}'");
return $convertedFields;
}
/**
* @param array $fields
* @param string $language
* @return array
*/
protected function parseSingleLangFields(array $fields, $language)
{
$fieldsList = [];
foreach ($fields as $fieldIdentifier => $fieldValue) {
$fieldsList[] = [$fieldIdentifier, $fieldValue, $language];
}
return $fieldsList;
}
/**
* @param array $fields
* @return array
* @throws \Exception
*/
protected function parseMultiLangFields(array$fields)
{
$fieldsList = [];
foreach ($fields as $key => $field) {
if (!is_array($field)) {
throw new \Exception("Field '$key' should use multilang syntax as other fields in this step");
}
$fieldDefinition = $contentType->fieldDefinitionsByIdentifier[$fieldIdentifier];
$fieldValue = $this->getFieldValue($fieldValue, $fieldDefinition, $contentType->identifier, $step->context);
foreach($field as $languageCode => $value) {
$fieldsList[] = [$key, $value, $languageCode];
}
}
$createOrUpdateStruct->setField($fieldIdentifier, $fieldValue, $this->getLanguageCode($step));
return $fieldsList;
}
$i++;
/**
* Checks whatever at least one of field is using multilang syntax.
*
* @param array $fields
* @return bool
*/
protected function hasLanguageCodesAsKeys(array $fields)
{
$languages = $this->getContentLanguages();
$languageCodes = array_combine($languages, $languages);
$hasLanguageCodesAsKeys = false;
foreach ($fields as $fieldIdentifier => $fieldData) {
if (!is_array($fieldData)) {
continue;
}
foreach ($fieldData as $key => $data) {
if (array_key_exists($key, $languageCodes)) {
$hasLanguageCodesAsKeys = true;
}
}
}
return $hasLanguageCodesAsKeys;
}
/**
* Returns all Languages.
*
* @return array
*/
protected function getContentLanguages()
{
static $languages;
return !empty($languages) ? $languages : $languages = array_map(
function($language) {
return $language->languageCode;
},
array_filter(
$this->repository->getContentLanguageService()->loadLanguages(),
function ($language) {
return $language->enabled;
}
)
);
}
protected function setSection(Content $content, $sectionKey)
@@ -0,0 +1,42 @@
# Fails by attempting creation of content in non-existing language.
# NB: needs a follow-up migration to remove the created content type
-
type: content_type
mode: create
content_type_group: 1
identifier: kmb_test_13
name: Kaliop Migration Bundle Test Class 13
name_pattern: '<ezstring>'
attributes:
-
type: ezstring
name: ezstring
identifier: ezstring
-
type: ezstring
name: ezstring 2
identifier: ezstring_2
-
type: language
mode: create
lang: abc-TA
name: Kaliop Migration Bundle Language abc-TA
-
type: language
mode: create
lang: abc-TB
name: Kaliop Migration Bundle Language abc-TB
-
type: content
mode: create
content_type: kmb_test_9
parent_location: 2
attributes:
ezstring:
abc-TA: hello world 4 abc-TA
abc-TB: hello world 4 abc-TB
ezstring_2: some non multilang value
@@ -0,0 +1,32 @@
# Fails by attempting creation of content in non-existing language.
# NB: needs a follow-up migration to remove the created content type
-
type: content_type
mode: create
content_type_group: 1
identifier: kmb_test_14
name: Kaliop Migration Bundle Test Class 14
name_pattern: '<ezstring>'
attributes:
-
type: ezstring
name: ezstring
identifier: ezstring
-
type: language
mode: create
lang: abd-TA
name: Kaliop Migration Bundle Language abd-TA
-
type: content
mode: create
content_type: kmb_test_9
parent_location: 2
attributes:
ezstring:
abc-TA: hello world 4 abc-TA
non-EXIST: hello world 4 non existing language
@@ -0,0 +1,44 @@
# Fails by attempting creation of content in non-existing language.
# NB: needs a follow-up migration to remove the created content type
-
type: content_type
mode: create
content_type_group: 1
identifier: kmb_test_15
name: Kaliop Migration Bundle Test Class 15
name_pattern: '<ezstring>'
attributes:
-
type: ezstring
name: ezstring
identifier: ezstring
-
type: ezstring
name: ezstring 2
identifier: ezstring_2
-
type: language
mode: create
lang: abe-TA
name: Kaliop Migration Bundle Language abe-TA
-
type: language
mode: create
lang: abe-TB
name: Kaliop Migration Bundle Language abe-TB
-
type: content
mode: create
content_type: kmb_test_9
parent_location: 2
attributes:
ezstring:
abe-TA: hello world 4 abe-TA
abe-TB: hello world 4 abe-TB
ezstring_2:
abe-TB: hello world 4 abe-TB
@@ -127,6 +127,69 @@
mode: delete
match:
content_id: reference:kmb_test_9_content
-
type: content
mode: create
content_type: reference:kmb_test_9
parent_location: 2
lang: abc-DE
attributes:
- ezstring:
eng-GB: hello world 1 eng-GB
abc-DE: hello world 1 abc-DE
references:
-
identifier: kmb_test_10_content
attribute: id
-
type: content
mode: update
match:
content_id: reference:kmb_test_10_content
lang: eng-GB
attributes:
- ezstring:
eng-GB: hello world 2 eng-GB
abc-DE: hello world 2 abc-DE
-
type: content
mode: delete
match:
content_id: reference:kmb_test_10_content
-
type: content
mode: create
content_type: reference:kmb_test_9
parent_location: 2
lang: abc-DE
attributes:
ezstring:
eng-GB: hello world 3 eng-GB
abc-DE: hello world 3 abc-DE
references:
-
identifier: kmb_test_11_content
attribute: id
-
type: content
mode: update
match:
content_id: reference:kmb_test_11_content
lang: eng-GB
attributes:
ezstring:
eng-GB: hello world 4 eng-GB
abc-DE: hello world 4 abc-DE
-
type: content
mode: delete
match:
content_id: reference:kmb_test_11_content
-
type: content_type
mode: delete

0 comments on commit 87316f8

Please sign in to comment.