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);
 }
 public function updateContentTypeAction(Request $request, $contentTypeId, $languageCode = null)
 {
     $languageCode = $languageCode ?: $this->prioritizedLanguages[0];
     // First try to load the draft.
     // If it doesn't exist, create it.
     try {
         $contentTypeDraft = $this->contentTypeService->loadContentTypeDraft($contentTypeId);
     } catch (NotFoundException $e) {
         $contentTypeDraft = $this->contentTypeService->createContentTypeDraft($this->contentTypeService->loadContentType($contentTypeId));
     }
     $contentTypeData = (new ContentTypeDraftMapper())->mapToFormData($contentTypeDraft);
     $form = $this->createForm('ezrepoforms_contenttype_update', $contentTypeData, ['languageCode' => $languageCode]);
     $actionUrl = $this->generateUrl('admin_contenttypeUpdate', ['contentTypeId' => $contentTypeId, 'languageCode' => $languageCode]);
     // Synchronize form and data.
     $form->handleRequest($request);
     $hasErrors = false;
     if ($form->isValid()) {
         $this->contentTypeActionDispatcher->dispatchFormAction($form, $contentTypeData, $form->getClickedButton() ? $form->getClickedButton()->getName() : null, ['languageCode' => $languageCode]);
         if ($response = $this->contentTypeActionDispatcher->getResponse()) {
             return $response;
         }
         return $this->redirectAfterFormPost($actionUrl);
     } elseif ($form->isSubmitted()) {
         $hasErrors = true;
     }
     return $this->render('eZPlatformUIBundle:ContentType:update_content_type.html.twig', ['form' => $form->createView(), 'action_url' => $actionUrl, 'contentTypeName' => $contentTypeDraft->getName($languageCode), 'contentTypeDraft' => $contentTypeDraft, 'modifier' => $this->userService->loadUser($contentTypeDraft->modifierId), 'languageCode' => $languageCode, 'hasErrors' => $hasErrors]);
 }
 /**
  * Returns field definition by 'typeFieldDefinitionDraft' pattern URL.
  *
  * Assumes given $data array has '__url' element set.
  * @todo depends on temporary solution to give parser access to the URL
  * @see \eZ\Publish\Core\REST\Server\Controller\ContentType::updateFieldDefinition
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
  *
  * @param array $data
  *
  * @return \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition
  */
 protected function getFieldDefinition(array $data)
 {
     $contentTypeId = $this->requestParser->parseHref($data["__url"], 'contentTypeId');
     $fieldDefinitionId = $this->requestParser->parseHref($data["__url"], 'fieldDefinitionId');
     $contentTypeDraft = $this->contentTypeService->loadContentTypeDraft($contentTypeId);
     foreach ($contentTypeDraft->getFieldDefinitions() as $fieldDefinition) {
         if ($fieldDefinition->id == $fieldDefinitionId) {
             return $fieldDefinition;
         }
     }
     throw new Exceptions\NotFoundException("Field definition not found: '{$data["__url"]}'.");
 }
 /**
  * Get a Content Type object draft by id
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the content type draft owned by the current user can not be found
  *
  * @param mixed $contentTypeId
  *
  * @return \eZ\Publish\API\Repository\Values\ContentType\ContentTypeDraft
  */
 public function loadContentTypeDraft($contentTypeId)
 {
     return $this->service->loadContentTypeDraft($contentTypeId);
 }
 /**
  * The given content type draft is deleted
  *
  * @param $contentTypeId
  *
  * @return \eZ\Publish\Core\REST\Server\Values\NoContent
  */
 public function deleteContentTypeDraft($contentTypeId)
 {
     $contentTypeDraft = $this->contentTypeService->loadContentTypeDraft($contentTypeId);
     $this->contentTypeService->deleteContentType($contentTypeDraft);
     return new Values\NoContent();
 }