Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-7278: Changed HTML form to symfony one #1075

Merged
merged 6 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/bundle/Controller/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Ibexa\AdminUi\Form\Factory\FormFactory;
use Ibexa\AdminUi\Form\SubmitHandler;
use Ibexa\AdminUi\Form\Type\Content\Translation\MainTranslationUpdateType;
use Ibexa\AdminUi\Form\Type\Preview\SiteAccessChoiceType;
use Ibexa\AdminUi\Permission\LookupLimitationsTransformer;
use Ibexa\AdminUi\Siteaccess\SiteAccessNameGeneratorInterface;
use Ibexa\AdminUi\Siteaccess\SiteaccessResolverInterface;
Expand All @@ -40,6 +41,7 @@
use Ibexa\Core\Base\Exceptions\BadStateException;
use Ibexa\Core\Helper\TranslationHelper;
use JMS\TranslationBundle\Annotation\Desc;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -90,6 +92,8 @@ class ContentController extends Controller
/** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
private $eventDispatcher;

private FormFactoryInterface $baseFormFactory;

public function __construct(
TranslatableNotificationHandlerInterface $notificationHandler,
ContentService $contentService,
Expand All @@ -104,7 +108,8 @@ public function __construct(
TranslationHelper $translationHelper,
ConfigResolverInterface $configResolver,
SiteAccessNameGeneratorInterface $siteAccessNameGenerator,
EventDispatcherInterface $eventDispatcher
EventDispatcherInterface $eventDispatcher,
FormFactoryInterface $baseFormFactory
) {
$this->notificationHandler = $notificationHandler;
$this->contentService = $contentService;
Expand All @@ -120,6 +125,7 @@ public function __construct(
$this->configResolver = $configResolver;
$this->siteAccessNameGenerator = $siteAccessNameGenerator;
$this->eventDispatcher = $eventDispatcher;
$this->baseFormFactory = $baseFormFactory;
}

/**
Expand Down Expand Up @@ -387,14 +393,36 @@ public function previewAction(
$preselectedSiteAccess !== null &&
!array_key_exists($preselectedSiteAccess, $siteAccessesList)
) {
$preselectedSiteAccess = null;
$preselectedSiteAccess = reset($siteAccessesList);
}

$urlValue = $this->generateUrl(
'ibexa.version.preview',
[
'contentId' => $content->id,
'versionNo' => $versionNo ?? $content->getVersionInfo()->versionNo,
'language' => $languageCode,
'siteAccessName' => $preselectedSiteAccess,
]
);

$siteAccessSelector = $this->baseFormFactory->create(
SiteAccessChoiceType::class,
$urlValue,
[
'location' => $location,
'content' => $content,
'versionNo' => $versionNo ?? $content->getVersionInfo()->versionNo,
'languageCode' => $languageCode,
]
);

return $this->render('@ibexadesign/content/content_preview.html.twig', [
'location' => $location,
'content' => $content,
'language_code' => $languageCode,
'siteaccesses' => $siteAccessesList,
'site_access_form' => $siteAccessSelector->createView(),
'version_no' => $versionNo ?? $content->getVersionInfo()->versionNo,
'preselected_site_access' => $preselectedSiteAccess,
'referrer' => $referrer ?? 'content_draft_edit',
Expand Down
5 changes: 5 additions & 0 deletions src/bundle/Resources/config/services/forms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,8 @@ services:
Ibexa\AdminUi\Form\Provider\GroupedNonMetaFormFieldsProvider:
arguments:
$innerGroupedContentFormFieldsProvider: '@Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface'

Ibexa\AdminUi\Form\Type\Preview\SiteAccessChoiceType:
arguments:
$siteAccessResolver: '@Ibexa\AdminUi\Siteaccess\NonAdminSiteaccessResolver'
$siteAccessNameGenerator: '@Ibexa\AdminUi\Siteaccess\SiteAccessNameGenerator'
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

{% block left_sidebar %}{% endblock %}

{% form_theme site_access_form '@ibexadesign/ui/form_fields.html.twig' %}

{% block header_row %}
<div class="ibexa-preview-header">
<div class="ibexa-preview-header__item ibexa-preview-header__item--back">
Expand Down Expand Up @@ -37,41 +39,7 @@
</a>
</div>
<div class="ibexa-preview-header__item ibexa-preview-header__item--siteaccess">
{% set value = '' %}
{% set choices = siteaccesses|map((site_access_label, site_access_name) => {
value: path(
'ibexa.version.preview',
{
'contentId': content.id,
'versionNo': version_no,
'language': language_code,
'siteAccessName': site_access_name
}
),
label: site_access_label|trans({}, 'ezplatform_siteaccess'),
is_selected: site_access_name == preselected_site_access,
}) %}
{% set source %}
<select class="form-control ibexa-input">
{% for choice in choices %}
{% if choice.is_selected %}
{% set value = choice.value %}
{% endif %}
<option
value="{{ choice.value }}"
{% if choice.is_selected %}selected{% endif %}
>
{{ choice.label }}
</option>
{% endfor %}
</select>
{% endset %}

{% include '@ibexadesign/ui/component/dropdown/dropdown.html.twig' with {
source: source,
choices: choices,
value: value,
} %}
{{ form(site_access_form) }}
</div>
<div class="ibexa-preview-header__item ibexa-preview-header__item--actions">
<button data-preview-mode="desktop" class="btn ibexa-btn ibexa-btn--no-text ibexa-btn--selector ibexa-btn--selected ibexa-preview-header__action">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Form\Type\ChoiceList\Loader;

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class SiteAccessPreviewChoiceLoader extends BaseChoiceLoader
{
private SiteAccessChoiceLoader $siteAccessChoiceLoader;

private UrlGeneratorInterface $urlGenerator;

private int $contentId;

private string $languageCode;

private int $versionNo;

public function __construct(
SiteAccessChoiceLoader $siteAccessChoiceLoader,
UrlGeneratorInterface $urlGenerator,
int $contentId,
string $languageCode,
int $versionNo
) {
$this->siteAccessChoiceLoader = $siteAccessChoiceLoader;
$this->urlGenerator = $urlGenerator;
$this->contentId = $contentId;
$this->languageCode = $languageCode;
$this->versionNo = $versionNo;
}

/**
* @return array<string, string>
*/
public function getChoiceList(): array
{
$baseChoiceList = $this->siteAccessChoiceLoader->getChoiceList();

$choiceList = [];
foreach ($baseChoiceList as $siteAccessKey => $siteAccessName) {
$choiceList[$siteAccessKey] = $this->urlGenerator->generate(
'ibexa.version.preview',
[
'contentId' => $this->contentId,
'versionNo' => $this->versionNo,
'language' => $this->languageCode,
'siteAccessName' => $siteAccessKey,
]
);
}

return $choiceList;
}
}
87 changes: 87 additions & 0 deletions src/lib/Form/Type/Preview/SiteAccessChoiceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Form\Type\Preview;

