eZ\Bundle\EzPublishCoreBundle\Features\Context\ContentTypeContext::createContentType PHP Method

createContentType() public method

Creates a content type with $identifier on content type group with identifier $groupIdentifier and with the given 'fields' definitions.
public createContentType ( string $groupIdentifier, string $identifier, array $fields ) : eZ\Publish\API\Repository\Values\ContentType\ContentType
$groupIdentifier string content type group identifier
$identifier string content type identifier
$fields array content type fields definitions
return eZ\Publish\API\Repository\Values\ContentType\ContentType
    public function createContentType($groupIdentifier, $identifier, $fields)
    {
        $contentTypeService = $this->contentTypeService;
        $contentTypeGroup = $contentTypeService->loadContentTypeGroupByIdentifier($groupIdentifier);
        // convert 'some_type' to 'Some Type';
        $contentTypeName = ucwords(str_replace('_', ' ', $identifier));
        $contentTypeCreateStruct = $contentTypeService->newContentTypeCreateStruct($identifier);
        $contentTypeCreateStruct->mainLanguageCode = self::DEFAULT_LANGUAGE;
        $contentTypeCreateStruct->names = array(self::DEFAULT_LANGUAGE => $contentTypeName);
        $fieldPosition = 0;
        foreach ($fields as $field) {
            $field = array_change_key_case($field, CASE_LOWER);
            $fieldPosition += 10;
            $fieldCreateStruct = $contentTypeService->newFieldDefinitionCreateStruct($field['identifier'], $field['type']);
            $fieldCreateStruct->names = array(self::DEFAULT_LANGUAGE => $field['name']);
            $fieldCreateStruct->position = $fieldPosition;
            if (isset($field['required'])) {
                $fieldCreateStruct->isRequired = $field['required'] === 'true';
            }
            if (isset($field['validator']) && $field['validator'] !== 'false') {
                $fieldCreateStruct->validatorConfiguration = $this->processValidator($field['validator']);
            }
            if (isset($field['settings']) && $field['settings'] !== 'false') {
                $fieldCreateStruct->fieldSettings = $this->processSettings($field['settings']);
            }
            $contentTypeCreateStruct->addFieldDefinition($fieldCreateStruct);
        }
        $contentTypeDraft = $contentTypeService->createContentType($contentTypeCreateStruct, array($contentTypeGroup));
        $contentTypeService->publishContentTypeDraft($contentTypeDraft);
        $contentType = $contentTypeService->loadContentTypeByIdentifier($identifier);
        return $contentType;
    }