Skip to content

Commit

Permalink
EZP-29925: Content Type Translate in Edit mode is not based on browse…
Browse files Browse the repository at this point in the history
…r's settings (ezsystems#267)

* EZP-29925: Content Type Translate in Edit mode is not based on browser's settings

* EZP-29925: Content Type Translate in Edit mode is not based on browser's settings

* EZP-29925: use UserLanguagePreferenceProviderInterface
  • Loading branch information
ViniTou authored and Łukasz Serwatka committed Dec 21, 2018
1 parent 3f127c0 commit 61f6e9f
Show file tree
Hide file tree
Showing 29 changed files with 98 additions and 118 deletions.
26 changes: 20 additions & 6 deletions lib/Content/View/Builder/ContentCreateViewBuilder.php
Expand Up @@ -13,6 +13,7 @@
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface;
use eZ\Publish\Core\MVC\Symfony\View\Builder\ViewBuilder;
use eZ\Publish\Core\MVC\Symfony\View\Configurator;
use eZ\Publish\Core\MVC\Symfony\View\ParametersInjector;
Expand Down Expand Up @@ -42,18 +43,31 @@ class ContentCreateViewBuilder implements ViewBuilder
/** @var ActionDispatcherInterface */
private $contentActionDispatcher;

/** @var \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface */
private $languagePreferenceProvider;

/**
* @param \eZ\Publish\API\Repository\Repository $repository
* @param \eZ\Publish\Core\MVC\Symfony\View\Configurator $viewConfigurator
* @param \eZ\Publish\Core\MVC\Symfony\View\ParametersInjector $viewParametersInjector
* @param $defaultTemplate
* @param \EzSystems\RepositoryForms\Form\ActionDispatcher\ActionDispatcherInterface $contentActionDispatcher
* @param \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface $languagePreferenceProvider
*/
public function __construct(
Repository $repository,
Configurator $viewConfigurator,
ParametersInjector $viewParametersInjector,
$defaultTemplate,
ActionDispatcherInterface $contentActionDispatcher
ActionDispatcherInterface $contentActionDispatcher,
UserLanguagePreferenceProviderInterface $languagePreferenceProvider
) {
$this->repository = $repository;
$this->viewConfigurator = $viewConfigurator;
$this->viewParametersInjector = $viewParametersInjector;
$this->defaultTemplate = $defaultTemplate;
$this->contentActionDispatcher = $contentActionDispatcher;
$this->languagePreferenceProvider = $languagePreferenceProvider;
}

public function matches($argument)
Expand All @@ -77,7 +91,7 @@ public function buildView(array $parameters)

$language = $this->resolveLanguage($parameters);
$location = $this->resolveLocation($parameters);
$contentType = $this->resolveContentType($parameters, $language);
$contentType = $this->resolveContentType($parameters, $this->languagePreferenceProvider->getPreferredLanguages());
$form = $parameters['form'];

