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 with 'place at the site root' option.</source>
barw4 marked this conversation as resolved.
Show resolved Hide resolved
<target state="new">Will only work with 'place at the site root' option.</target>
barw4 marked this conversation as resolved.
Show resolved Hide resolved
<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>Leaving this choice empty will result in alias being placed at main root location.</source>
barw4 marked this conversation as resolved.
Show resolved Hide resolved
<target state="new">Leaving this choice empty will result in alias being placed at main root location.</target>
barw4 marked this conversation as resolved.
Show resolved Hide resolved
<note>key: tab.urls.add.root_location_id.helper_secondary</note>
</trans-unit>
<trans-unit id="f933f01aef7b171904b5c0b1ed4327413828f96f" resname="tab.urls.add.site_access">
<source>Site access</source>
barw4 marked this conversation as resolved.
Show resolved Hide resolved
<target state="new">Site access</target>
barw4 marked this conversation as resolved.
Show resolved Hide resolved
<note>key: tab.urls.add.site_access</note>
</trans-unit>
alongosz marked this conversation as resolved.
Show resolved Hide resolved
<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('Site access') }}</span>
barw4 marked this conversation as resolved.
Show resolved Hide resolved
{{ 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 with 'place at the site root' option.") }}</div>
barw4 marked this conversation as resolved.
Show resolved Hide resolved
<div class="ez-modal--custom-url-alias__info-text">
{{ 'tab.urls.add.root_location_id.helper_secondary'|trans|desc("Leaving this choice empty will result in alias being placed at main root location.") }}
barw4 marked this conversation as resolved.
Show resolved Hide resolved
</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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?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\EventListener;

use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use EzSystems\EzPlatformAdminUi\Form\Data\Content\CustomUrl\CustomUrlAddData;
use EzSystems\EzPlatformAdminUi\Siteaccess\NonAdminSiteaccessResolver;
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormEvent;

final class AddSiteAccessFieldBasedOnContentListener
{
/** @var \EzSystems\EzPlatformAdminUi\Siteaccess\NonAdminSiteaccessResolver */
private $nonAdminSiteaccessResolver;

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

/**
* @param \EzSystems\EzPlatformAdminUi\Siteaccess\NonAdminSiteaccessResolver $nonAdminSiteaccessResolver
* @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver
*/
alongosz marked this conversation as resolved.
Show resolved Hide resolved
public function __construct(
NonAdminSiteaccessResolver $nonAdminSiteaccessResolver,
ConfigResolverInterface $configResolver
) {
$this->nonAdminSiteaccessResolver = $nonAdminSiteaccessResolver;
$this->configResolver = $configResolver;
}

public function onPreSetData(FormEvent $event): void
{
/** @var CustomUrlAddData $data */
barw4 marked this conversation as resolved.
Show resolved Hide resolved
$data = $event->getData();
$location = $data->getLocation();

$form = $event->getForm();

$form->add(
'root_location_id',
ChoiceType::class,
[
'required' => false,
'choice_loader' => new CallbackChoiceLoader($this->getCallableData($location)),
barw4 marked this conversation as resolved.
Show resolved Hide resolved
]
);
}

private function getCallableData(?Location $location): callable
{
return function () use ($location): array {
return $this->loadSiteAccesses($location);
};
}

/**
* Provides data in format:
* [
* site_access => location_id,
* ...
* ].
*
* @return int[]
*/
private function loadSiteAccesses(?Location $location): array
barw4 marked this conversation as resolved.
Show resolved Hide resolved
{
$siteAccesses = $location instanceof Location
barw4 marked this conversation as resolved.
Show resolved Hide resolved
? $this->nonAdminSiteaccessResolver->getSiteaccessesForLocation($location)
: $this->nonAdminSiteaccessResolver->getSiteaccesses();

$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;
}
}
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;
}
}
21 changes: 20 additions & 1 deletion src/lib/Form/Type/Content/CustomUrl/CustomUrlAddType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use eZ\Publish\API\Repository\LanguageService;
use EzSystems\EzPlatformAdminUi\Form\EventListener\AddLanguageFieldBasedOnContentListener;
use EzSystems\EzPlatformAdminUi\Form\EventListener\AddSiteAccessFieldBasedOnContentListener;
use EzSystems\EzPlatformAdminUi\Form\EventListener\BuildPathFromRootListener;
use EzSystems\EzPlatformAdminUi\Form\EventListener\DisableSiteRootCheckboxIfRootLocationListener;
use EzSystems\EzPlatformAdminUi\Form\Type\Content\LocationType;
Expand All @@ -36,22 +37,28 @@ class CustomUrlAddType extends AbstractType
/** @var \EzSystems\EzPlatformAdminUi\Form\EventListener\DisableSiteRootCheckboxIfRootLocationListener */
private $checkboxIfRootLocationListener;

/** @var \EzSystems\EzPlatformAdminUi\Form\EventListener\AddSiteAccessFieldBasedOnContentListener */
private $addSiteAccessFieldBasedOnContentListener;

/**
* @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
* @param \EzSystems\EzPlatformAdminUi\Form\EventListener\AddSiteAccessFieldBasedOnContentListener $addSiteAccessFieldBasedOnContentListener
*/
public function __construct(
LanguageService $languageService,
AddLanguageFieldBasedOnContentListener $addLanguageFieldBasedOnContentListener,
BuildPathFromRootListener $buildPathFromRootListener,
DisableSiteRootCheckboxIfRootLocationListener $checkboxIfRootLocationListener
DisableSiteRootCheckboxIfRootLocationListener $checkboxIfRootLocationListener,
AddSiteAccessFieldBasedOnContentListener $addSiteAccessFieldBasedOnContentListener
) {
$this->languageService = $languageService;
$this->addLanguageFieldBasedOnContentListener = $addLanguageFieldBasedOnContentListener;
$this->buildPathFromRootListener = $buildPathFromRootListener;
$this->checkboxIfRootLocationListener = $checkboxIfRootLocationListener;
$this->addSiteAccessFieldBasedOnContentListener = $addSiteAccessFieldBasedOnContentListener;
}

/**
Expand Down Expand Up @@ -97,6 +104,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'label' => false,
]
)
->add(
'root_location_id',
ChoiceType::class,
[
'required' => false,
'choices' => [],
]
)
->add(
'add',
SubmitType::class,
Expand All @@ -109,6 +124,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$this->addLanguageFieldBasedOnContentListener,
'onPreSetData',
]);
$builder->addEventListener(FormEvents::PRE_SET_DATA, [
$this->addSiteAccessFieldBasedOnContentListener,
'onPreSetData',
]);
$builder->addEventListener(FormEvents::PRE_SUBMIT, [
$this->buildPathFromRootListener,
'onPreSubmitData',
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