Skip to content

Commit

Permalink
Merge branch '1.5' into 2.1
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/bundle/Resources/public/js/scripts/admin.location.add.custom_url.js
  • Loading branch information
barw4 committed Nov 12, 2020
2 parents 4302b15 + 44f2129 commit 23d4206
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const submitBtn = modal.querySelector('[type="submit"]');
const input = modal.querySelector('[required="required"]');
const checkboxes = modal.querySelectorAll('.ez-field-edit--ezboolean input');
const siteRootCheckbox = modal.querySelector('[name="custom_url_add[site_root]"]');
const toggleButtonState = () => {
const hasValue = input.value.trim().length !== 0;
const methodName = hasValue ? 'removeAttribute' : 'setAttribute';
Expand All @@ -22,8 +23,16 @@
input.value = '';
toggleButtonState();
};
const toggleSiteAccessSelect = (event) => {
const isChecked = event.target.checked;
const siteAccessSelect = modal.querySelector('[name="custom_url_add[site_access]"]');
const methodName = isChecked ? 'removeAttribute' : 'setAttribute';

siteAccessSelect[methodName]('disabled', true);
};

input.addEventListener('input', toggleButtonState, false);
siteRootCheckbox.addEventListener('change', toggleSiteAccessSelect, false);
checkboxes.forEach((checkbox) => checkbox.addEventListener('change', toggleCheckbox, false));
discardBtns.forEach((btn) => btn.addEventListener('click', clearValues, false));
}
Expand Down
10 changes: 10 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,16 @@
<target state="new">When unchecked, the alias 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="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 at the site root</source>
<target state="new">Place at the site root</target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
{{ 'tab.urls.add.site_root.helper.no_parent_name'|trans|desc('When unchecked, the alias will be placed 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.site_access) }}
<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 string|null */
private $siteAccess;

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

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

return $this;
}

public function getSiteAccess(): ?string
{
return $this->siteAccess;
}

public function setSiteAccess(?string $siteAccess): self
{
$this->siteAccess = $siteAccess;

return $this;
}
}
37 changes: 28 additions & 9 deletions src/lib/Form/EventListener/BuildPathFromRootListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\URLAliasService;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use Symfony\Component\Form\FormEvent;

class BuildPathFromRootListener
Expand All @@ -20,14 +21,17 @@ class BuildPathFromRootListener
/** @var \eZ\Publish\API\Repository\URLAliasService */
private $urlAliasService;

/**
* @param \eZ\Publish\API\Repository\LocationService $locationService
* @param \eZ\Publish\API\Repository\URLAliasService $urlAliasService
*/
public function __construct(LocationService $locationService, URLAliasService $urlAliasService)
{
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
private $configResolver;

public function __construct(
LocationService $locationService,
URLAliasService $urlAliasService,
ConfigResolverInterface $configResolver
) {
$this->locationService = $locationService;
$this->urlAliasService = $urlAliasService;
$this->configResolver = $configResolver;
}

/**
Expand All @@ -44,10 +48,25 @@ 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['site_access'])) {
$parameterName = 'content.tree_root.location_id';
$defaultTreeRootLocationId = $this->configResolver->getParameter($parameterName);
$treeRootLocationId = $this->configResolver->hasParameter($parameterName, null, $data['site_access'])
? $this->configResolver->getParameter($parameterName, null, $data['site_access'])
: $defaultTreeRootLocationId;

$data['path'] = $this->createPathBasedOnParentLocation((int)$treeRootLocationId, $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;
}
}
88 changes: 88 additions & 0 deletions src/lib/Form/Type/ChoiceList/Loader/SiteAccessChoiceLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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 EzSystems\EzPlatformAdminUi\Siteaccess\NonAdminSiteaccessResolver;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;

class SiteAccessChoiceLoader implements ChoiceLoaderInterface
{
/** @var \EzSystems\EzPlatformAdminUi\Siteaccess\NonAdminSiteaccessResolver */
private $nonAdminSiteaccessResolver;

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

public function __construct(
NonAdminSiteaccessResolver $nonAdminSiteaccessResolver,
?Location $location = null
) {
$this->nonAdminSiteaccessResolver = $nonAdminSiteaccessResolver;
$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);

$data = [];
foreach ($siteAccesses as $siteAccess) {
$data[$siteAccess] = $siteAccess;
}

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);
}
}
28 changes: 21 additions & 7 deletions src/lib/Form/Type/Content/CustomUrl/CustomUrlAddType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
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 +38,21 @@ 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;

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

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

$builder
->add(
'location',
Expand Down Expand Up @@ -97,6 +100,17 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'label' => false,
]
)
->add(
'site_access',
ChoiceType::class,
[
'required' => false,
'choice_loader' => new SiteAccessChoiceLoader(
$this->nonAdminSiteaccessResolver,
$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';
$siteAccess = 'site3';

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

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

0 comments on commit 23d4206

Please sign in to comment.