Skip to content

Commit

Permalink
EZP-27539: Adds language information to the content edit/create views
Browse files Browse the repository at this point in the history
  • Loading branch information
webhdx committed Dec 1, 2017
1 parent faf5158 commit 6276945
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 22 deletions.
39 changes: 21 additions & 18 deletions bundle/Controller/ContentEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use eZ\Bundle\EzPublishCoreBundle\Controller;
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\LanguageService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
use eZ\Publish\Core\Base\Exceptions\BadStateException;
Expand All @@ -27,24 +28,19 @@

class ContentEditController extends Controller
{
/**
* @var ContentTypeService
*/
/** @var ContentTypeService */
private $contentTypeService;

/**
* @var ContentService
*/
/** @var ContentService */
private $contentService;

/**
* @var LocationService
*/
/** @var LocationService */
private $locationService;

/**
* @var ActionDispatcherInterface
*/
/** @var LanguageService */
private $languageService;

/** @var ActionDispatcherInterface */
private $contentActionDispatcher;

/**
Expand All @@ -58,10 +54,12 @@ public function __construct(
ContentTypeService $contentTypeService,
ContentService $contentService,
LocationService $locationService,
LanguageService $languageService,
ActionDispatcherInterface $contentActionDispatcher
) {
$this->contentTypeService = $contentTypeService;
$this->locationService = $locationService;
$this->languageService = $languageService;
$this->contentActionDispatcher = $contentActionDispatcher;
$this->contentService = $contentService;
}
Expand All @@ -82,14 +80,16 @@ public function __construct(
*/
public function createWithoutDraftAction($contentTypeIdentifier, $language, $parentLocationId, Request $request)
{
$language = $this->languageService->loadLanguage($language);
$parentLocation = $this->locationService->loadLocation($parentLocationId);
$contentType = $this->contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier);
$data = (new ContentCreateMapper())->mapToFormData($contentType, [
'mainLanguageCode' => $language,
'mainLanguageCode' => $language->languageCode,
'parentLocation' => $this->locationService->newLocationCreateStruct($parentLocationId),
]);
$form = $this->createForm(ContentEditType::class, $data, [
'languageCode' => $language,
'languageCode' => $language->languageCode,
'mainLanguageCode' => $language->languageCode,
'drafts_enabled' => true,
]);
$form->handleRequest($request);
Expand All @@ -103,7 +103,7 @@ public function createWithoutDraftAction($contentTypeIdentifier, $language, $par

return new ContentCreateView(null, [
'form' => $form->createView(),
'languageCode' => $language,
'language' => $language,
'contentType' => $contentType,
'parentLocation' => $parentLocation,
]);
Expand Down Expand Up @@ -190,20 +190,23 @@ public function editContentDraftAction($contentId, $versionNo = null, Request $r
throw new BadStateException('Version status', 'status is not draft');
}

$language = $language ?: $draft->getVersionInfo()->getContentInfo()->mainLanguageCode;
$language = $this->languageService->loadLanguage($language);
$contentType = $this->contentTypeService->loadContentType($draft->contentInfo->contentTypeId);

$contentUpdate = (new ContentUpdateMapper())->mapToFormData(
$draft,
[
'languageCode' => $language,
'languageCode' => $language->languageCode,
'contentType' => $this->contentTypeService->loadContentType($draft->contentInfo->contentTypeId),
]
);
$form = $this->createForm(
ContentEditType::class,
$contentUpdate,
[
'languageCode' => $language,
'languageCode' => $language->languageCode,
'mainLanguageCode' => $draft->contentInfo->mainLanguageCode,
'drafts_enabled' => true,
]
);
Expand All @@ -218,7 +221,7 @@ public function editContentDraftAction($contentId, $versionNo = null, Request $r

return new ContentEditView(null, [
'form' => $form->createView(),
'languageCode' => $language,
'language' => $language,
'content' => $draft,
'contentType' => $contentType,
]);
Expand Down
1 change: 1 addition & 0 deletions bundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ services:
- "@ezpublish.api.service.content_type"
- "@ezpublish.api.service.content"
- "@ezpublish.api.service.location"
- "@ezpublish.api.service.language"
- "@ezrepoforms.action_dispatcher.content"
parent: ezpublish.controller.base
calls:
Expand Down
2 changes: 1 addition & 1 deletion lib/Data/Content/ContentUpdateData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

/**
* @property-read \EzSystems\RepositoryForms\Data\Content\FieldData[] $fieldsData
* @property-read \eZ\Publish\API\Repository\Values\Content\Content[] $contentDraft
* @property-read \eZ\Publish\API\Repository\Values\Content\Content $contentDraft
*/
class ContentUpdateData extends ContentUpdateStruct implements NewnessCheckable
{
Expand Down
20 changes: 20 additions & 0 deletions lib/Form/Processor/ContentFormProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\ContentStruct;
use eZ\Publish\Core\MVC\Symfony\Routing\UrlAliasRouter;
use EzSystems\RepositoryForms\Data\Content\ContentCreateData;
use EzSystems\RepositoryForms\Data\Content\ContentUpdateData;
use EzSystems\RepositoryForms\Data\NewnessChecker;
use EzSystems\RepositoryForms\Event\FormActionEvent;
use EzSystems\RepositoryForms\Event\RepositoryFormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down Expand Up @@ -153,7 +156,12 @@ public function processCreateDraft(FormActionEvent $event)
*/
private function saveDraft(ContentStruct $data, $languageCode)
{
$mainLanuageCode = $this->resolveMainLanguageCode($data);
foreach ($data->fieldsData as $fieldDefIdentifier => $fieldData) {
if ($mainLanuageCode && !$fieldData->fieldDefinition->isTranslatable) {
continue;
}

$data->setField($fieldDefIdentifier, $fieldData->value, $languageCode);
}

Expand All @@ -165,4 +173,16 @@ private function saveDraft(ContentStruct $data, $languageCode)

return $contentDraft;
}

/**
* @param ContentUpdateData|ContentCreateData|NewnessChecker $data
*
* @return string
*/
private function resolveMainLanguageCode($data): string
{
return $data->isNew()
? $data->mainLanguageCode
: $data->contentDraft->getVersionInfo()->getContentInfo()->mainLanguageCode;
}
}
15 changes: 13 additions & 2 deletions lib/Form/Type/Content/ContentEditType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -39,7 +41,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('fieldsData', CollectionType::class, [
'entry_type' => ContentFieldType::class,
'label' => 'ezrepoforms.content.fields',
'entry_options' => ['languageCode' => $options['languageCode']],
'entry_options' => [
'languageCode' => $options['languageCode'],
'mainLanguageCode' => $options['mainLanguageCode'],
],
])
->add('redirectUrlAfterPublish', HiddenType::class, ['required' => false, 'mapped' => false])
->add('publish', SubmitType::class, ['label' => 'content.publish_button']);
Expand All @@ -55,6 +60,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}
}

public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['languageCode'] = $options['languageCode'];
$view->vars['mainLanguageCode'] = $options['mainLanguageCode'];
}

public function suppressValidationOnCancel(FormEvent $event)
{
$form = $event->getForm();
Expand All @@ -72,6 +83,6 @@ public function configureOptions(OptionsResolver $resolver)
'data_class' => '\eZ\Publish\API\Repository\Values\Content\ContentStruct',
'translation_domain' => 'ezrepoforms_content',
])
->setRequired(['languageCode']);
->setRequired(['languageCode', 'mainLanguageCode']);
}
}
10 changes: 9 additions & 1 deletion lib/Form/Type/Content/ContentFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ContentFieldType extends AbstractType
Expand Down Expand Up @@ -43,7 +45,13 @@ public function configureOptions(OptionsResolver $resolver)
'data_class' => '\EzSystems\RepositoryForms\Data\Content\FieldData',
'translation_domain' => 'ezrepoforms_content',
])
->setRequired(['languageCode']);
->setRequired(['languageCode', 'mainLanguageCode']);
}

public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['languageCode'] = $options['languageCode'];
$view->vars['mainLanguageCode'] = $options['mainLanguageCode'];
}

public function buildForm(FormBuilderInterface $builder, array $options)
Expand Down

0 comments on commit 6276945

Please sign in to comment.