Created
April 12, 2018 12:40
-
-
Save alongosz/2e059a9eb1ea9ef2d94b4f15c6843c70 to your computer and use it in GitHub Desktop.
eZ Platform - update FieldDefinition Default Value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use eZ\Publish\Core\Base\Exceptions\ContentTypeFieldDefinitionValidationException; | |
use eZ\Publish\Core\FieldType\Country\Value as CountryValue; | |
$countryContentType = $contentTypeService->loadContentTypeByIdentifier('country_t'); | |
$countryFieldDefinition = $countryContentType->getFieldDefinition('country'); | |
$countryFieldType = $fieldTypeService->getFieldType( | |
$countryFieldDefinition->fieldTypeIdentifier | |
); | |
$repository->beginTransaction(); | |
try { | |
$countryTypeDraft = $contentTypeService->createContentTypeDraft($countryContentType); | |
$fieldDefinitionUpdateStruct = $contentTypeService->newFieldDefinitionUpdateStruct(); | |
$fieldDefinitionUpdateStruct->fieldSettings['isMultiple'] = false; | |
$fieldDefinitionUpdateStruct->defaultValue = new CountryValue( | |
[ | |
'PL' => [ | |
'Name' => 'Poland', | |
'Alpha2' => 'PL', | |
'Alpha3' => 'POL', | |
'IDC' => 48, | |
], | |
'NO' => [ | |
'Name' => 'Norway', | |
'Alpha2' => 'NO', | |
'Alpha3' => 'NOR', | |
'IDC' => 47, | |
], | |
] | |
); | |
$contentTypeService->updateFieldDefinition( | |
$countryTypeDraft, | |
$countryFieldDefinition, | |
$fieldDefinitionUpdateStruct | |
); | |
// Validate Default value | |
$countryTypeDraft = $contentTypeService->loadContentTypeDraft( | |
$countryTypeDraft->id | |
); | |
$countryFieldDefinition = $countryTypeDraft->getFieldDefinition('country'); | |
$errors = $countryFieldType->validateValue( | |
$countryFieldDefinition, | |
$countryFieldDefinition->defaultValue | |
); | |
if (!empty($errors)) { | |
throw new ContentTypeFieldDefinitionValidationException($errors); | |
} | |
$contentTypeService->publishContentTypeDraft($countryTypeDraft); | |
$repository->commit(); | |
} catch (\Exception $ex) { | |
$repository->rollback(); | |
throw $ex; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment