Skip to content

Commit c5a2c50

Browse files
committed
Breadcrumb for OpenAPI node
1 parent 8235f3e commit c5a2c50

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

apigee_api_catalog.services.yml

+5
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ services:
1717
apigee_api_catalog.updates:
1818
class: Drupal\apigee_api_catalog\UpdateService
1919
arguments: ['@uuid', '@config.factory', '@module_handler', '@entity_type.manager', '@entity_field.manager', '@entity.last_installed_schema.repository']
20+
21+
apigee_api_catalog.breadcrumb:
22+
class: Drupal\apigee_api_catalog\ApigeeApiCatalogBreadcrumbBuilder
23+
tags:
24+
- { name: breadcrumb_builder, priority: 1000 }
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Copyright 2022 Google Inc.
6+
*
7+
* This program is free software; you can redistribute it and/or modify it under
8+
* the terms of the GNU General Public License version 2 as published by the
9+
* Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14+
* License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License along
17+
* with this program; if not, write to the Free Software Foundation, Inc., 51
18+
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
21+
namespace Drupal\apigee_api_catalog;
22+
23+
use Drupal\Core\Breadcrumb\Breadcrumb;
24+
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
25+
use Drupal\Core\Link;
26+
use Drupal\Core\Routing\RouteMatchInterface;
27+
use Drupal\Core\StringTranslation\StringTranslationTrait;
28+
use Drupal\node\NodeInterface;
29+
30+
/**
31+
* Provides a breadcrumb builder for apidoc.
32+
*/
33+
class ApigeeApiCatalogBreadcrumbBuilder implements BreadcrumbBuilderInterface {
34+
35+
use StringTranslationTrait;
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function applies(RouteMatchInterface $route_match) {
41+
$node = $route_match->getParameter('node');
42+
return $node instanceof NodeInterface && $node->getType() === 'apidoc';
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function build(RouteMatchInterface $route_match) {
49+
$breadcrumb = new Breadcrumb();
50+
51+
$links[] = Link::createFromRoute($this->t('Home'), '<front>');
52+
53+
// API Catalog is a view.
54+
$links[] = Link::createFromRoute($this->t('API Catalog'), 'view.apigee_api_catalog.page_1');
55+
56+
$breadcrumb->setLinks($links);
57+
$breadcrumb->addCacheContexts(['route']);
58+
59+
return $breadcrumb;
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2022 Google Inc.
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU General Public License
8+
* version 2 as published by the Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18+
* MA 02110-1301, USA.
19+
*/
20+
21+
namespace Drupal\Tests\apigee_api_catalog\Functional;
22+
23+
use Drupal\Core\Url;
24+
use Drupal\Tests\BrowserTestBase;
25+
26+
/**
27+
* Tests apidoc breadcrumb.
28+
*
29+
* @group apigee_api_catalog
30+
*/
31+
class ApiDocsBreadcrumbTest extends BrowserTestBase {
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
protected $defaultTheme = 'classy';
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
protected static $modules = [
42+
'node',
43+
'path_alias',
44+
'apigee_api_catalog',
45+
'views',
46+
'block',
47+
];
48+
49+
/**
50+
* A test doc.
51+
*
52+
* @var \Drupal\node\NodeInterface
53+
*/
54+
protected $apidoc;
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
protected function setUp(): void {
60+
parent::setUp();
61+
$this->drupalPlaceBlock('system_breadcrumb_block');
62+
$this->drupalPlaceBlock('page_title_block');
63+
64+
$this->apidoc = $this->container->get('entity_type.manager')
65+
->getStorage('node')
66+
->create([
67+
'type' => 'apidoc',
68+
'title' => 'API 1',
69+
'body' => [
70+
'value' => 'Test API 1',
71+
'format' => 'basic_html',
72+
],
73+
'field_apidoc_spec' => NULL,
74+
]);
75+
76+
$this->apidoc->save();
77+
78+
$user = $this->drupalCreateUser(['access content']);
79+
$this->drupalLogin($user);
80+
}
81+
82+
/**
83+
* Tests the route subscriber will redirect from smartdoc routes.
84+
*/
85+
public function testApiDocBreadcrumb() {
86+
// Tests the normal response.
87+
$url = Url::fromRoute('entity.node.canonical', ['node' => $this->apidoc->id()]);
88+
$this->drupalGet($url);
89+
90+
// Fetch each node title in the current breadcrumb.
91+
$links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
92+
$got_breadcrumb = [];
93+
foreach ($links as $link) {
94+
$got_breadcrumb[] = $link->getText();
95+
}
96+
// print_r($got_breadcrumb);
97+
$this->assertEquals($got_breadcrumb[0], 'Home');
98+
$this->assertEquals($got_breadcrumb[1], 'API Catalog');
99+
100+
}
101+
102+
}

0 commit comments

Comments
 (0)