/** * Returns field relations data for the current version of the given $contentInfo. * * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo * * @return mixed */ public function getFieldRelations(ContentInfo $contentInfo) { $relations = array(); $locationIdToContentIdMapping = array(); $content = $this->repository->getContentService()->loadContentByContentInfo($contentInfo); foreach ($content->getFields() as $field) { $fieldDefinition = $content->contentType->getFieldDefinition($field->fieldDefIdentifier); $fieldType = $this->repository->getFieldTypeService()->buildFieldType($fieldDefinition->fieldTypeIdentifier); $this->appendFieldRelations($relations, $locationIdToContentIdMapping, $fieldType, $fieldType->acceptValue($field->value), $fieldDefinition->id); } return $relations; }
/** * Get FieldTypeService * * @return \eZ\Publish\API\Repository\FieldTypeService */ public function getFieldTypeService() { if ($this->fieldTypeService !== null) { return $this->fieldTypeService; } $this->fieldTypeService = new FieldTypeService($this->repository->getFieldTypeService(), $this->signalDispatcher); return $this->fieldTypeService; }
/** * Fetches the list of available Field identifiers in the token and returns * an array of their current title value. * * @see \eZ\Publish\Core\Repository\FieldType::getName() * * @param string[] $schemaIdentifiers * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType $fieldDefinitions * @param array $fieldMap * @param string $languageCode * * @return string[] Key is the field identifier, value is the title value */ protected function getFieldTitles(array $schemaIdentifiers, ContentType $contentType, array $fieldMap, $languageCode) { $fieldTitles = array(); foreach ($schemaIdentifiers as $fieldDefinitionIdentifier) { if (isset($fieldMap[$fieldDefinitionIdentifier][$languageCode])) { $fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier); $fieldType = $this->repository->getFieldTypeService()->getFieldType($fieldDefinition->fieldTypeIdentifier); $fieldTitles[$fieldDefinitionIdentifier] = $fieldType->getName($fieldMap[$fieldDefinitionIdentifier][$languageCode]); } } return $fieldTitles; }
/** * Returns an array of domain fields created from given array of SPI fields * * @param \eZ\Publish\SPI\Persistence\Content\Field[] $spiFields * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType * * @return array */ public function buildDomainFields(array $spiFields, ContentType $contentType) { $fieldIdentifierMap = array(); foreach ($contentType->getFieldDefinitions() as $fieldDefinitions) { $fieldIdentifierMap[$fieldDefinitions->id] = $fieldDefinitions->identifier; } /** @var \eZ\Publish\Core\Repository\FieldTypeService $fieldTypeService */ $fieldTypeService = $this->repository->getFieldTypeService(); $fields = array(); foreach ($spiFields as $spiField) { $fields[] = new Field(array("id" => $spiField->id, "value" => $fieldTypeService->buildFieldType($spiField->type)->fromPersistenceValue($spiField->value), "languageCode" => $spiField->languageCode, "fieldDefIdentifier" => $fieldIdentifierMap[$spiField->fieldDefinitionId])); } return $fields; }
/** * Returns an array of domain fields created from given array of SPI fields * * @param \eZ\Publish\SPI\Persistence\Content\Field[] $spiFields * @param ContentType|SPIType $contentType * * @return array */ public function buildDomainFields(array $spiFields, $contentType) { $fieldIdentifierMap = array(); if (!$contentType instanceof SPIType && !$contentType instanceof ContentType) { throw new InvalidArgumentType("\$contentType", "SPI ContentType | API ContentType"); } foreach ($contentType->fieldDefinitions as $fieldDefinitions) { $fieldIdentifierMap[$fieldDefinitions->id] = $fieldDefinitions->identifier; } $fieldTypeService = $this->repository->getFieldTypeService(); $fields = array(); foreach ($spiFields as $spiField) { $fields[] = new Field(array("id" => $spiField->id, "value" => $fieldTypeService->buildFieldType($spiField->type)->fromPersistenceValue($spiField->value), "languageCode" => $spiField->languageCode, "fieldDefIdentifier" => $fieldIdentifierMap[$spiField->fieldDefinitionId])); } return $fields; }
/** * Adds a new field definition to an existing content type. * * The content type must be in state DRAFT. * * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the identifier in already exists in the content type * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to edit a content type * @throws \eZ\Publish\API\Repository\Exceptions\ContentTypeFieldDefinitionValidationException * if a field definition in the $contentTypeCreateStruct is not valid * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If field definition of the same non-repeatable type is being * added to the ContentType that already contains one * or field definition that can't be added to a ContentType that * has Content instances is being added to such ContentType * * @param \eZ\Publish\API\Repository\Values\ContentType\ContentTypeDraft $contentTypeDraft * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct $fieldDefinitionCreateStruct */ public function addFieldDefinition(APIContentTypeDraft $contentTypeDraft, FieldDefinitionCreateStruct $fieldDefinitionCreateStruct) { if ($this->repository->hasAccess('class', 'update') !== true) { throw new UnauthorizedException('ContentType', 'update'); } $this->validateInputFieldDefinitionCreateStruct($fieldDefinitionCreateStruct); $loadedContentTypeDraft = $this->loadContentTypeDraft($contentTypeDraft->id); if ($loadedContentTypeDraft->getFieldDefinition($fieldDefinitionCreateStruct->identifier) !== null) { throw new InvalidArgumentException("\$fieldDefinitionCreateStruct", "Another FieldDefinition with identifier '{$fieldDefinitionCreateStruct->identifier}' exists in the ContentType"); } /** @var $fieldType \eZ\Publish\SPI\FieldType\FieldType */ $fieldType = $this->repository->getFieldTypeService()->buildFieldType($fieldDefinitionCreateStruct->fieldTypeIdentifier); $fieldType->applyDefaultSettings($fieldDefinitionCreateStruct->fieldSettings); $fieldType->applyDefaultValidatorConfiguration($fieldDefinitionCreateStruct->validatorConfiguration); $validationErrors = $this->validateFieldDefinitionCreateStruct($fieldDefinitionCreateStruct, $fieldType); if (!empty($validationErrors)) { $validationErrors = array($fieldDefinitionCreateStruct->identifier => $validationErrors); throw new ContentTypeFieldDefinitionValidationException($validationErrors); } if ($fieldType->isSingular()) { foreach ($loadedContentTypeDraft->getFieldDefinitions() as $fieldDefinition) { if ($fieldDefinition->fieldTypeIdentifier === $fieldDefinitionCreateStruct->fieldTypeIdentifier) { throw new BadStateException("\$contentTypeDraft", "ContentType already contains field definition of non-repeatable field type '{$fieldDefinition->fieldTypeIdentifier}'"); } } } if ($fieldType->onlyEmptyInstance() && $this->contentTypeHandler->getContentCount($loadedContentTypeDraft->id)) { throw new BadStateException("\$contentTypeDraft", "Field definition of '{$fieldDefinitionCreateStruct->fieldTypeIdentifier}' field type cannot be added because ContentType has Content instances"); } $spiFieldDefinitionCreateStruct = $this->buildSPIFieldDefinitionCreate($fieldDefinitionCreateStruct, $fieldType); $this->repository->beginTransaction(); try { $this->contentTypeHandler->addFieldDefinition($contentTypeDraft->id, $contentTypeDraft->status, $spiFieldDefinitionCreateStruct); $this->repository->commit(); } catch (Exception $e) { $this->repository->rollback(); throw $e; } }