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

EZP-32157: Added option to choose SA when creating custom alias #1583

Merged
merged 12 commits into from
Nov 12, 2020
15 changes: 15 additions & 0 deletions src/bundle/Resources/translations/content_url.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@
<target state="new">Unchecked will not redirect to the destination and the URL will stay the same.</target>
<note>key: tab.urls.add.redirect.helper.unchecked</note>
</trans-unit>
<trans-unit id="054be8f2dbbd3e40aba0f00c5c29496b13099895" resname="tab.urls.add.root_location_id.helper">
<source>Will only work when 'Place at the site root' is enabled.</source>
<target state="new">Will only work when 'Place at the site root' is enabled.</target>
<note>key: tab.urls.add.root_location_id.helper</note>
</trans-unit>
<trans-unit id="c1277a6823f4d281355e706e54cd723b5ed4adf7" resname="tab.urls.add.root_location_id.helper_secondary">
<source>If you leave this empty, the alias will be placed at main root Location.</source>
<target state="new">If you leave this empty, the alias will be placed at main root Location.</target>
<note>key: tab.urls.add.root_location_id.helper_secondary</note>
</trans-unit>
<trans-unit id="f933f01aef7b171904b5c0b1ed4327413828f96f" resname="tab.urls.add.site_access">
<source>SiteAccess</source>
<target state="new">SiteAccess</target>
<note>key: tab.urls.add.site_access</note>
</trans-unit>
<trans-unit id="9ef990fca45f612ab2d3af7b21968803d76d5d77" resname="tab.urls.add.site_root">
<source>Place alias at the site root</source>
<target state="new">Place alias at the site root</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
{{ 'tab.urls.add.site_root.helper.no_parent_name'|trans|desc('Unchecked will create the new alias under the parent of this location') }}
{% endif %}
</div>

<span class="ez-modal--custom-url-alias__label">{{ 'tab.urls.add.site_access'|trans|desc('SiteAccess') }}</span>
{{ form_widget(form.root_location_id) }}
<div class="ez-modal--custom-url-alias__info-text">{{ 'tab.urls.add.root_location_id.helper'|trans|desc("Will only work when 'Place at the site root' is enabled.") }}</div>
<div class="ez-modal--custom-url-alias__info-text">
{{ 'tab.urls.add.root_location_id.helper_secondary'|trans|desc("If you leave this empty, the alias will be placed at main root Location.") }}
</div>
</div>
<div class="modal-footer justify-content-center">
<button type="button" class="btn btn-dark" data-dismiss="modal">
Expand Down
19 changes: 18 additions & 1 deletion src/lib/Form/Data/Content/CustomUrl/CustomUrlAddData.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,23 @@ class CustomUrlAddData
/** @var bool */
private $siteRoot;

/** @var int|null */
private $rootLocationId;

public function __construct(
?Location $location = null,
?string $path = null,
?Language $language = null,
bool $redirect = true,
bool $siteRoot = true
bool $siteRoot = true,
?int $rootLocationId = null
) {
$this->location = $location;
$this->path = $path;
$this->language = $language;
$this->redirect = $redirect;
$this->siteRoot = $siteRoot;
$this->rootLocationId = $rootLocationId;
}

