Commit 0f9fd871 authored by catch's avatar catch

Issue #2551907 by Wim Leers, dawehner: Follow-up for #2483183: make the...

Issue #2551907 by Wim Leers, dawehner: Follow-up for #2483183: make the Breadcrumb value object use composition instead of inheritance
parent ae7ec6c5
......@@ -7,15 +7,16 @@
namespace Drupal\Core\Breadcrumb;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
use Drupal\Core\Link;
/**
* Used to return generated breadcrumbs with associated cacheability metadata.
*
* @todo implement RenderableInterface once https://www.drupal.org/node/2529560 lands.
*/
class Breadcrumb extends CacheableMetadata {
class Breadcrumb implements RefinableCacheableDependencyInterface {
use RefinableCacheableDependencyTrait;
/**
* An ordered list of links for the breadcrumb.
......@@ -68,4 +69,29 @@ public function addLink(Link $link) {
return $this;
}
/**
* Returns a render array representation of the object.
*
* @return mixed[]
* A render array.
*
* @todo implement RenderableInterface once https://www.drupal.org/node/2529560 lands.
*/
public function toRenderable() {
$build = [
'#cache' => [
'contexts' => $this->cacheContexts,
'tags' => $this->cacheTags,
'max-age' => $this->cacheMaxAge,
],
];
if (!empty($this->links)) {
$build += [
'#theme' => 'breadcrumb',
'#links' => $this->links,
];
}
return $build;
}
}
......@@ -33,6 +33,27 @@ trait RefinableCacheableDependencyTrait {
*/
protected $cacheMaxAge = Cache::PERMANENT;
/**
* {@inheritdoc}
*/
public function getCacheTags() {
return $this->cacheTags;
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return $this->cacheContexts;
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return $this->cacheMaxAge;
}
/**
* {@inheritdoc}
*/
......
......@@ -96,7 +96,7 @@ public function build(RouteMatchInterface $route_match) {
}
}
$breadcrumb->setLinks($links);
$breadcrumb->setCacheContexts(['route.book_navigation']);
$breadcrumb->addCacheContexts(['route.book_navigation']);
return $breadcrumb;
}
......
......@@ -49,7 +49,7 @@ public function applies(RouteMatchInterface $route_match) {
*/
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$breadcrumb->setCacheContexts(['route']);
$breadcrumb->addCacheContexts(['route']);
$breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '<front>'));
$entity = $route_match->getParameter('entity');
......
......@@ -67,7 +67,7 @@ public function __construct(EntityManagerInterface $entity_manager, ConfigFactor
*/
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$breadcrumb->setCacheContexts(['route']);
$breadcrumb->addCacheContexts(['route']);
$links[] = Link::createFromRoute($this->t('Home'), '<front>');
......
......@@ -143,7 +143,7 @@ public function build(RouteMatchInterface $route_match) {
$exclude['/user'] = TRUE;
// Because this breadcrumb builder is entirely path-based, vary by the
// 'url.path' cache context.
$breadcrumb->setCacheContexts(['url.path']);
$breadcrumb->addCacheContexts(['url.path']);
while (count($path_elements) > 1) {
array_pop($path_elements);
// Copy the path elements for up-casting.
......
......@@ -74,16 +74,7 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
public function build() {
$breadcrumb = $this->breadcrumbManager->build($this->routeMatch);
if (!empty($breadcrumb)) {
// $breadcrumb is expected to be an array of rendered breadcrumb links.
$build = [
'#theme' => 'breadcrumb',
'#links' => $breadcrumb->getLinks(),
];
$breadcrumb->applyTo($build);
return $build;
}
return $this->breadcrumbManager->build($this->routeMatch)->toRenderable();
}
}
......@@ -78,7 +78,7 @@ public function build(RouteMatchInterface $route_match) {
// This breadcrumb builder is based on a route parameter, and hence it
// depends on the 'route' cache context.
$breadcrumb->setCacheContexts(['route']);
$breadcrumb->addCacheContexts(['route']);
return $breadcrumb;
}
......
......@@ -10,6 +10,8 @@
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbManager;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
/**
......@@ -18,6 +20,13 @@
*/
class BreadcrumbManagerTest extends UnitTestCase {
/**
* The dependency injection container.
*
* @var \Symfony\Component\DependencyInjection\ContainerBuilder
*/
protected $container;
/**
* The breadcrumb object.
*
......@@ -46,6 +55,11 @@ protected function setUp() {
$this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$this->breadcrumbManager = new BreadcrumbManager($this->moduleHandler);
$this->breadcrumb = new Breadcrumb();
$this->container = new ContainerBuilder();
$cache_contexts_manager = $this->prophesize(CacheContextsManager::class)->reveal();
$this->container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($this->container);
}
/**
......@@ -71,7 +85,7 @@ public function testBuildWithSingleBuilder() {
$builder = $this->getMock('Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface');
$links = array('<a href="/example">Test</a>');
$this->breadcrumb->setLinks($links);
$this->breadcrumb->setCacheContexts(['foo'])->setCacheTags(['bar']);
$this->breadcrumb->addCacheContexts(['foo'])->addCacheTags(['bar']);
$builder->expects($this->once())
->method('applies')
......@@ -108,7 +122,7 @@ public function testBuildWithMultipleApplyingBuilders() {
$builder2 = $this->getMock('Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface');
$links2 = array('<a href="/example2">Test2</a>');
$this->breadcrumb->setLinks($links2);
$this->breadcrumb->setCacheContexts(['baz'])->setCacheTags(['qux']);
$this->breadcrumb->addCacheContexts(['baz'])->addCacheTags(['qux']);
$builder2->expects($this->once())
->method('applies')
->will($this->returnValue(TRUE));
......@@ -146,7 +160,7 @@ public function testBuildWithOneNotApplyingBuilders() {
$builder2 = $this->getMock('Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface');
$links2 = ['<a href="/example2">Test2</a>'];
$this->breadcrumb->setLinks($links2);
$this->breadcrumb->setCacheContexts(['baz'])->setCacheTags(['qux']);
$this->breadcrumb->addCacheContexts(['baz'])->addCacheTags(['qux']);
$builder2->expects($this->once())
->method('applies')
->will($this->returnValue(TRUE));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment