eZ\Publish\Core\Persistence\Legacy\Content\FieldHandler::createNewFields PHP Method

createNewFields() public method

Creates new fields in the database from $content of $contentType.
public createNewFields ( eZ\Publish\SPI\Persistence\Content $content, eZ\Publish\SPI\Persistence\Content\Type $contentType )
$content eZ\Publish\SPI\Persistence\Content
$contentType eZ\Publish\SPI\Persistence\Content\Type
    public function createNewFields(Content $content, Type $contentType)
    {
        $fieldsToCopy = array();
        $languageCodes = array();
        $fields = $this->getFieldMap($content->fields, $languageCodes);
        $languageCodes[$content->versionInfo->contentInfo->mainLanguageCode] = true;
        foreach ($contentType->fieldDefinitions as $fieldDefinition) {
            foreach (array_keys($languageCodes) as $languageCode) {
                // Create fields passed from struct
                if (isset($fields[$fieldDefinition->id][$languageCode])) {
                    $field = $fields[$fieldDefinition->id][$languageCode];
                    $this->createNewField($field, $content);
                } elseif (!$fieldDefinition->isTranslatable && isset($fields[$fieldDefinition->id][$content->versionInfo->contentInfo->mainLanguageCode])) {
                    // Copy only for untranslatable field and when field in main language exists
                    // Only register here, process later as field copied should be already stored
                    $fieldsToCopy[$fieldDefinition->id][$languageCode] = $fields[$fieldDefinition->id][$content->versionInfo->contentInfo->mainLanguageCode];
                } else {
                    // In all other cases create empty field
                    $field = $this->getEmptyField($fieldDefinition, $languageCode);
                    $content->fields[] = $field;
                    $this->createNewField($field, $content);
                }
            }
        }
        $this->copyFields($fieldsToCopy, $content);
    }

Usage Example

Example #1
0
 /**
  * Copy Content with Fields and Versions from $contentId in $version.
  *
  * Copies all fields from $contentId in $versionNo (or all versions if null)
  * to a new object which is returned. Version numbers are maintained.
  *
  * @todo Should relations be copied? Which ones?
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If content or version is not found
  *
  * @param mixed $contentId
  * @param mixed|null $versionNo Copy all versions if left null
  *
  * @return \eZ\Publish\SPI\Persistence\Content
  */
 public function copy($contentId, $versionNo = null)
 {
     $currentVersionNo = isset($versionNo) ? $versionNo : $this->loadContentInfo($contentId)->currentVersionNo;
     // Copy content in given version or current version
     $createStruct = $this->mapper->createCreateStructFromContent($this->load($contentId, $currentVersionNo));
     $content = $this->internalCreate($createStruct, $currentVersionNo);
     $contentType = $this->contentTypeHandler->load($createStruct->typeId);
     // If version was not passed also copy other versions
     if (!isset($versionNo)) {
         foreach ($this->listVersions($contentId) as $versionInfo) {
             if ($versionInfo->versionNo === $currentVersionNo) {
                 continue;
             }
             $versionContent = $this->load($contentId, $versionInfo->versionNo);
             $versionContent->versionInfo->contentInfo->id = $content->versionInfo->contentInfo->id;
             $versionContent->versionInfo->modificationDate = $createStruct->modified;
             $versionContent->versionInfo->creationDate = $createStruct->modified;
             $versionContent->versionInfo->id = $this->contentGateway->insertVersion($versionContent->versionInfo, $versionContent->fields);
             $this->fieldHandler->createNewFields($versionContent, $contentType);
             // Create names
             foreach ($versionContent->versionInfo->names as $language => $name) {
                 $this->contentGateway->setName($content->versionInfo->contentInfo->id, $versionInfo->versionNo, $name, $language);
             }
         }
     }
     return $content;
 }