diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module index 342ed7b..df97e9f 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -113,19 +113,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(). + * Entity URI callback. + * + * @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()]); @@ -544,7 +535,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 = array('#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 6ab41c9..8b5354e 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\Access\RouteProcessor + tags: + - { name: route_processor_outbound } + arguments: ['@config.factory', '@entity.manager'] forum.uninstall_validator: class: Drupal\forum\ForumUninstallValidator tags: diff --git a/core/modules/forum/src/Access/RouteProcessor.php b/core/modules/forum/src/Access/RouteProcessor.php new file mode 100644 index 0000000..599d76f --- /dev/null +++ b/core/modules/forum/src/Access/RouteProcessor.php @@ -0,0 +1,62 @@ +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')) { + if ($this->entityTypeManager->getStorage('taxonomy_term')->load($parameters['taxonomy_term'])->getVocabularyId() == $vid) { + $route->setPath('/forum/{taxonomy_term}'); + } + } + } + } + +} diff --git a/core/modules/forum/src/Tests/ForumTest.php b/core/modules/forum/src/Tests/ForumTest.php index c81828e..407ca7e 100644 --- a/core/modules/forum/src/Tests/ForumTest.php +++ b/core/modules/forum/src/Tests/ForumTest.php @@ -224,6 +224,11 @@ 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)]', array(':href' => 'forum/' . $this->forum['tid'])); + $this->assertEqual(count($elements), 1, 'Found taxonomy term overridden link.'); + $edit = array(); $edit['comment_body[0][value]'] = $this->randomMachineName(); $this->drupalPostForm('node/' . $node->id(), $edit, t('Save'));