use Ibexa\AdminUi\Form\Type\ChoiceList\Loader\SiteAccessChoiceLoader;
use Ibexa\AdminUi\Form\Type\ChoiceList\Loader\SiteAccessPreviewChoiceLoader;
use Ibexa\AdminUi\Siteaccess\SiteAccessNameGeneratorInterface;
use Ibexa\AdminUi\Siteaccess\SiteaccessResolverInterface;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class SiteAccessChoiceType extends AbstractType
{
private SiteaccessResolverInterface $siteAccessResolver;

private SiteAccessNameGeneratorInterface $siteAccessNameGenerator;

private UrlGeneratorInterface $urlGenerator;

public function __construct(
SiteaccessResolverInterface $siteAccessResolver,
SiteAccessNameGeneratorInterface $siteAccessNameGenerator,
UrlGeneratorInterface $urlGenerator
) {
$this->siteAccessResolver = $siteAccessResolver;
$this->siteAccessNameGenerator = $siteAccessNameGenerator;
$this->urlGenerator = $urlGenerator;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'choice_loader' => function (Options $options): ChoiceLoaderInterface {
return ChoiceList::loader(
$this,
new SiteAccessPreviewChoiceLoader(
new SiteAccessChoiceLoader(
$this->siteAccessResolver,
$this->siteAccessNameGenerator,
$options['location']
),
$this->urlGenerator,
$options['content']->id,
$options['languageCode'],
$options['versionNo'],
),
[
$options['location'],
$options['content']->id,
$options['languageCode'],
$options['versionNo'],
]
);
},
]);

$resolver->setRequired([
'location',
'content',
'versionNo',
'languageCode',
]);
$resolver->setAllowedTypes('location', Location::class);
$resolver->setAllowedTypes('content', Content::class);
$resolver->setAllowedTypes('versionNo', 'integer');
$resolver->setAllowedTypes('languageCode', 'string');
}

public function getParent(): ?string
{
return ChoiceType::class;
}
}
Loading