if ($form->isValid() && null !== $form->getClickedButton()) {
Expand Down Expand Up @@ -144,7 +158,7 @@ private function loadLanguage(string $languageCode): Language
* Loads ContentType with identifier $contentTypeIdentifier.
*
* @param string $contentTypeIdentifier
* @param array $prioritizedLanguages
* @param string[] $prioritizedLanguages
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentType
*
Expand Down Expand Up @@ -187,21 +201,21 @@ private function resolveLanguage(array $parameters): Language

/**
* @param array $parameters
* @param \eZ\Publish\API\Repository\Values\Content\Language $language
* @param array $languageCodes
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentType
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
private function resolveContentType(array $parameters, Language $language): ContentType
private function resolveContentType(array $parameters, array $languageCodes): ContentType
{
if (isset($parameters['contentType'])) {
return $parameters['contentType'];
}

if (isset($parameters['contentTypeIdentifier'])) {
return $this->loadContentType($parameters['contentTypeIdentifier'], [$language->languageCode]);
return $this->loadContentType($parameters['contentTypeIdentifier'], $languageCodes);
}

throw new InvalidArgumentException(
Expand Down
24 changes: 19 additions & 5 deletions lib/Content/View/Builder/ContentEditViewBuilder.php
Expand Up @@ -14,6 +14,7 @@
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface;
use eZ\Publish\Core\MVC\Symfony\View\Builder\ViewBuilder;
use eZ\Publish\Core\MVC\Symfony\View\Configurator;
use eZ\Publish\Core\MVC\Symfony\View\ParametersInjector;
Expand Down Expand Up @@ -43,18 +44,31 @@ class ContentEditViewBuilder implements ViewBuilder
/** @var \EzSystems\RepositoryForms\Form\ActionDispatcher\ActionDispatcherInterface */
private $contentActionDispatcher;

/** @var \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface */
private $languagePreferenceProvider;

/**
* @param \eZ\Publish\API\Repository\Repository $repository
* @param \eZ\Publish\Core\MVC\Symfony\View\Configurator $viewConfigurator
* @param \eZ\Publish\Core\MVC\Symfony\View\ParametersInjector $viewParametersInjector
* @param string $defaultTemplate
* @param \EzSystems\RepositoryForms\Form\ActionDispatcher\ActionDispatcherInterface $contentActionDispatcher
* @param \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface $languagePreferenceProvider
*/
public function __construct(
Repository $repository,
Configurator $viewConfigurator,
ParametersInjector $viewParametersInjector,
string $defaultTemplate,
ActionDispatcherInterface $contentActionDispatcher
ActionDispatcherInterface $contentActionDispatcher,
UserLanguagePreferenceProviderInterface $languagePreferenceProvider
) {
$this->repository = $repository;
$this->viewConfigurator = $viewConfigurator;
$this->viewParametersInjector = $viewParametersInjector;
$this->defaultTemplate = $defaultTemplate;
$this->contentActionDispatcher = $contentActionDispatcher;
$this->languagePreferenceProvider = $languagePreferenceProvider;
}

public function matches($argument)
Expand Down Expand Up @@ -82,7 +96,7 @@ public function buildView(array $parameters)
$location = $this->resolveLocation($parameters);
$content = $this->resolveContent($parameters, $location, $language);
$contentInfo = $content->contentInfo;
$contentType = $this->loadContentType((int) $contentInfo->contentTypeId, $language->languageCode);
$contentType = $this->loadContentType((int) $contentInfo->contentTypeId, $this->languagePreferenceProvider->getPreferredLanguages());
$form = $parameters['form'];
$isPublished = null !== $contentInfo->mainLocationId && $contentInfo->published;

Expand Down Expand Up @@ -178,15 +192,15 @@ private function loadLanguage(string $languageCode): Language
* Loads ContentType with id $contentTypeId.
*
* @param int $contentTypeId
* @param string $languageCode
* @param string[] $languageCodes
*
* @return \eZ\Publish\API\Repository\Values\ContentType\ContentType
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
private function loadContentType(int $contentTypeId, string $languageCode): ContentType
private function loadContentType(int $contentTypeId, array $languageCodes): ContentType
{
return $this->repository->getContentTypeService()->loadContentType($contentTypeId, [$languageCode]);
return $this->repository->getContentTypeService()->loadContentType($contentTypeId, $languageCodes);
}

/**
Expand Down
20 changes: 15 additions & 5 deletions lib/Content/View/Filter/ContentCreateViewFilter.php
Expand Up @@ -12,6 +12,7 @@
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface;
use eZ\Publish\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent;
use eZ\Publish\Core\MVC\Symfony\View\ViewEvents;
use EzSystems\RepositoryForms\Data\Content\ContentCreateData;
Expand All @@ -32,19 +33,25 @@ class ContentCreateViewFilter implements EventSubscriberInterface
/** @var \Symfony\Component\Form\FormFactoryInterface */
private $formFactory;

/** @var \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface */
private $languagePreferenceProvider;

/**
* @param \eZ\Publish\API\Repository\LocationService $locationService
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService
* @param \Symfony\Component\Form\FormFactoryInterface $formFactory
* @param \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface $languagePreferenceProvider
*/
public function __construct(
LocationService $locationService,
ContentTypeService $contentTypeService,
FormFactoryInterface $formFactory
FormFactoryInterface $formFactory,
UserLanguagePreferenceProviderInterface $languagePreferenceProvider
) {
$this->locationService = $locationService;
$this->contentTypeService = $contentTypeService;
$this->formFactory = $formFactory;
$this->languagePreferenceProvider = $languagePreferenceProvider;
}

public static function getSubscribedEvents()
Expand All @@ -67,13 +74,18 @@ public function handleContentCreateForm(FilterViewBuilderParametersEvent $event)

$request = $event->getRequest();
$languageCode = $request->attributes->get('language');

$contentType = $this->contentTypeService->loadContentTypeByIdentifier(
$request->attributes->get('contentTypeIdentifier')
$request->attributes->get('contentTypeIdentifier'),
$this->languagePreferenceProvider->getPreferredLanguages()
);
$location = $this->locationService->loadLocation($request->attributes->get('parentLocationId'));

$contentCreateData = $this->resolveContentCreateData($contentType, $location, $languageCode);
$form = $this->resolveContentCreateForm($contentCreateData, $languageCode);
$form = $this->resolveContentCreateForm(
$contentCreateData,
$languageCode
);

$event->getParameters()->add(['form' => $form->handleRequest($request)]);
}
Expand Down Expand Up @@ -106,8 +118,6 @@ private function resolveContentCreateData(
* @param string $languageCode
*
* @return \Symfony\Component\Form\FormInterface
*
* @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
private function resolveContentCreateForm(
ContentCreateData $contentCreateData,
Expand Down
23 changes: 18 additions & 5 deletions lib/Content/View/Filter/ContentEditViewFilter.php
Expand Up @@ -12,6 +12,7 @@
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface;
use eZ\Publish\Core\MVC\Symfony\View\Event\FilterViewBuilderParametersEvent;
use eZ\Publish\Core\MVC\Symfony\View\ViewEvents;
use EzSystems\RepositoryForms\Data\Content\ContentUpdateData;
Expand All @@ -32,19 +33,25 @@ class ContentEditViewFilter implements EventSubscriberInterface
/** @var \Symfony\Component\Form\FormFactoryInterface */
private $formFactory;

/** @var \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface */
private $languagePreferenceProvider;

/**
* @param \eZ\Publish\API\Repository\ContentService $contentService
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService
* @param \Symfony\Component\Form\FormFactoryInterface $formFactory
* @param \eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface $languagePreferenceProvider
*/
public function __construct(
ContentService $contentService,
ContentTypeService $contentTypeService,
FormFactoryInterface $formFactory
FormFactoryInterface $formFactory,
UserLanguagePreferenceProviderInterface $languagePreferenceProvider
) {
$this->contentService = $contentService;
$this->contentTypeService = $contentTypeService;
$this->formFactory = $formFactory;
$this->languagePreferenceProvider = $languagePreferenceProvider;
}

public static function getSubscribedEvents()
Expand Down Expand Up @@ -72,10 +79,18 @@ public function handleContentEditForm(FilterViewBuilderParametersEvent $event)
[$languageCode], // @todo: rename to languageCode in 3.0
$request->attributes->get('versionNo')
);
$contentType = $this->contentTypeService->loadContentType($contentDraft->contentInfo->contentTypeId);

$contentType = $this->contentTypeService->loadContentType(
$contentDraft->contentInfo->contentTypeId,
$this->languagePreferenceProvider->getPreferredLanguages()
);

$contentUpdate = $this->resolveContentEditData($contentDraft, $languageCode, $contentType);
$form = $this->resolveContentEditForm($contentUpdate, $languageCode, $contentDraft);
$form = $this->resolveContentEditForm(
$contentUpdate,
$languageCode,
$contentDraft
);

$event->getParameters()->add(['form' => $form->handleRequest($request)]);
}
Expand Down Expand Up @@ -106,8 +121,6 @@ private function resolveContentEditData(
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
*
* @return \Symfony\Component\Form\FormInterface
*
* @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
private function resolveContentEditForm(
ContentUpdateData $contentUpdate,
Expand Down
5 changes: 1 addition & 4 deletions lib/FieldType/Mapper/AuthorFormMapper.php
Expand Up @@ -59,17 +59,14 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
$fieldDefinition = $data->fieldDefinition;
$fieldSettings = $fieldDefinition->getFieldSettings();
$formConfig = $fieldForm->getConfig();
$names = $fieldDefinition->getNames();
$label = $fieldDefinition->getName($formConfig->getOption('languageCode'))
?: $fieldDefinition->getName($formConfig->getOption('mainLanguageCode'));

$fieldForm
->add(
$formConfig->getFormFactory()->createBuilder()
->create('value', AuthorFieldType::class, [
'default_author' => $fieldSettings['defaultAuthor'],
'required' => $fieldDefinition->isRequired,
'label' => $label ?? reset($names),
'label' => $fieldDefinition->getName(),
])
->setAutoInitialize(false)
->getForm()
Expand Down
5 changes: 1 addition & 4 deletions lib/FieldType/Mapper/BinaryFileFormMapper.php
Expand Up @@ -61,9 +61,6 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
$fieldDefinition = $data->fieldDefinition;
$formConfig = $fieldForm->getConfig();
$fieldType = $this->fieldTypeService->getFieldType($fieldDefinition->fieldTypeIdentifier);
$names = $fieldDefinition->getNames();
$label = $fieldDefinition->getName($formConfig->getOption('languageCode'))
?: $fieldDefinition->getName($formConfig->getOption('mainLanguageCode'));

$fieldForm
->add(
Expand All @@ -73,7 +70,7 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
BinaryFileFieldType::class,
[
'required' => $fieldDefinition->isRequired,
'label' => $label ?? reset($names),
'label' => $fieldDefinition->getName(),
]
)
->addModelTransformer(new BinaryFileValueTransformer($fieldType, $data->value, Value::class))
Expand Down
5 changes: 1 addition & 4 deletions lib/FieldType/Mapper/CheckboxFormMapper.php
Expand Up @@ -43,9 +43,6 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
{
$fieldDefinition = $data->fieldDefinition;
$formConfig = $fieldForm->getConfig();
$names = $fieldDefinition->getNames();
$label = $fieldDefinition->getName($formConfig->getOption('languageCode'))
?: $fieldDefinition->getName($formConfig->getOption('mainLanguageCode'));

$fieldForm
->add(
Expand All @@ -55,7 +52,7 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
CheckboxFieldType::class,
[
'required' => $fieldDefinition->isRequired,
'label' => $label ?? reset($names),
'label' => $fieldDefinition->getName(),
]
)
->setAutoInitialize(false)
Expand Down
5 changes: 1 addition & 4 deletions lib/FieldType/Mapper/CountryFormMapper.php
Expand Up @@ -56,17 +56,14 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
$fieldDefinition = $data->fieldDefinition;
$fieldSettings = $fieldDefinition->getFieldSettings();
$formConfig = $fieldForm->getConfig();
$names = $fieldDefinition->getNames();
$label = $fieldDefinition->getName($formConfig->getOption('languageCode'))
?: $fieldDefinition->getName($formConfig->getOption('mainLanguageCode'));

$fieldForm
->add(
$formConfig->getFormFactory()->createBuilder()
->create('value', CountryFieldType::class, [
'multiple' => $fieldSettings['isMultiple'],
'required' => $fieldDefinition->isRequired,
'label' => $label ?? reset($names),
'label' => $fieldDefinition->getName(),
])
->setAutoInitialize(false)
->getForm()
Expand Down
5 changes: 1 addition & 4 deletions lib/FieldType/Mapper/DateFormMapper.php
Expand Up @@ -50,9 +50,6 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
{
$fieldDefinition = $data->fieldDefinition;
$formConfig = $fieldForm->getConfig();
$names = $fieldDefinition->getNames();
$label = $fieldDefinition->getName($formConfig->getOption('languageCode'))
?: $fieldDefinition->getName($formConfig->getOption('mainLanguageCode'));

$fieldForm
->add(
Expand All @@ -62,7 +59,7 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
DateFieldType::class,
[
'required' => $fieldDefinition->isRequired,
'label' => $label ?? reset($names),
'label' => $fieldDefinition->getName(),
]
)
->setAutoInitialize(false)
Expand Down
5 changes: 1 addition & 4 deletions lib/FieldType/Mapper/DateTimeFormMapper.php
Expand Up @@ -61,9 +61,6 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
$fieldDefinition = $data->fieldDefinition;
$fieldSettings = $fieldDefinition->getFieldSettings();
$formConfig = $fieldForm->getConfig();
$names = $fieldDefinition->getNames();
$label = $fieldDefinition->getName($formConfig->getOption('languageCode'))
?: $fieldDefinition->getName($formConfig->getOption('mainLanguageCode'));

$fieldForm
->add(
Expand All @@ -74,7 +71,7 @@ public function mapFieldValueForm(FormInterface $fieldForm, FieldData $data)
[
'with_seconds' => $fieldSettings['useSeconds'],
'required' => $fieldDefinition->isRequired,
'label' => $label ?? reset($names),
'label' => $fieldDefinition->getName(),
]
)
->setAutoInitialize(false)
Expand Down

0 comments on commit 61f6e9f

Please sign in to comment.