Skip to content

Add ability to override routing with record_route #1484

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

Merged
merged 5 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
67 changes: 21 additions & 46 deletions src/Canonical.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
use Tightenco\Collect\Support\Collection;

class Canonical
{
Expand All @@ -37,18 +35,14 @@ class Canonical
/** @var string */
private $path = null;

/** @var RouterInterface */
private $router;

/** @var string */
private $defaultLocale;

public function __construct(Config $config, UrlGeneratorInterface $urlGenerator, RequestStack $requestStack, RouterInterface $router, string $defaultLocale)
public function __construct(Config $config, UrlGeneratorInterface $urlGenerator, RequestStack $requestStack, string $defaultLocale)
{
$this->config = $config;
$this->urlGenerator = $urlGenerator;
$this->request = $requestStack->getCurrentRequest();
$this->router = $router;
$this->defaultLocale = $defaultLocale;

$this->init();
Expand Down Expand Up @@ -150,13 +144,8 @@ public function getPath(): string
if ($this->path === null) {
$route = $this->request->attributes->get('_route');
$params = $this->request->attributes->get('_route_params');
$canonicalRoute = $this->getCanonicalRoute($route, $params);

$this->path = $this->urlGenerator->generate(
$canonicalRoute,
$params,
UrlGeneratorInterface::ABSOLUTE_PATH
);
$this->path = $this->generateLink($route, $params, false);
}

return $this->path;
Expand All @@ -170,46 +159,32 @@ public function setPath(?string $route = null, array $params = []): void
$route = $this->request->attributes->get('_route');
}

$canonicalRoute = $this->getCanonicalRoute($route, $params);

try {
$this->path = $this->urlGenerator->generate(
$canonicalRoute,
$params
);
$this->path = $this->generateLink($route, $params, false);
} catch (InvalidParameterException | MissingMandatoryParametersException $e) {
// Just use the current URL /shrug
$this->request->getUri();
$this->path = $this->request->getUri();
}
}

public function getCanonicalRoute(string $route, array &$params = []): string
public function generateLink(string $route, array $params, $canonical = false): ?string
{
$routes = new Collection($this->router->getRouteCollection()->getIterator());
$currentController = $routes->get($route)->getDefault('_controller');

$routes = collect($routes->filter(function (Route $route) use ($currentController) {
return $route->getDefault('_controller') === $currentController;
})->keys());

// If only one route matched, return that.
if ($routes->count() === 1) {
return $routes->first();
if (isset($params['_locale']) && $params['_locale'] === $this->defaultLocale) {
unset($params['_locale']);
$routeWithoutLocale = str_replace('_locale', '', $route);

// If a route without the locale exists, use that. e.g. record_locale -> record
try {
$this->generateLink($routeWithoutLocale, $params);
$route = $routeWithoutLocale;
} catch (RouteNotFoundException $e) {
}
}

// If no locale or locale is not default, get the first route which is named *_locale
if (array_key_exists('_locale', $params) && $params['_locale'] !== $this->defaultLocale) {
return $routes->filter(function (string $name) {
return fnmatch('*locale', $name);
})->first();
}

// Unset _locale so that it is not passed as query param to url.
unset($params['_locale']);

// Otherwise, get the first route that is not *_locale
return $routes->filter(function (string $name) {
return ! fnmatch('*locale', $name);
})->first();
return $this->urlGenerator->generate(
$route,
$params,
$canonical ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH
);
}
}
3 changes: 3 additions & 0 deletions src/Configuration/Parser/ContentTypesParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ protected function parseContentType($key, array $contentType): ?ContentType
if (! isset($contentType['listing_template'])) {
$contentType['listing_template'] = $contentType['slug'] . '.twig';
}
if (! isset($contentType['record_route'])) {
$contentType['record_route'] = isset($contentType['locales']) ? 'record_locale' : 'record';
}

if ($contentType['singleton']) {
$contentType['listing_records'] = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/ContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function findOneBySlug(string $slug, ?ContentType $contentType = null): ?
->andWhere($where . ' = :slug')
->setParameter('slug', $slug);

if ($contentType) {
if ($contentType && $contentType->get('slug')) {
$query->andWhere('content.contentType = :ct')
->setParameter('ct', $contentType->get('slug'));
}
Expand Down
10 changes: 2 additions & 8 deletions src/Twig/ContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public function getLink(Content $content, bool $canonical = false, string $local
'contentTypeSlug' => $content->getContentTypeSingularSlug(),
];

return $this->generateLink('record_locale', $params, $canonical);
return $this->generateLink($content->getDefinition()->get('record_route'), $params, $canonical);
}

public function getEditLink(Content $content): ?string
Expand Down Expand Up @@ -396,14 +396,8 @@ public function getStatusLink(Content $content, bool $absolute = false): ?string

private function generateLink(string $route, array $params, $canonical = false): string
{
$canonicalRoute = $this->canonical->getCanonicalRoute($route, $params);

try {
$link = $this->urlGenerator->generate(
$canonicalRoute,
$params,
$canonical ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH
);
$link = $this->canonical->generateLink($route, $params, $canonical);
} catch (InvalidParameterException $e) {
$this->logger->notice('Could not create URL for route \'' . $route . '\'. Perhaps the ContentType was changed or removed. Try clearing the cache');
$link = '';
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Configuration/Parser/ContentTypesParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ContentTypesParserTest extends ParserTestBase
{
public const NUMBER_OF_CONTENT_TYPES_IN_MINIMAL_FILE = 2;

public const AMOUNT_OF_ATTRIBUTES_IN_CONTENT_TYPE = 24;
public const AMOUNT_OF_ATTRIBUTES_IN_CONTENT_TYPE = 25;

public const AMOUNT_OF_ATTRIBUTES_IN_FIELD = 24;

Expand Down