/**
Expand Down Expand Up @@ -141,4 +146,16 @@ public function setSiteRoot(bool $siteRoot): self

return $this;
}

public function getRootLocationId(): ?int
{
return $this->rootLocationId;
}

public function setRootLocationId(?int $rootLocationId): self
{
$this->rootLocationId = $rootLocationId;

return $this;
}
}
15 changes: 12 additions & 3 deletions src/lib/Form/EventListener/BuildPathFromRootListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,19 @@ public function onPreSubmitData(FormEvent $event)
if (1 >= $location->depth) {
return;
}
$parentLocation = $this->locationService->loadLocation($location->parentLocationId);
$urlAlias = $this->urlAliasService->reverseLookup($parentLocation);
$data['path'] = $urlAlias->path . '/' . $data['path'];
$data['path'] = $this->createPathBasedOnParentLocation($location->parentLocationId, $data['path']);
$event->setData($data);
} elseif (isset($data['site_root']) && true === (bool)$data['site_root'] && !empty($data['root_location_id'])) {
$data['path'] = $this->createPathBasedOnParentLocation((int)$data['root_location_id'], $data['path']);
$event->setData($data);
}
}

private function createPathBasedOnParentLocation(int $locationId, string $path): string
{
$parentLocation = $this->locationService->loadLocation($locationId);
$urlAlias = $this->urlAliasService->reverseLookup($parentLocation);

return $urlAlias->path . '/' . $path;
}
}
101 changes: 101 additions & 0 deletions src/lib/Form/Type/ChoiceList/Loader/SiteAccessChoiceLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

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

namespace EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Loader;

use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use EzSystems\EzPlatformAdminUi\Siteaccess\NonAdminSiteaccessResolver;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;

class SiteAccessChoiceLoader implements ChoiceLoaderInterface
Steveb-p marked this conversation as resolved.
Show resolved Hide resolved
{
/** @var \EzSystems\EzPlatformAdminUi\Siteaccess\NonAdminSiteaccessResolver */
private $nonAdminSiteaccessResolver;

/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
private $configResolver;

/** @var \eZ\Publish\API\Repository\Values\Content\Location|null */
private $location;

public function __construct(
NonAdminSiteaccessResolver $nonAdminSiteaccessResolver,
ConfigResolverInterface $configResolver,
?Location $location = null
) {
$this->nonAdminSiteaccessResolver = $nonAdminSiteaccessResolver;
$this->configResolver = $configResolver;
$this->location = $location;
}

/**
* Provides data in format:
* [
* site_access => location_id,
* ...
* ].
*
* @return int[]
*/
public function getChoiceList(): array
{
$siteAccesses = $this->location === null
? $this->nonAdminSiteaccessResolver->getSiteaccesses()
: $this->nonAdminSiteaccessResolver->getSiteaccessesForLocation($this->location);

$parameterName = 'content.tree_root.location_id';
$defaultTreeRootLocationId = $this->configResolver->getParameter($parameterName);

$data = [];
foreach ($siteAccesses as $siteAccess) {
$treeRootLocationId = $this->configResolver->hasParameter($parameterName, null, $siteAccess)
? $this->configResolver->getParameter($parameterName, null, $siteAccess)
: $defaultTreeRootLocationId;

$data[$siteAccess] = $treeRootLocationId;
}

return $data;
}

public function loadChoiceList($value = null)
{
$choices = $this->getChoiceList();

return new ArrayChoiceList($choices, $value);
}

public function loadChoicesForValues(array $values, $value = null)
{
// Optimize
$values = array_filter($values);
if (empty($values)) {
return [];
}

return $this->loadChoiceList($value)->getChoicesForValues($values);
}

public function loadValuesForChoices(array $choices, $value = null)
{
// Optimize
$choices = array_filter($choices);
if (empty($choices)) {
return [];
}

// If no callable is set, choices are the same as values
if (null === $value) {
return $choices;
}

return $this->loadChoiceList($value)->getValuesForChoices($choices);
}
}
35 changes: 28 additions & 7 deletions src/lib/Form/Type/Content/CustomUrl/CustomUrlAddType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
namespace EzSystems\EzPlatformAdminUi\Form\Type\Content\CustomUrl;

use eZ\Publish\API\Repository\LanguageService;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use EzSystems\EzPlatformAdminUi\Form\EventListener\AddLanguageFieldBasedOnContentListener;
use EzSystems\EzPlatformAdminUi\Form\EventListener\BuildPathFromRootListener;
use EzSystems\EzPlatformAdminUi\Form\EventListener\DisableSiteRootCheckboxIfRootLocationListener;
use EzSystems\EzPlatformAdminUi\Form\Type\ChoiceList\Loader\SiteAccessChoiceLoader;
use EzSystems\EzPlatformAdminUi\Form\Type\Content\LocationType;
use EzSystems\EzPlatformAdminUi\Siteaccess\NonAdminSiteaccessResolver;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
Expand All @@ -36,22 +39,26 @@ class CustomUrlAddType extends AbstractType
/** @var \EzSystems\EzPlatformAdminUi\Form\EventListener\DisableSiteRootCheckboxIfRootLocationListener */
private $checkboxIfRootLocationListener;

