Skip to content

Commit

Permalink
Spiked content edition
Browse files Browse the repository at this point in the history
Given a content with id N with a draft number 2 that I can edit
 When I go to /content/edit/N/2
 Then I see a content edit form
 When I click on the publish button
 Then the draft is published
  • Loading branch information
Bertrand Dunogier committed Feb 23, 2017
1 parent 3ab2ce0 commit a6506e2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
54 changes: 49 additions & 5 deletions bundle/Controller/ContentEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
use eZ\Publish\Core\Base\Exceptions\BadStateException;
use EzSystems\RepositoryForms\Data\Content\CreateContentDraftData;
use EzSystems\RepositoryForms\Data\Mapper\ContentCreateMapper;
use EzSystems\RepositoryForms\Data\Mapper\ContentUpdateMapper;
use EzSystems\RepositoryForms\Form\ActionDispatcher\ActionDispatcherInterface;
use EzSystems\RepositoryForms\Form\Type\Content\ContentDraftCreateType;
use EzSystems\RepositoryForms\Form\Type\Content\ContentEditType;
Expand Down Expand Up @@ -126,15 +129,56 @@ public function createContentDraftAction($contentId, $fromVersionNo = null, $fro
$form->handleRequest($request);

if ($form->isValid()) {
$this->contentService->createContentDraft(
$contentInfo,
$this->contentService->loadVersionInfo($contentInfo, $fromVersionNo)
);
$this->contentActionDispatcher->dispatchFormAction($form, $createContentDraft, $form->getClickedButton()->getName());
if ($response = $this->contentActionDispatcher->getResponse()) {
return $response;
}
}

return $this->render('@EzSystemsRepositoryForms/Content/content_create_draft.html.twig', [
'form' => $form->createView(),
//'languageCode' => $language,
'pagelayout' => $this->pagelayout,
]);
}

/**
* Shows a content draft editing form.
*
* @param int $contentId ContentType id to create
* @param int $versionNo Version number the version should be created from. Defaults to the currently published one.
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string $language Language code to create the version in (eng-GB, ger-DE, ...))
*
* @return \Symfony\Component\HttpFoundation\Response
* @throws \eZ\Publish\Core\Base\Exceptions\BadStateException If the version isn't editable, or if there is no editable version.
*/
public function editContentDraftAction($contentId, $versionNo = null, Request $request, $language = null)
{
$draft = $this->contentService->loadContent($contentId, [$language], $versionNo);
if ($draft->getVersionInfo()->status !== VersionInfo::STATUS_DRAFT) {
throw new BadStateException('Version status', 'status is not draft');
}

$contentUpdate = (new ContentUpdateMapper())->mapToFormData(
$draft,
[
'languageCode' => $language,
'contentType' => $this->contentTypeService->loadContentType($draft->contentInfo->contentTypeId),
]
);
$form = $this->createForm(ContentEditType::class, $contentUpdate, ['languageCode' => $language]);
$form->handleRequest($request);

if ($form->isValid()) {
$this->contentActionDispatcher->dispatchFormAction($form, $contentUpdate, $form->getClickedButton()->getName());
if ($response = $this->contentActionDispatcher->getResponse()) {
return $response;
}
}

return $this->render('EzSystemsRepositoryFormsBundle:Content:content_edit.html.twig', [
'form' => $form->createView(),
'languageCode' => $language,
'pagelayout' => $this->pagelayout,
]);
}
Expand Down
6 changes: 6 additions & 0 deletions bundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ ez_content_create_no_draft:
defaults:
_controller: ez_content_edit:createWithoutDraftAction

ez_content_edit:
path: /content/edit/{contentId}/{versionNo}/{language}
defaults:
_controller: ez_content_edit:editContentDraftAction
language: null

ez_content_draft_create:
path: /content/create/draft/{contentId}/{fromVersionNo}/{fromLanguage}/{toLanguage}
defaults:
Expand Down
28 changes: 28 additions & 0 deletions lib/Data/Content/ContentUpdateData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* This file is part of the eZ RepositoryForms package.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/
namespace EzSystems\RepositoryForms\Data\Content;

use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct;
use EzSystems\RepositoryForms\Data\NewnessCheckable;

/**
* @property-read \EzSystems\RepositoryForms\Data\Content\FieldData[] $fieldsData
* @property-read \eZ\Publish\API\Repository\Values\Content\Content[] $contentDraft
*/
class ContentUpdateData extends ContentUpdateStruct implements NewnessCheckable
{
use ContentData;

protected $contentDraft;

public function isNew()
{
return false;
}
}
2 changes: 1 addition & 1 deletion lib/Data/Mapper/ContentUpdateMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function mapToFormData(ValueObject $contentDraft, array $params = [])
$this->configureOptions($optionsResolver);
$params = $optionsResolver->resolve($params);

$data = new ContentUpdateData(['contentDraft' => $contentDraft, 'contentType' => $params['contentType']]);
$data = new ContentUpdateData(['contentDraft' => $contentDraft]);
$fields = $contentDraft->getFieldsByLanguage($params['languageCode']);
foreach ($params['contentType']->fieldDefinitions as $fieldDef) {
$field = $fields[$fieldDef->identifier];
Expand Down

0 comments on commit a6506e2

Please sign in to comment.