From 56f670c8610b06bd00b5e09f05cf0f36cde55d0a Mon Sep 17 00:00:00 2001
From: Lee Rowlands <lee.rowlands@previousnext.com.au>
Date: Tue, 15 Oct 2019 10:53:29 +1000
Subject: [PATCH] Issue #3079810 by jhodgdon, andypost, mikelutz:
 core/help_topics directory does not work

(cherry picked from commit e0f9dba353f5d738c6e20c934b6c855e2ac29d19)
---
 .../help_topics/help_topics.services.yml      |  2 +-
 .../src/HelpTopicPluginManager.php            | 14 +++++++++--
 .../help_topics/src/HelpTopicTwigLoader.php   |  3 ++-
 .../src/Functional/HelpTopicsSyntaxTest.php   |  2 +-
 .../tests/src/Unit/HelpTopicDiscoveryTest.php | 23 +++++++++++++++++++
 5 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/core/modules/help_topics/help_topics.services.yml b/core/modules/help_topics/help_topics.services.yml
index aff7698abc5e..ed6a59df0e2b 100644
--- a/core/modules/help_topics/help_topics.services.yml
+++ b/core/modules/help_topics/help_topics.services.yml
@@ -7,7 +7,7 @@ services:
     public: false
   plugin.manager.help_topic:
     class: Drupal\help_topics\HelpTopicPluginManager
-    arguments: ['@module_handler', '@theme_handler', '@cache.discovery']
+    arguments: ['@module_handler', '@theme_handler', '@cache.discovery', '@app.root']
   help.twig.loader:
     class: Drupal\help_topics\HelpTopicTwigLoader
     arguments: ['@app.root', '@module_handler', '@theme_handler']
diff --git a/core/modules/help_topics/src/HelpTopicPluginManager.php b/core/modules/help_topics/src/HelpTopicPluginManager.php
index 40efa8510630..b2a94fc348b8 100644
--- a/core/modules/help_topics/src/HelpTopicPluginManager.php
+++ b/core/modules/help_topics/src/HelpTopicPluginManager.php
@@ -93,6 +93,13 @@ class HelpTopicPluginManager extends DefaultPluginManager implements HelpTopicPl
    */
   protected $themeHandler;
 
+  /**
+   * The app root.
+   *
+   * @var string
+   */
+  protected $root;
+
   /**
    * Constructs a new HelpTopicManager object.
    *
@@ -102,8 +109,10 @@ class HelpTopicPluginManager extends DefaultPluginManager implements HelpTopicPl
    *   The theme handler.
    * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
    *   Cache backend instance to use.
+   * @param string $root
+   *   The app root.
    */
-  public function __construct(ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache_backend) {
+  public function __construct(ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache_backend, $root) {
     // Note that the parent construct is not called because this not use
     // annotated class discovery.
     $this->moduleHandler = $module_handler;
@@ -112,6 +121,7 @@ public function __construct(ModuleHandlerInterface $module_handler, ThemeHandler
     // Use the 'config:core.extension' cache tag so the plugin cache is
     // invalidated on theme install and uninstall.
     $this->setCacheBackend($cache_backend, 'help_topics', ['config:core.extension']);
+    $this->root = (string) $root;
   }
 
   /**
@@ -121,7 +131,7 @@ protected function getDiscovery() {
     if (!isset($this->discovery)) {
       $module_directories = $this->moduleHandler->getModuleDirectories();
       $all_directories = array_merge(
-        ['core'],
+        ['core' => $this->root . '/core'],
         $module_directories,
         $this->themeHandler->getThemeDirectories()
       );
diff --git a/core/modules/help_topics/src/HelpTopicTwigLoader.php b/core/modules/help_topics/src/HelpTopicTwigLoader.php
index c945f526ddfc..b785736f1968 100644
--- a/core/modules/help_topics/src/HelpTopicTwigLoader.php
+++ b/core/modules/help_topics/src/HelpTopicTwigLoader.php
@@ -44,7 +44,8 @@ class HelpTopicTwigLoader extends \Twig_Loader_Filesystem {
   public function __construct($root_path, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
     parent::__construct([], $root_path);
     // Add help_topics directories for modules and themes in the 'help_topic'
-    // namespace.
+    // namespace, plus core.
+    $this->addExtension($root_path . '/core');
     array_map([$this, 'addExtension'], $module_handler->getModuleDirectories());
     array_map([$this, 'addExtension'], $theme_handler->getThemeDirectories());
   }
diff --git a/core/modules/help_topics/tests/src/Functional/HelpTopicsSyntaxTest.php b/core/modules/help_topics/tests/src/Functional/HelpTopicsSyntaxTest.php
index a8ef4eb2f7b1..c2f3b016a73b 100644
--- a/core/modules/help_topics/tests/src/Functional/HelpTopicsSyntaxTest.php
+++ b/core/modules/help_topics/tests/src/Functional/HelpTopicsSyntaxTest.php
@@ -42,7 +42,7 @@ public function testHelpTopics() {
 
     $directories = $module_directories + $theme_directories +
       $this->listDirectories('profile');
-    $directories['core'] = 'core/help_topics';
+    $directories['core'] = \Drupal::service('app.root') . '/core/help_topics';
     $directories['bad_help_topics'] = \Drupal::service('extension.list.module')->getPath('help_topics_test') . '/bad_help_topics/syntax/';
 
     // Filter out directories outside of core. If you want to run this test
diff --git a/core/modules/help_topics/tests/src/Unit/HelpTopicDiscoveryTest.php b/core/modules/help_topics/tests/src/Unit/HelpTopicDiscoveryTest.php
index 6457cf75b347..cc319dfa8716 100644
--- a/core/modules/help_topics/tests/src/Unit/HelpTopicDiscoveryTest.php
+++ b/core/modules/help_topics/tests/src/Unit/HelpTopicDiscoveryTest.php
@@ -170,6 +170,29 @@ public function testHelpTopicsExtensionProviderSpecialCase() {
     $this->assertArrayHasKey('core.topic', $discovery->getDefinitions());
   }
 
+  /**
+   * @covers ::findAll
+   */
+  public function testHelpTopicsInCore() {
+    vfsStream::setup('root');
+    $topic_content = <<<EOF
+---
+label: Test
+---
+<h2>Test</h2>
+EOF;
+
+    vfsStream::create([
+      'core' => [
+        'help_topics' => [
+          'core.topic.html.twig' => $topic_content,
+        ],
+      ],
+    ]);
+    $discovery = new HelpTopicDiscovery(['core' => vfsStream::url('root/core/help_topics')]);
+    $this->assertArrayHasKey('core.topic', $discovery->getDefinitions());
+  }
+
   /**
    * @covers ::findAll
    */
-- 
GitLab