diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module index c2de3c9e..6ad065b3 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -115,19 +115,10 @@ function forum_entity_type_build(array &$entity_types) { } /** - * Implements hook_entity_bundle_info_alter(). - */ -function forum_entity_bundle_info_alter(&$bundles) { - // Take over URI construction for taxonomy terms that are forums. - if ($vid = \Drupal::config('forum.settings')->get('vocabulary')) { - if (isset($bundles['taxonomy_term'][$vid])) { - $bundles['taxonomy_term'][$vid]['uri_callback'] = 'forum_uri'; - } - } -} - -/** * Entity URI callback used in forum_entity_bundle_info_alter(). + * + * @deprecated in Drupal 8.x, will be removed before Drupal 9.0.0. + * Use $forum->url() instead. */ function forum_uri($forum) { return Url::fromRoute('forum.page', ['taxonomy_term' => $forum->id()]); @@ -546,7 +537,7 @@ function template_preprocess_forum_list(&$variables) { // Sanitize each forum so that the template can safely print the data. foreach ($variables['forums'] as $id => $forum) { $variables['forums'][$id]->description = ['#markup' => $forum->description->value]; - $variables['forums'][$id]->link = forum_uri($forum); + $variables['forums'][$id]->link = $forum->url(); $variables['forums'][$id]->name = $forum->label(); $variables['forums'][$id]->is_container = !empty($forum->forum_container->value); $variables['forums'][$id]->zebra = $row % 2 == 0 ? 'odd' : 'even'; diff --git a/core/modules/forum/forum.services.yml b/core/modules/forum/forum.services.yml index 88557171..df863802 100644 --- a/core/modules/forum/forum.services.yml +++ b/core/modules/forum/forum.services.yml @@ -19,7 +19,11 @@ services: arguments: ['@database', '@forum_manager'] tags: - { name: backend_overridable } - + forum.route_processor: + class: Drupal\forum\Routing\RouteProcessor + tags: + - { name: route_processor_outbound } + arguments: ['@config.factory', '@entity_type.manager'] forum.uninstall_validator: class: Drupal\forum\ForumUninstallValidator tags: diff --git a/core/modules/forum/src/Routing/RouteProcessor.php b/core/modules/forum/src/Routing/RouteProcessor.php new file mode 100644 index 00000000..285e186b --- /dev/null +++ b/core/modules/forum/src/Routing/RouteProcessor.php @@ -0,0 +1,58 @@ +configFactory = $config_factory; + $this->entityTypeManager = $entity_type_manager; + } + + /** + * {@inheritdoc} + */ + public function processOutbound($route_name, Route $route, array &$parameters, BubbleableMetadata $bubbleable_metadata = NULL) { + if ($route_name == 'entity.taxonomy_term.canonical' && !empty($parameters['taxonomy_term'])) { + // Take over URI construction for taxonomy terms that belong to forum. + if ($vid = $this->configFactory->get('forum.settings')->get('vocabulary')) { + $term = $this->entityTypeManager->getStorage('taxonomy_term')->load($parameters['taxonomy_term']); + if ($term && $term->getVocabularyId() == $vid) { + $route->setPath('/forum/{taxonomy_term}'); + } + } + } + } + +} diff --git a/core/modules/forum/tests/src/Functional/ForumTest.php b/core/modules/forum/tests/src/Functional/ForumTest.php index 76823a75..a2039368 100644 --- a/core/modules/forum/tests/src/Functional/ForumTest.php +++ b/core/modules/forum/tests/src/Functional/ForumTest.php @@ -218,6 +218,10 @@ public function testForum() { // Test adding a comment to a forum topic. $node = $this->createForumTopic($this->forum, FALSE); + // Make sure the forum field points to the forum. + $elements = $this->xpath('//div[contains(@class, "field--name-taxonomy-forums")]//a[contains(@href, :href)]', [':href' => 'forum/' . $this->forum['tid']]); + $this->assertEqual(count($elements), 1, 'Found taxonomy term overridden link.'); + $edit = []; $edit['comment_body[0][value]'] = $this->randomMachineName(); $this->drupalPostForm('node/' . $node->id(), $edit, t('Save'));