/**
* @param \eZ\Publish\API\Repository\LanguageService $languageService
* @param \EzSystems\EzPlatformAdminUi\Form\EventListener\AddLanguageFieldBasedOnContentListener $addLanguageFieldBasedOnContentListener
* @param \EzSystems\EzPlatformAdminUi\Form\EventListener\BuildPathFromRootListener $buildPathFromRootListener
* @param \EzSystems\EzPlatformAdminUi\Form\EventListener\DisableSiteRootCheckboxIfRootLocationListener $checkboxIfRootLocationListener
*/
/** @var \EzSystems\EzPlatformAdminUi\Siteaccess\NonAdminSiteaccessResolver */
private $nonAdminSiteaccessResolver;

/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
private $configResolver;

public function __construct(
LanguageService $languageService,
AddLanguageFieldBasedOnContentListener $addLanguageFieldBasedOnContentListener,
BuildPathFromRootListener $buildPathFromRootListener,
DisableSiteRootCheckboxIfRootLocationListener $checkboxIfRootLocationListener
DisableSiteRootCheckboxIfRootLocationListener $checkboxIfRootLocationListener,
NonAdminSiteaccessResolver $nonAdminSiteaccessResolver,
ConfigResolverInterface $configResolver
) {
$this->languageService = $languageService;
$this->addLanguageFieldBasedOnContentListener = $addLanguageFieldBasedOnContentListener;
$this->buildPathFromRootListener = $buildPathFromRootListener;
$this->checkboxIfRootLocationListener = $checkboxIfRootLocationListener;
$this->nonAdminSiteaccessResolver = $nonAdminSiteaccessResolver;
$this->configResolver = $configResolver;
}

/**
Expand All @@ -60,6 +67,8 @@ public function __construct(
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$location = $options['data']->getLocation();

$builder
->add(
'location',
Expand Down Expand Up @@ -97,6 +106,18 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'label' => false,
]
)
->add(
'root_location_id',
ChoiceType::class,
[
'required' => false,
'choice_loader' => new SiteAccessChoiceLoader(
$this->nonAdminSiteaccessResolver,
$this->configResolver,
$location
),
]
)
->add(
'add',
SubmitType::class,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Siteaccess/AdminSiteaccessPreviewVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function getRootLocationIds(string $siteaccess): array
{
$locationIds = [];
$locationIds[] = $this->configResolver->getParameter(
'location_ids.content_structure',
'content.tree_root.location_id',
null,
$siteaccess
);
Expand Down
32 changes: 32 additions & 0 deletions src/lib/Tests/Form/Data/Content/CustomUrl/CustomUrlAddDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

namespace EzSystems\EzPlatformAdminUi\Tests\Form\Data\Content\CustomUrl;

use eZ\Publish\API\Repository\Values\Content\Language;
use eZ\Publish\Core\Repository\Values\Content\Location;
use EzSystems\EzPlatformAdminUi\Form\Data\Content\CustomUrl\CustomUrlAddData;
use PHPUnit\Framework\TestCase;

class CustomUrlAddDataTest extends TestCase
{
public function testConstruct(): void
{
$location = new Location(['id' => 2]);
$language = new Language(['languageCode' => 'eng-GB']);
$path = '/test';
$rootLocationId = 52;

$data = new CustomUrlAddData($location, $path, $language, false, true, $rootLocationId);

$this->assertSame($location, $data->getLocation());
$this->assertSame($language, $data->getLanguage());
$this->assertSame($path, $data->getPath());
$this->assertSame($rootLocationId, $data->getRootLocationId());
}
}