public function processAddFieldDefinition(FormActionEvent $event) { $contentTypeDraft = $event->getData()->contentTypeDraft; $fieldTypeIdentifier = $event->getForm()->get('fieldTypeSelection')->getData(); $fieldDefCreateStruct = new FieldDefinitionCreateStruct(['fieldTypeIdentifier' => $fieldTypeIdentifier, 'identifier' => sprintf('new_%s_%d', $fieldTypeIdentifier, count($contentTypeDraft->fieldDefinitions) + 1), 'names' => [$event->getOption('languageCode') => 'New FieldDefinition']]); $this->contentTypeService->addFieldDefinition($contentTypeDraft, $fieldDefCreateStruct); }
public function processAddFieldDefinition(FormActionEvent $event) { // Reload the draft, to make sure we include any changes made in the current form submit $contentTypeDraft = $this->contentTypeService->loadContentTypeDraft($event->getData()->contentTypeDraft->id); $fieldTypeIdentifier = $event->getForm()->get('fieldTypeSelection')->getData(); $maxFieldPos = 0; foreach ($contentTypeDraft->fieldDefinitions as $existingFieldDef) { if ($existingFieldDef->position > $maxFieldPos) { $maxFieldPos = $existingFieldDef->position; } } $fieldDefCreateStruct = new FieldDefinitionCreateStruct(['fieldTypeIdentifier' => $fieldTypeIdentifier, 'identifier' => sprintf('new_%s_%d', $fieldTypeIdentifier, count($contentTypeDraft->fieldDefinitions) + 1), 'names' => [$event->getOption('languageCode') => 'New FieldDefinition'], 'position' => $maxFieldPos + 1]); $this->contentTypeService->addFieldDefinition($contentTypeDraft, $fieldDefCreateStruct); }
/** * Update draft fields with diff data * * @param ContentTypeDraft $draft * @param array $diff * @param array $data */ private function updateDraftFields(ContentTypeDraft $draft, array $diff, array &$data) { // Remove fields which are missing in the new definition foreach ($draft->fieldDefinitions as $fieldDefinition) { if (in_array($fieldDefinition->identifier, $diff['remove'])) { $this->contentTypeService->removeFieldDefinition($draft, $fieldDefinition); } } // Add fields which are missing in the old content type $fieldStructs = $data['field_definitions']; //$this->getFieldDefinitionCreateStructs($data); foreach ($fieldStructs as $fieldStruct) { if (in_array($fieldStruct->identifier, $diff['add'])) { $this->contentTypeService->addFieldDefinition($draft, $fieldStruct); } } }
/** * Creates a new field definition for the given content type draft * * @param $contentTypeId * * @throws \eZ\Publish\Core\REST\Server\Exceptions\ForbiddenException * @throws \eZ\Publish\Core\REST\Common\Exceptions\NotFoundException * @return \eZ\Publish\Core\REST\Server\Values\CreatedFieldDefinition */ public function addContentTypeDraftFieldDefinition($contentTypeId, Request $request) { $contentTypeDraft = $this->contentTypeService->loadContentTypeDraft($contentTypeId); $fieldDefinitionCreate = $this->inputDispatcher->parse(new Message(array('Content-Type' => $request->headers->get('Content-Type')), $request->getContent())); try { $this->contentTypeService->addFieldDefinition($contentTypeDraft, $fieldDefinitionCreate); } catch (InvalidArgumentException $e) { throw new ForbiddenException($e->getMessage()); } catch (ContentTypeFieldDefinitionValidationException $e) { throw new BadRequestException($e->getMessage()); } catch (BadStateException $e) { throw new ForbiddenException($e->getMessage()); } $updatedDraft = $this->contentTypeService->loadContentTypeDraft($contentTypeId); foreach ($updatedDraft->getFieldDefinitions() as $fieldDefinition) { if ($fieldDefinition->identifier == $fieldDefinitionCreate->identifier) { return new Values\CreatedFieldDefinition(array('fieldDefinition' => new Values\RestFieldDefinition($updatedDraft, $fieldDefinition))); } } throw new Exceptions\NotFoundException("Field definition not found: '{$request->getPathInfo()}'."); }
/** * 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(ContentTypeDraft $contentTypeDraft, FieldDefinitionCreateStruct $fieldDefinitionCreateStruct) { $returnValue = $this->service->addFieldDefinition($contentTypeDraft, $fieldDefinitionCreateStruct); $this->signalDispatcher->emit(new AddFieldDefinitionSignal(array('contentTypeDraftId' => $contentTypeDraft->id))); return $returnValue; }