Skip to content

Instantly share code, notes, and snippets.

@alongosz
Created April 12, 2018 12:40
Show Gist options
  • Save alongosz/2e059a9eb1ea9ef2d94b4f15c6843c70 to your computer and use it in GitHub Desktop.
Save alongosz/2e059a9eb1ea9ef2d94b4f15c6843c70 to your computer and use it in GitHub Desktop.
eZ Platform - update FieldDefinition Default Value
<?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