diff -u b/core/modules/help_topics/src/HelpTopicPluginBase.php b/core/modules/help_topics/src/HelpTopicPluginBase.php --- b/core/modules/help_topics/src/HelpTopicPluginBase.php +++ b/core/modules/help_topics/src/HelpTopicPluginBase.php @@ -2,12 +2,9 @@ namespace Drupal\help_topics; -use Drupal\Core\Cache\Cache; use Drupal\Core\Link; -use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\PluginBase; use Drupal\Core\Url; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Base class for help topic plugins. @@ -17,43 +14,7 @@ * experimental modules and development releases of contributed modules. * See https://www.drupal.org/core/experimental for more information. */ -abstract class HelpTopicPluginBase extends PluginBase implements HelpTopicPluginInterface, ContainerFactoryPluginInterface { - - /** - * The help topic plugin manager. - * - * @var \Drupal\help_topics\HelpTopicPluginManagerInterface - */ - protected $pluginManager; - - /** - * HelpTopicPluginBase constructor. - * - * @param array $configuration - * A configuration array containing information about the plugin instance. - * @param string $plugin_id - * The plugin_id for the plugin instance. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\help_topics\HelpTopicPluginManagerInterface $plugin_manager - * The help topic plugin manager. - */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, HelpTopicPluginManagerInterface $plugin_manager) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->pluginManager = $plugin_manager; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('plugin.manager.help_topic') - ); - } +abstract class HelpTopicPluginBase extends PluginBase implements HelpTopicPluginInterface { /** * The name of the module or theme providing the help topic. @@ -86,52 +47,6 @@ /** * {@inheritdoc} */ - public function getCacheMaxAge() { - return Cache::PERMANENT; - } - - /** - * {@inheritdoc} - */ - public function getCacheContexts() { - return []; - } - - /** - * {@inheritdoc} - */ - public function getCacheTags() { - $tags = []; - if ($this->pluginDefinition['cache_tag']) { - $tags[] = $this->pluginDefinition['cache_tag']; - } - - foreach ($this->pluginDefinition['related'] as $topic) { - $tag = $this->pluginManager->getDefinition($topic)['cache_tag']; - if ($tag) { - $tags[] = $tag; - } - } - return array_unique($tags); - } - - /** - * {@inheritdoc} - */ - public function getCacheTagsForList() { - $tags = []; - // By default, there are no list cache tags, but we do want the cache - // tags for this plugin. - if ($this->pluginDefinition['cache_tag']) { - $tags[] = $this->pluginDefinition['cache_tag']; - } - - return $tags; - } - - /** - * {@inheritdoc} - */ public function toUrl(array $options = []) { return Url::fromRoute('help_topics.help_topic', ['id' => $this->getPluginId()], $options); } diff -u b/core/modules/help_topics/src/HelpTopicPluginInterface.php b/core/modules/help_topics/src/HelpTopicPluginInterface.php --- b/core/modules/help_topics/src/HelpTopicPluginInterface.php +++ b/core/modules/help_topics/src/HelpTopicPluginInterface.php @@ -82,10 +82,2 @@ - /** - * Returns the cache tags appropriate for listings of plugins of this type. - * - * @return string[] - * Array of cache tags. - */ - public function getCacheTagsForList(); - } diff -u b/core/modules/help_topics/src/HelpTopicPluginManager.php b/core/modules/help_topics/src/HelpTopicPluginManager.php --- b/core/modules/help_topics/src/HelpTopicPluginManager.php +++ b/core/modules/help_topics/src/HelpTopicPluginManager.php @@ -58,7 +58,6 @@ 'related' => [], // The class used to instantiate the plugin. 'class' => '', - 'cache_tag' => '', ]; /** diff -u b/core/modules/help_topics/src/HelpTopicTwig.php b/core/modules/help_topics/src/HelpTopicTwig.php --- b/core/modules/help_topics/src/HelpTopicTwig.php +++ b/core/modules/help_topics/src/HelpTopicTwig.php @@ -2,6 +2,8 @@ namespace Drupal\help_topics; +use Drupal\Core\Cache\UnchangingCacheableDependencyTrait; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Template\TwigEnvironment; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -16,7 +18,8 @@ * experimental modules and development releases of contributed modules. * See https://www.drupal.org/core/experimental for more information. */ -class HelpTopicTwig extends HelpTopicPluginBase { +class HelpTopicTwig extends HelpTopicPluginBase implements ContainerFactoryPluginInterface { + use UnchangingCacheableDependencyTrait; /** * The Twig environment. @@ -34,13 +37,11 @@ * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. - * @param \Drupal\help_topics\HelpTopicPluginManagerInterface $plugin_manager - * The help topic plugin manager. * @param \Drupal\Core\Template\TwigEnvironment $twig * The Twig environment. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, HelpTopicPluginManagerInterface $plugin_manager, TwigEnvironment $twig) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $plugin_manager); + public function __construct(array $configuration, $plugin_id, $plugin_definition, TwigEnvironment $twig) { + parent::__construct($configuration, $plugin_id, $plugin_definition); $this->twig = $twig; } @@ -52,7 +53,6 @@ $configuration, $plugin_id, $plugin_definition, - $container->get('plugin.manager.help_topic'), $container->get('twig') ); } diff -u b/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php b/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php --- b/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php +++ b/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php @@ -2,6 +2,7 @@ namespace Drupal\help_topics\Plugin\HelpSection; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\help_topics\HelpTopicPluginInterface; use Drupal\help_topics\HelpTopicPluginManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -33,18 +34,16 @@ protected $pluginManager; /** - * The render array for the list of topics. + * The top level help topic plugins. * - * @var array + * @var \Drupal\help_topics\HelpTopicPluginInterface[] */ - protected $topicList; + protected $topLevelPlugins; /** - * The cache tags for the list of topics. - * - * @var string[] + * The merged top level help topic plugins cache metadata. */ - protected $cacheTagList; + protected $cacheableMetadata; /** * Constructs a HelpTopicSection object. @@ -79,56 +78,77 @@ * {@inheritdoc} */ public function getCacheTags() { - if (!isset($this->topicList)) { - $this->setProperties(); - } - - return $this->cacheTagList; + return $this->getCacheMetadata()->getCacheTags(); } /** * {@inheritdoc} */ - public function listTopics() { - if (!isset($this->topicList)) { - $this->setProperties(); - } + public function getCacheContexts() { + return $this->getCacheMetadata()->getCacheContexts(); + } - return $this->topicList; + /** + * {@inheritdoc} + */ + public function getCacheMaxAge() { + return $this->getCacheMetadata()->getCacheMaxAge(); } /** - * Sets the topic list and cache tags properties. + * {@inheritdoc} */ - protected function setProperties() { - $definitions = $this->pluginManager->getDefinitions(); + public function listTopics() { + // Map the top level help topic plugins to a list of topic links. + return array_map(function (HelpTopicPluginInterface $topic) { + return $topic->toLink(); + }, $this->getPlugins()); + } - // Get all the top level topics and merge their list cache tags. - $top_level_topics = []; - $this->cacheTagList = []; - foreach ($definitions as $definition) { - if ($definition['top_level']) { - $plugin = $this->pluginManager->createInstance($definition['id']); - $top_level_topics[$plugin->getPluginId()] = $plugin; - $this->cacheTagList = array_merge($this->cacheTagList, $plugin->getCacheTagsForList()); + /** + * Gets the top level help topic plugins. + * + * @return \Drupal\help_topics\HelpTopicPluginInterface[] + * The top level help topic plugins + */ + protected function getPlugins() { + if (!isset($this->topLevelPlugins)) { + $definitions = $this->pluginManager->getDefinitions(); + + // Get all the top level topics and merge their list cache tags. + foreach ($definitions as $definition) { + if ($definition['top_level']) { + $this->topLevelPlugins[$definition['id']] = $this->pluginManager->createInstance($definition['id']); + } } + + // Sort the top level topics by label and if they match by plugin ID. + usort($this->topLevelPlugins, function (HelpTopicPluginInterface $a, HelpTopicPluginInterface $b) { + $a_label = (string) $a->getLabel(); + $b_label = (string) $b->getLabel(); + if ($a_label === $b_label) { + return $a->getPluginId() < $b->getPluginId() ? -1 : 1; + } + return $a_label < $b_label ? -1 : 1; + }); } - $this->cacheTagList = array_unique($this->cacheTagList); + return $this->topLevelPlugins; + } - // Sort the top level topics by label and if they match by plugin ID. - usort($top_level_topics, function (HelpTopicPluginInterface $a, HelpTopicPluginInterface $b) { - $a_label = (string) $a->getLabel(); - $b_label = (string) $b->getLabel(); - if ($a_label === $b_label) { - return $a->getPluginId() < $b->getPluginId() ? -1 : 1; + /** + * Gets the merged CacheableMetadata for all the top level help topic plugins. + * + * @return \Drupal\Core\Cache\CacheableMetadata + * The merged CacheableMetadata for all the top level help topic plugins. + */ + protected function getCacheMetadata() { + if (!isset($this->cacheableMetadata)) { + $this->cacheableMetadata = new CacheableMetadata(); + foreach ($this->getPlugins() as $plugin) { + $this->cacheableMetadata->addCacheableDependency($plugin); } - return $a_label < $b_label ? -1 : 1; - }); - - // Map the top level help topic plugins to a list of topic links. - $this->topicList = array_map(function (HelpTopicPluginInterface $topic) { - return $topic->toLink(); - }, $top_level_topics); + } + return $this->cacheableMetadata; } } diff -u b/core/modules/help_topics/tests/src/Functional/HelpTopicTest.php b/core/modules/help_topics/tests/src/Functional/HelpTopicTest.php --- b/core/modules/help_topics/tests/src/Functional/HelpTopicTest.php +++ b/core/modules/help_topics/tests/src/Functional/HelpTopicTest.php @@ -186,7 +186,6 @@ * keys are the machine names of the topics. The values are arrays with the * following elements: * - name: Displayed name. - * - cache_tags: Cache tags to verify are present on the topic display page. */ protected function getTopicList() { return [