Skip to content

Commit

Permalink
inject and use UrlBuilder instead of using permalink to work with slu…
Browse files Browse the repository at this point in the history
…g change too
  • Loading branch information
Leny Bernard committed Jun 3, 2019
1 parent e89e00f commit 0060d36
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Bundle/CoreBundle/Entity/WebViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ public function isPublished();
public function isHomepage();

public function setHomepage($homepage);

public function getPermalink();

public function setPermalink($permalink);
}
10 changes: 7 additions & 3 deletions Bundle/CoreBundle/Helper/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ public function buildUrl(WebViewInterface $view)
if (!(method_exists($view, 'isHomepage') && $view->isHomepage())) {
$slug = [$view->getSlug()];
}

//get the slug of the parents
$url = $this->getParentSlugs($view, $slug);
// if permalink is set
if ($view->getPermalink() != null && $view->getPermalink() != '') {
$url = [$view->getPermalink()];
} else {
//get the slug of the parents
$url = $this->getParentSlugs($view, $slug);
}

//reorder the list of slugs
$url = array_reverse($url);
Expand Down
1 change: 1 addition & 0 deletions Bundle/CoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ services:
arguments:
- "@victoire_view_reference.repository"
- "@request_stack"
- "@victoire_core.url_builder"
tags:
- { name: 'validator.constraint_validator', alias: 'victoire.validator.view.permalink_unique' }

Expand Down
13 changes: 8 additions & 5 deletions Bundle/CoreBundle/Validator/UniquePermalinkValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Victoire\Bundle\CoreBundle\Helper\UrlBuilder;
use Victoire\Bundle\ViewReferenceBundle\Connector\ViewReferenceRepository;

class UniquePermalinkValidator extends ConstraintValidator
{
private $viewReferenceRepository;
private $requestStack;
private $urlBuilder;

/**
* PermalinkValidator constructor.
*
* UniquePermalinkValidator constructor.
* @param ViewReferenceRepository $viewReferenceRepository
* @param RequestStack $requestStack
* @param RequestStack $requestStack
* @param UrlBuilder $urlBuilder
*/
public function __construct(ViewReferenceRepository $viewReferenceRepository, RequestStack $requestStack)
public function __construct(ViewReferenceRepository $viewReferenceRepository, RequestStack $requestStack, UrlBuilder $urlBuilder)
{
$this->viewReferenceRepository = $viewReferenceRepository;
$this->requestStack = $requestStack;
$this->urlBuilder = $urlBuilder;
}

/**
Expand All @@ -31,7 +34,7 @@ public function __construct(ViewReferenceRepository $viewReferenceRepository, Re
public function validate($view, Constraint $constraint)
{
$viewReference = $this->viewReferenceRepository->getReferenceByUrl(
$view->getPermalink(),
$this->urlBuilder->buildUrl($view),
$this->requestStack->getCurrentRequest()->getLocale()
);

Expand Down
4 changes: 2 additions & 2 deletions Tests/Features/Page/create.feature
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ Feature: Create a page
And I submit the widget
And I wait 5 seconds
Then the url should match "/en/anakin-skywalker/luke-skywalker"
And I should see "The permalink is already in use"
And I should see "The url is already in use"
When I fill in "page_settings_translations_en_permalink" with "contact"
And I submit the widget
And I wait 5 seconds
Then the url should match "/en/anakin-skywalker/luke-skywalker"
And I should see "The permalink is already in use"
And I should see "The url is already in use"
Then I fill in "page_settings_translations_en_permalink" with "amazing-luke-skywalker"
And I submit the widget
And I wait 5 seconds
Expand Down

0 comments on commit 0060d36

Please sign in to comment.