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

Fix drupal_set_title() changing breadcrumb titles #100

Merged
merged 3 commits into from
Nov 23, 2023
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
14 changes: 10 additions & 4 deletions src/Controller/RetrofitTitleResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,32 @@

use Drupal\Core\Controller\TitleResolverInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Route;

final class RetrofitTitleResolver implements TitleResolverInterface
{
private string $storedTitle = '';
/**
* @var \SplObjectStorage<Request, string>
*/
private $titles;

public function __construct(
private readonly TitleResolverInterface $inner,
private readonly RequestStack $requestStack,
) {
$this->titles = new \SplObjectStorage();
}

public function setStoredTitle(string $title): void
{
$this->storedTitle = $title;
$this->titles[$this->requestStack->getCurrentRequest()] = $title;
}

public function getTitle(Request $request, Route $route): array|string|\Stringable|null
{
if ($this->storedTitle !== '') {
return $this->storedTitle;
if (isset($this->titles[$request])) {
return $this->titles[$request];
}
return $this->inner->getTitle($request, $route);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ public function register(ContainerBuilder $container)

$container->register(RetrofitTitleResolver::class)
->setDecoratedService('title_resolver')
->addArgument(new Reference(RetrofitTitleResolver::class . '.inner'));
->addArgument(new Reference(RetrofitTitleResolver::class . '.inner'))
->addArgument(new Reference('request_stack'));

$container->setDefinition(
FormBuilder::class,
Expand Down
9 changes: 7 additions & 2 deletions tests/src/Integration/RetrofitTitleResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use mglaman\DrupalTestHelpers\RequestTrait;
use Retrofit\Drupal\Controller\RetrofitTitleResolver;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Route;

final class RetrofitTitleResolverTest extends IntegrationTestCase
Expand Down Expand Up @@ -45,13 +46,17 @@ public function testSetStoredTitle(): void
RetrofitTitleResolver::class,
$titleResolver
);
$requestStack = $this->container->get('request_stack');
assert($requestStack instanceof RequestStack);
$request = $requestStack->getCurrentRequest();
assert($request instanceof Request);
Comment on lines +49 to +52
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I'm surprised this works without pushing a request to the stack.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that pathInfo is not set when a request is created, so it never matched the request from the request stack.

self::assertEquals(null, $titleResolver->getTitle(
Request::create('/'),
$request,
$this->createMock(Route::class)
));
$titleResolver->setStoredTitle('foo');
self::assertEquals('foo', $titleResolver->getTitle(
Request::create('/'),
$request,
$this->createMock(Route::class)
));
}
Expand Down
Loading