Installer/Field.php
public function addToContentType($fieldName, ContentType $contentType)
    {
        try {
            $contentTypeDraft = $this->contentTypeService->loadContentTypeDraft($contentType->id);
        } catch (NotFoundException $e) {
            $contentTypeDraft = $this->contentTypeService->createContentTypeDraft($contentType);
        }

        $typeUpdate = $this->contentTypeService->newContentTypeUpdateStruct();
        $typeUpdate->modificationDate = new DateTime();

        $knowLanguage = array_keys($contentType->getDescriptions());

        if (!in_array($contentType->mainLanguageCode, $knowLanguage)) {
            $knowLanguage[] = $contentType->mainLanguageCode;
        }

        $fieldCreateStruct = $this->contentTypeService->newFieldDefinitionCreateStruct(
            $fieldName,
            'novaseometas'
        );

        $fieldCreateStruct->names = array_fill_keys($knowLanguage, $this->metaFieldName);
        $fieldCreateStruct->descriptions = array_fill_keys($knowLanguage, $this->metaFieldDescription);
        $fieldCreateStruct->fieldGroup = $this->metaFieldGroup;
        $fieldCreateStruct->position = 100;
        $fieldCreateStruct->isTranslatable = true;
        $fieldCreateStruct->isRequired = false;
        $fieldCreateStruct->isSearchable = false;
        $fieldCreateStruct->isInfoCollector = false;

        try {
            $this->contentTypeService->updateContentTypeDraft($contentTypeDraft, $typeUpdate);

            if ($contentTypeDraft->getFieldDefinition($fieldName) == null) {
                $this->contentTypeService->addFieldDefinition($contentTypeDraft, $fieldCreateStruct);
            }

            $this->contentTypeService->publishContentTypeDraft($contentTypeDraft);

            return true;
        } catch (Exception $e) {
            $this->errorMessage = $e->getMessage();

            return false;
        }
    }
public function addToContentType($fieldName, ContentType $contentType)
    {
        try {
            $contentTypeDraft = $this->contentTypeService->loadContentTypeDraft($contentType->id);
        } catch (NotFoundException $e) {
            $contentTypeDraft = $this->contentTypeService->createContentTypeDraft($contentType);
        }

        $typeUpdate = $this->contentTypeService->newContentTypeUpdateStruct();
        $typeUpdate->modificationDate = new DateTime();

        $knowLanguage = array_keys($contentType->getDescriptions());

        if (!in_array($contentType->mainLanguageCode, $knowLanguage)) {
            $knowLanguage[] = $contentType->mainLanguageCode;
        }

        $fieldCreateStruct = $this->contentTypeService->newFieldDefinitionCreateStruct(
            $fieldName,
            'novaseometas'
        );

        $fieldCreateStruct->names = array_fill_keys($knowLanguage, $this->metaFieldName);
        $fieldCreateStruct->descriptions = array_fill_keys($knowLanguage, $this->metaFieldDescription);
        $fieldCreateStruct->fieldGroup = $this->metaFieldGroup;
        $fieldCreateStruct->position = 100;
        $fieldCreateStruct->isTranslatable = true;
        $fieldCreateStruct->isRequired = false;
        $fieldCreateStruct->isSearchable = false;
        $fieldCreateStruct->isInfoCollector = false;

        try {
            $this->contentTypeService->updateContentTypeDraft($contentTypeDraft, $typeUpdate);

            if ($contentTypeDraft->getFieldDefinition($fieldName) == null) {
                $this->contentTypeService->addFieldDefinition($contentTypeDraft, $fieldCreateStruct);
            }

            $this->contentTypeService->publishContentTypeDraft($contentTypeDraft);

            return true;
        } catch (Exception $e) {
            $this->errorMessage = $e->getMessage();

            return false;
        }
    }
public function addToContentType ( $ fieldName , ContentType $ contentType ) { try { $ contentTypeDraft = $ this -> contentTypeService -> loadContentTypeDraft ( $ contentType -> id ) ; } catch ( NotFoundException $ e ) { $ contentTypeDraft = $ this -> contentTypeService -> createContentTypeDraft ( $ contentType ) ; } $ typeUpdate = $ this -> contentTypeService -> newContentTypeUpdateStruct ( ) ; $ typeUpdate -> modificationDate = new DateTime ( ) ; $ knowLanguage = array_keys ( $ contentType -> getDescriptions ( ) ) ; if ( ! in_array ( $ contentType -> mainLanguageCode , $ knowLanguage ) ) { $ knowLanguage [ ] = $ contentType -> mainLanguageCode ; } $ fieldCreateStruct = $ this -> contentTypeService -> newFieldDefinitionCreateStruct ( $ fieldName , 'novaseometas' ) ; $ fieldCreateStruct -> names = array_fill_keys ( $ knowLanguage , $ this -> metaFieldName ) ; $ fieldCreateStruct -> descriptions = array_fill_keys ( $ knowLanguage , $ this -> metaFieldDescription ) ; $ fieldCreateStruct -> fieldGroup = $ this -> metaFieldGroup ; $ fieldCreateStruct -> position = 100 ; $ fieldCreateStruct -> isTranslatable = true ; $ fieldCreateStruct -> isRequired = false ; $ fieldCreateStruct -> isSearchable = false ; $ fieldCreateStruct -> isInfoCollector = false ; try { $ this -> contentTypeService -> updateContentTypeDraft ( $ contentTypeDraft , $ typeUpdate ) ; if ( $ contentTypeDraft -> getFieldDefinition ( $ fieldName ) == null ) { $ this -> contentTypeService -> addFieldDefinition ( $ contentTypeDraft , $ fieldCreateStruct ) ; } $ this -> contentTypeService -> publishContentTypeDraft ( $ contentTypeDraft ) ; return true ; } catch ( Exception $ e ) { $ this -> errorMessage = $ e -> getMessage ( ) ; return false ; } }
Adds `metas` field to existing ContentType.

@param string $fieldName
@param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType

@return bool
Adds metas field to existing ContentType .