diff --git a/core/MAINTAINERS.txt b/core/MAINTAINERS.txt index a505c631ab..4d98cd95a2 100644 --- a/core/MAINTAINERS.txt +++ b/core/MAINTAINERS.txt @@ -231,6 +231,10 @@ Hypertext Application Language (HAL) Help - ? +Help Topics +- Amber Matz 'Amber Himes Matz' https://www.drupal.org/u/amber-himes-matz +- Andrey Postnikov 'andypost' https://www.drupal.org/u/andypost + Image - Claudiu Cristea 'claudiu.cristea' https://www.drupal.org/u/claudiu.cristea diff --git a/core/composer.json b/core/composer.json index 8dfd8a10f2..53732fbb68 100644 --- a/core/composer.json +++ b/core/composer.json @@ -124,6 +124,7 @@ "drupal/forum": "self.version", "drupal/hal": "self.version", "drupal/help": "self.version", + "drupal/help_topics": "self.version", "drupal/history": "self.version", "drupal/image": "self.version", "drupal/inline_form_errors": "self.version", diff --git a/core/modules/help_topics/help_topics.info.yml b/core/modules/help_topics/help_topics.info.yml new file mode 100644 index 0000000000..3afdacc432 --- /dev/null +++ b/core/modules/help_topics/help_topics.info.yml @@ -0,0 +1,8 @@ +name: Help Topics +type: module +description: 'Displays help topics provided by themes and modules.' +core: 8.x +package: Core (Experimental) +version: VERSION +dependencies: + - drupal:help diff --git a/core/modules/help_topics/help_topics.module b/core/modules/help_topics/help_topics.module new file mode 100644 index 0000000000..8f9b064a10 --- /dev/null +++ b/core/modules/help_topics/help_topics.module @@ -0,0 +1,59 @@ +' . t('About') . ''; + $output .= '

' . t('The Help Topics module adds module- and theme-provided help topics to the module overviews from the core Help module. For more information, see the online documentation for the Help Topics module.', [':online' => 'https://www.drupal.org/modules/help_topics']) . '

'; + $output .= '

' . t('Uses') . '

'; + $output .= '
'; + $output .= '
' . t('Viewing help topics') . '
'; + $output .= '
' . t('The top-level help topics are listed on the main Help page. Links to other topics, including non-top-level help topics, can be found under the "Related" heading when viewing a topic page.', [':help_page' => Url::fromRoute('help.main')->toString()]) . '
'; + $output .= '
' . t('Providing help topics') . '
'; + $output .= '
' . t("Modules and themes can provide help topics as YAML-file-based plugins in a project sub-directory called help_topics. The content of the topic is contained in a matching Twig file in help_topics/templates. Any file in a module or theme's help_topics directory with the suffix *.help_topic.yml will be discovered by the Help Topic module. Plugin-based help topics provided by modules and themes will automatically be updated when a module or theme is updated. Use the plugins in core/modules/help_topics/help_topics as a guide when writing and formatting a help topic plugin for your theme or module.") . '
'; + $output .= '
' . t('Translating help topics') . '
'; + $output .= '
' . t('The title and body text of help topics provided by contributed modules and themes are translatable using the Interface Translation module. Topics provided by custom modules and themes are also translatable if they have been viewed at least once in a non-English language, which triggers putting their translatable text into the translation database.', [':locale_help' => (\Drupal::moduleHandler()->moduleExists('locale')) ? Url::fromRoute('help.page', ['name' => 'locale'])->toString() : '#']) . '
'; + $output .= '
'; + return ['#markup' => $output]; + + case 'help_topics.help_topic': + return '

' . t('See the Help page for more topics.', [ + ':help_page' => Url::fromRoute('help.main')->toString(), + ]) . '

'; + } +} + +/** + * Implements hook_theme(). + * + * @see \Drupal\help_topics\HelpTopicPluginManager::getThemeImplementations() + */ +function help_topics_theme() { + return \Drupal::service('plugin.manager.help_topic')->getThemeImplementations(); +} + +/** + * Implements hook_themes_installed(). + */ +function help_topics_themes_installed($theme_list) { + \Drupal::service('plugin.manager.help_topic')->clearCachedDefinitions(); +} + +/** + * Implements hook_themes_uninstalled(). + */ +function help_topics_themes_uninstalled($theme_list) { + \Drupal::service('plugin.manager.help_topic')->clearCachedDefinitions(); +} diff --git a/core/modules/help_topics/help_topics.routing.yml b/core/modules/help_topics/help_topics.routing.yml new file mode 100644 index 0000000000..88b4784f4d --- /dev/null +++ b/core/modules/help_topics/help_topics.routing.yml @@ -0,0 +1,6 @@ +help_topics.help_topic: + path: '/admin/help/topic/{id}' + defaults: + _controller: '\Drupal\help_topics\Controller\HelpTopicPluginController::viewHelpTopic' + requirements: + _permission: 'access administration pages' diff --git a/core/modules/help_topics/help_topics.services.yml b/core/modules/help_topics/help_topics.services.yml new file mode 100644 index 0000000000..fbe85059be --- /dev/null +++ b/core/modules/help_topics/help_topics.services.yml @@ -0,0 +1,9 @@ +services: + help_topics.breadcrumb: + class: Drupal\help_topics\HelpBreadcrumbBuilder + arguments: ['@string_translation'] + tags: + - { name: breadcrumb_builder, priority: 900 } + plugin.manager.help_topic: + class: Drupal\help_topics\HelpTopicPluginManager + arguments: ['@module_handler', '@theme_handler', '@cache.discovery'] diff --git a/core/modules/help_topics/help_topics/config-basic.html.twig b/core/modules/help_topics/help_topics/config-basic.html.twig new file mode 100644 index 0000000000..459bc9c32c --- /dev/null +++ b/core/modules/help_topics/help_topics/config-basic.html.twig @@ -0,0 +1,15 @@ + + + +{% set regional_url = render_var(url('system.regional_settings')) %} +{% set information_url = render_var(url('system.site_information_settings')) %} +{% set datetime_url = render_var(url('entity.date_format.collection')) %} +

{% trans %}The settings for your site are configured on various administrative pages, as follows:{% endtrans %}

+
+
{% trans %}Site name, slogan, and email address{% endtrans %}
+
{% trans %}On the Basic site settings page, which you can reach in the main Manage administrative menu, by navigating to Configuration > System > Basic site settings.{% endtrans %}
+
{% trans %}Time zone and country{% endtrans %}
+
{% trans %}On the Regional settings page, which you can reach in the main Manage administrative menu, by navigating to Configuration > Regional and language > Regional settings.{% endtrans %}
+
{% trans %}Date and time formats{% endtrans %}
+
{% trans %}On the Date and time formats page, which you can reach in the main Manage administrative menu, by navigating to Configuration > Regional and language > Date and time formats.{% endtrans %}
+
diff --git a/core/modules/help_topics/help_topics/config-error.html.twig b/core/modules/help_topics/help_topics/config-error.html.twig new file mode 100644 index 0000000000..ef1fe5d26a --- /dev/null +++ b/core/modules/help_topics/help_topics/config-error.html.twig @@ -0,0 +1,10 @@ + + +{% set log_settings_url = render_var(url('system.logging_settings')) %} +{% set site_settings_url = render_var(url('system.site_information_settings')) %} +

{% trans %}Configuring 403/404 pages{% endtrans %}

+

{% trans %}The core software provides default responses for 403 response (Not Authorized: when someone tries to visit a page they do not have permission to see) and 404 response (Not Found: when someone tries to visit a page that does not exist). You can change what page is displayed for these responses on the Basic site settings page, which you can reach in the main Manage administrative menu, by navigating to Configuration > System > Basic site settings. Note that the pages you want to use must already exist as either system-provided pages or content that you have created.{% endtrans %}

+

{% trans %}Responding to software errors{% endtrans %}

+

{% trans %}Software errors on your site are logged, if you have a logging module installed (such as the core Database Logging module or the core Syslog module). You can configure whether or not error messages are also shown (to both administrators and other site visitors) on the Logging and errors configuration page, which you can reach in the main Manage administrative menu, by navigating to Configuration > Development > Logging and errors.{% endtrans %}

+

{% trans %}Viewing the site log{% endtrans %}

+

{% trans %}If you have the core Database Logging module installed, you can view recent error and informational messages by navigating in the main Manage administrative menu to Reports > Recent log messages. If you are using the core Syslog module for logging, error messages will be logged in your web server''s log files.{% endtrans %}

diff --git a/core/modules/help_topics/help_topics/help-topic-writing.html.twig b/core/modules/help_topics/help_topics/help-topic-writing.html.twig new file mode 100644 index 0000000000..64e77efad4 --- /dev/null +++ b/core/modules/help_topics/help_topics/help-topic-writing.html.twig @@ -0,0 +1,9 @@ + + +

{% trans %}Here are some suggestions for how to make your help topics as useful as possible for readers:{% endtrans %}

+ diff --git a/core/modules/help_topics/help_topics/maintenance.html.twig b/core/modules/help_topics/help_topics/maintenance.html.twig new file mode 100644 index 0000000000..ed2c6fb837 --- /dev/null +++ b/core/modules/help_topics/help_topics/maintenance.html.twig @@ -0,0 +1,3 @@ + + +

{% trans %}The related topics listed here will help you keep your site running and troubleshoot problems.{% endtrans %}

diff --git a/core/modules/help_topics/help_topics/menu-overview.html.twig b/core/modules/help_topics/help_topics/menu-overview.html.twig new file mode 100644 index 0000000000..8973a3a00a --- /dev/null +++ b/core/modules/help_topics/help_topics/menu-overview.html.twig @@ -0,0 +1,3 @@ + + +

{% trans %}The related topics listed here describe how to set up various aspects of site navigation and URLs.{% endtrans %}

diff --git a/core/modules/help_topics/help_topics/security-account-settings.html.twig b/core/modules/help_topics/help_topics/security-account-settings.html.twig new file mode 100644 index 0000000000..92f4bf0d9e --- /dev/null +++ b/core/modules/help_topics/help_topics/security-account-settings.html.twig @@ -0,0 +1,10 @@ + + +{% set account_settings_url = render_var(url('entity.user.admin_form')) %} +

{% trans %}On the Account settings page, which you can reach from the Manage administrative menu, by navigating to Configuration > People > Account settings (requires the Administer account settings permission), you can configure several settings related to how user accounts are created:{% endtrans %}

+ diff --git a/core/modules/help_topics/help_topics/security.html.twig b/core/modules/help_topics/help_topics/security.html.twig new file mode 100644 index 0000000000..5b742b6e99 --- /dev/null +++ b/core/modules/help_topics/help_topics/security.html.twig @@ -0,0 +1,4 @@ + + + +

{% trans %}The topics listed here will help you make and keep your site secure.{% endtrans %}

diff --git a/core/modules/help_topics/help_topics/ui-accessibility.html.twig b/core/modules/help_topics/help_topics/ui-accessibility.html.twig new file mode 100644 index 0000000000..2f38a11f2b --- /dev/null +++ b/core/modules/help_topics/help_topics/ui-accessibility.html.twig @@ -0,0 +1,7 @@ + + +

{% trans %}The following features of the administrative user interface may help administrative users with disabilities access your site:{% endtrans %}

+
+
{% trans %}Disabling drag-and-drop functionality{% endtrans %}
+
{% trans %}The default drag-and-drop user interface for ordering tables in the administrative interface presents a challenge for some users, including users of screen readers and other assistive technology. The drag-and-drop interface can be disabled in a table by clicking a link labeled Show row weights above the table. The replacement interface allows users to order the table by choosing numerical weights instead of dragging table rows.{% endtrans %}
+
diff --git a/core/modules/help_topics/help_topics/ui-components.html.twig b/core/modules/help_topics/help_topics/ui-components.html.twig new file mode 100644 index 0000000000..ffa064b914 --- /dev/null +++ b/core/modules/help_topics/help_topics/ui-components.html.twig @@ -0,0 +1,3 @@ + + +

{% trans %}The related topics listed here describe various aspects of the administrative interface, and tell how to use them.{% endtrans %}

diff --git a/core/modules/help_topics/help_topics/ui-contextual.html.twig b/core/modules/help_topics/help_topics/ui-contextual.html.twig new file mode 100644 index 0000000000..80bfe93845 --- /dev/null +++ b/core/modules/help_topics/help_topics/ui-contextual.html.twig @@ -0,0 +1,11 @@ + + +

{% trans %}What are contextual links?{% endtrans %}

+

{% trans %}Contextual links give users with the Use contextual links permission quick access to administrative tasks related to areas of non-administrative pages. For example, if a page on your site displays a block, the block would have a contextual link that would allow users with permission to configure the block. If the block contains a menu or a view, it would also have a contextual link for editing the menu links or the view. Clicking a contextual link takes you to the related administrative page directly, without needing to navigate through the administrative menu system.{% endtrans %}

+

{% trans %}Displaying and using contextual links{% endtrans %}

+

{% trans %}If you have the core Contextual Links module installed, the contextual links related to an area on a page can be displayed by clicking the contextual links button in that area of the page. In most themes, this button looks like a pencil and is placed in the upper right corner of the page area (upper left for right-to-left languages); however, contextual links buttons are normally hidden. Here are two ways to make contextual links buttons visible:{% endtrans %}

+ +

{% trans %}While the contextual links button for the area of interest is visible, click the button to display the list of links for that area. Click a link in the list to perform the task.{% endtrans %}

diff --git a/core/modules/help_topics/help_topics/ui-shortcuts.html.twig b/core/modules/help_topics/help_topics/ui-shortcuts.html.twig new file mode 100644 index 0000000000..78fe091676 --- /dev/null +++ b/core/modules/help_topics/help_topics/ui-shortcuts.html.twig @@ -0,0 +1,8 @@ + + +

{% trans %}What are shortcuts?{% endtrans %}

+

{% trans %}Shortcuts are quick links to administrative pages; they are managed by the core Shortcut module. A site can have one or more shortcut sets, which can be shared by one or more users; each set contains one or more shortcuts. Users need Use shortcuts permission to view shortcuts; Edit current shortcut set permission to add, delete, or edit the shortcuts in the set assigned to them; and Select any shortcut set permission to select a different shortcut set when editing their user profile. There is also an Administer shortcuts permission, which allows an administrator to do any of these actions, and also permits assigning shortcut sets to other users.{% endtrans %}

+

{% trans %}Creating and deleting shortcuts{% endtrans %}

+

{% trans %}When viewing certain administrative pages, you will see a link that allows you to add the page to your current shortcut set. In the core Seven administrative theme, the link looks like a star, and is displayed next to the page title. If the page is already in your shortcut set, you will instead see a link that allows you to remove it.{% endtrans %}

+

{% trans %}Viewing and using shortcuts{% endtrans %}

+

{% trans %}If you have the core Toolbar module installed, click Shortcuts in the toolbar to display your shortcuts. Once they are displayed, click any link in the shortcut bar to go directly to the administrative page. If you are not using the Toolbar module, you can display shortcuts by placing the Shortcuts block in a region of your theme.{% endtrans %}

diff --git a/core/modules/help_topics/help_topics/ui-tours.html.twig b/core/modules/help_topics/help_topics/ui-tours.html.twig new file mode 100644 index 0000000000..0f69dd351f --- /dev/null +++ b/core/modules/help_topics/help_topics/ui-tours.html.twig @@ -0,0 +1,6 @@ + + +

{% trans %}What are tours?{% endtrans %}

+

{% trans %}The core Tour module provides users with tours, which are guided tours of the administrative interface. Each tour starts on a particular administrative page, and consists of one or more tips that highlight elements of the page, guide you through a workflow, or explain key concepts. Users need Access tour permission to view tours, and JavaScript must be enabled in their browsers.{% endtrans %}

+

{% trans %}Viewing tours{% endtrans %}

+

{% trans %}If a tour is available on a page, and you have the core Toolbar module installed, a Tour button will appear on the right end of the toolbar (left end for right-to-left languages). Click this button to view the first tip of the tour; click the Next button to advance to the next tip, and End tour at the end to close the tour.{% endtrans %}

diff --git a/core/modules/help_topics/src/Controller/HelpTopicPluginController.php b/core/modules/help_topics/src/Controller/HelpTopicPluginController.php new file mode 100644 index 0000000000..5de339d2ad --- /dev/null +++ b/core/modules/help_topics/src/Controller/HelpTopicPluginController.php @@ -0,0 +1,124 @@ +helpTopicPluginManager = $help_topic_plugin_manager; + $this->renderer = $renderer; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.help_topic'), + $container->get('renderer') + ); + } + + /** + * Displays a help topic page. + * + * @param string $id + * The plugin ID. Maps to the {id} placeholder in the + * help_topics.help_topic route. + * + * @return array + * A render array with the contents of a help topic page. + */ + public function viewHelpTopic($id) { + $build = []; + + if (!$this->helpTopicPluginManager->hasDefinition($id)) { + throw new NotFoundHttpException(); + } + /* @var \Drupal\help_topics\HelpTopicPluginInterface $help_topic */ + $help_topic = $this->helpTopicPluginManager->createInstance($id); + + $build['#body'] = $help_topic->getBody(); + + $this->renderer->addCacheableDependency($build, $help_topic); + + // Build the related topics section, starting with the list this topic + // says are related. + $links = []; + + $related = $help_topic->getRelated(); + foreach ($related as $other_id) { + if ($other_id !== $id) { + if (!$this->helpTopicPluginManager->hasDefinition($other_id)) { + break; + } + /** @var \Drupal\help_topics\HelpTopicPluginInterface $topic */ + $topic = $this->helpTopicPluginManager->createInstance($other_id); + $links[$other_id] = [ + 'title' => $topic->getLabel(), + 'url' => Url::fromRoute('help_topics.help_topic', ['id' => $other_id]), + ]; + $this->renderer->addCacheableDependency($build, $topic); + } + } + + if (count($links)) { + uasort($links, function ($a, $b) { + if ($a['title'] == $b['title']) { + return 0; + } + return ($a['title'] < $b['title']) ? -1 : 1; + }); + $build['#related'] = [ + '#theme' => 'links__related', + '#heading' => [ + 'text' => $this->t('Related topics'), + 'level' => 'h2', + ], + '#links' => $links, + ]; + } + + $build['#theme'] = 'help_topic'; + $build['#title'] = $help_topic->getLabel(); + return $build; + } + +} diff --git a/core/modules/help_topics/src/HelpBreadcrumbBuilder.php b/core/modules/help_topics/src/HelpBreadcrumbBuilder.php new file mode 100644 index 0000000000..a98b05fbc2 --- /dev/null +++ b/core/modules/help_topics/src/HelpBreadcrumbBuilder.php @@ -0,0 +1,53 @@ +stringTranslation = $string_translation; + } + + /** + * {@inheritdoc} + */ + public function applies(RouteMatchInterface $route_match) { + return $route_match->getRouteName() == 'help_topics.help_topic'; + } + + /** + * {@inheritdoc} + */ + public function build(RouteMatchInterface $route_match) { + $breadcrumb = new Breadcrumb(); + $breadcrumb->addLink(Link::createFromRoute($this->t('Home'), '')); + $breadcrumb->addLink(Link::createFromRoute($this->t('Administration'), 'system.admin')); + $breadcrumb->addLink(Link::createFromRoute($this->t('Help'), 'help.main')); + + return $breadcrumb; + } + +} diff --git a/core/modules/help_topics/src/HelpTopicDiscovery.php b/core/modules/help_topics/src/HelpTopicDiscovery.php new file mode 100644 index 0000000000..54a7633fdd --- /dev/null +++ b/core/modules/help_topics/src/HelpTopicDiscovery.php @@ -0,0 +1,147 @@ +directories = $directories; + } + + /** + * {@inheritdoc} + */ + public function getDefinitions() { + $plugins = $this->findAll(); + + // Flatten definitions into what's expected from plugins. + $definitions = []; + foreach ($plugins as $list) { + foreach ($list as $id => $definition) { + $definitions[$id] = $definition; + } + } + + return $definitions; + } + + /** + * Returns an array of discoverable items. + * + * @return array + * An array of discovered data keyed by provider. + * + * @throws \Drupal\Component\Discovery\DiscoveryException + * Exception thrown if there is a problem during discovery. + */ + public function findAll() { + $all = []; + + $files = $this->findFiles(); + + $file_cache = FileCacheFactory::get('help_topic_discovery:help_topics'); + + // Try to load from the file cache first. + foreach ($file_cache->getMultiple(array_keys($files)) as $file => $data) { + $all[$files[$file]][$data['id']] = $data; + unset($files[$file]); + } + + // If there are files left that were not returned from the cache, load and + // parse them now. This list was flipped above and is keyed by filename. + if ($files) { + foreach ($files as $file => $provider) { + $data = [ + // The plugin ID is derived from the filename. The extension + // '.html.twig' is removed + 'id' => substr(basename($file), 0, -10), + 'provider' => $provider, + static::FILE_KEY => $file, + ]; + + // Get the rest of the plugin definition from meta tags contained in the + // help topic twig template. + foreach (get_meta_tags($file) as $key => $value) { + $key = substr($key, 11); + switch ($key) { + case 'related': + $data[$key] = explode(',', $value); + break; + case 'top_level': + $data[$key] = strtolower($value) === 'true' ? TRUE : FALSE; + break; + case 'label': + $data[$key] = new TranslatableMarkup($value); + break; + default: + throw new DiscoveryException("The $file contains invalid meta tag with name='$key'"); + } + } + + $all[$provider][$data['id']] = $data; + $file_cache->set($file, $data); + } + } + + return $all; + } + + /** + * Returns an array of providers keyed by file path. + * + * @return array + * An array of providers keyed by file path. + */ + protected function findFiles() { + $file_list = []; + foreach ($this->directories as $provider => $directories) { + $directories = (array) $directories; + foreach ($directories as $directory) { + if (is_dir($directory)) { + /** @var \SplFileInfo $fileInfo */ + $iterator = new RegexDirectoryIterator($directory, '/\.html\.twig$/i'); + foreach ($iterator as $fileInfo) { + $file_list[$fileInfo->getPathname()] = $provider; + } + } + } + } + return $file_list; + } + +} diff --git a/core/modules/help_topics/src/HelpTopicPluginBase.php b/core/modules/help_topics/src/HelpTopicPluginBase.php new file mode 100644 index 0000000000..4988d54dff --- /dev/null +++ b/core/modules/help_topics/src/HelpTopicPluginBase.php @@ -0,0 +1,149 @@ +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') + ); + } + + /** + * The name of the module or theme providing the help topic. + */ + public function getProvider() { + return $this->pluginDefinition['provider']; + } + + /** + * {@inheritdoc} + */ + public function getLabel() { + return $this->pluginDefinition['label']; + } + + /** + * {@inheritdoc} + */ + public function isTopLevel() { + return $this->pluginDefinition['top_level']; + } + + /** + * {@inheritdoc} + */ + public function getRelated() { + return $this->pluginDefinition['related']; + } + + /** + * {@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); + } + + /** + * {@inheritdoc} + */ + public function toLink($text = NULL, array $options = []) { + if (!$text) { + $text = $this->getLabel(); + } + return Link::createFromRoute($text, 'help_topics.help_topic', ['id' => $this->getPluginId()], $options); + } + +} diff --git a/core/modules/help_topics/src/HelpTopicPluginInterface.php b/core/modules/help_topics/src/HelpTopicPluginInterface.php new file mode 100644 index 0000000000..9c90c4396a --- /dev/null +++ b/core/modules/help_topics/src/HelpTopicPluginInterface.php @@ -0,0 +1,95 @@ + '', + // The title of the help topic plugin. + 'label' => '', + // Whether or not the topic should appear on the help topics list. + 'top_level' => '', + // List of related topic machine names. + 'related' => [], + 'class' => HelpTopicTwig::class, + 'cache_tag' => '', + ]; + + /** + * The theme handler. + * + * @var \Drupal\Core\Extension\ThemeHandlerInterface + */ + protected $themeHandler; + + /** + * Constructs a new HelpTopicManager object. + * + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler. + * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler + * The theme handler. + * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend + * Cache backend instance to use. + */ + public function __construct(ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache_backend) { + $this->moduleHandler = $module_handler; + $this->themeHandler = $theme_handler; + $this->alterInfo('help_topics_info'); + $this->setCacheBackend($cache_backend, 'help_topics'); + } + + /** + * {@inheritdoc} + */ + protected function getDiscovery() { + if (!isset($this->discovery)) { + // We want to find help topic plugins in both modules and themes in + // a sub-directory called help_topics. + $directories = array_merge($this->moduleHandler->getModuleDirectories(), $this->themeHandler->getThemeDirectories()); + + $directories = array_map(function ($dir) { + return [$dir . '/help_topics']; + }, $directories); + + $this->discovery = new HelpTopicDiscovery($directories); + } + return $this->discovery; + } + + /** + * {@inheritdoc} + */ + protected function providerExists($provider) { + return $this->moduleHandler->moduleExists($provider) || $this->themeHandler->themeExists($provider); + } + + /** + * Checks to see if plugin is provided by an uninstalled module or theme. + */ + public function alterDefinitions(&$definitions) { + parent::alterDefinitions($definitions); + + foreach ($definitions as $plugin_id => $plugin_definition) { + foreach ($plugin_definition['related'] as $key => $related_id) { + // If the related help topic does not exist it might be for a module + // that is not installed. Remove it. This silent removal is a bit + // worrying but how else do we support optional relations? + if (!isset($definitions[$related_id])) { + unset($definitions[$plugin_id]['related'][$key]); + continue; + } + // Ensure that the other plugin is related to this ID. + if (isset($definitions[$related_id]) && !in_array($plugin_id, $definitions[$related_id]['related'], TRUE)) { + $definitions[$related_id]['related'][] = $plugin_id; + } + } + } + } + + /** + * {@inheritdoc} + */ + public function getThemeImplementations() { + $twigPlugins = array_filter($this->getDefinitions(), function (array $definition) { + return $definition['class'] === HelpTopicTwig::class; + }); + $return = [ + 'help_topic' => [ + 'variables' => [ + 'body' => [], + 'related' => [], + ], + ], + ]; + foreach ($twigPlugins as $pluginId => $definition) { + $hook = HelpTopicTwig::getThemeHook($pluginId); + if ($this->moduleHandler->moduleExists($definition['provider'])) { + $provider_path = $this->moduleHandler->getModule($definition['provider'])->getPath(); + } + else { + $provider_path = $this->themeHandler->getTheme($definition['provider'])->getPath(); + } + $return[$hook] = [ + 'variables' => [], + 'path' => $provider_path . '/help_topics', + 'template' => $pluginId, + ]; + } + return $return; + } + +} diff --git a/core/modules/help_topics/src/HelpTopicPluginManagerInterface.php b/core/modules/help_topics/src/HelpTopicPluginManagerInterface.php new file mode 100644 index 0000000000..34ea6504eb --- /dev/null +++ b/core/modules/help_topics/src/HelpTopicPluginManagerInterface.php @@ -0,0 +1,27 @@ + self::getThemeHook($this->getPluginId()), + ]; + } + + /** + * Gets the theme hook from the plugin ID. + * + * @param string $plugin_id + * Plugin id. + * + * @return string + * Theme hook. + */ + public static function getThemeHook($plugin_id) { + return Html::cleanCssIdentifier('help_topic_' . $plugin_id); + } + +} diff --git a/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php b/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php new file mode 100644 index 0000000000..97b0f4c3e9 --- /dev/null +++ b/core/modules/help_topics/src/Plugin/HelpSection/HelpTopicSection.php @@ -0,0 +1,134 @@ +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') + ); + } + + /** + * {@inheritdoc} + */ + public function getCacheTags() { + if (!isset($this->topicList)) { + $this->setProperties(); + } + + return $this->cacheTagList; + } + + /** + * {@inheritdoc} + */ + public function listTopics() { + if (!isset($this->topicList)) { + $this->setProperties(); + } + + return $this->topicList; + } + + /** + * Sets the topic list and cache tags properties. + */ + protected function setProperties() { + $definitions = $this->pluginManager->getDefinitions(); + + // 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()); + } + } + $this->cacheTagList = array_unique($this->cacheTagList); + + // 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; + } + 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); + } + +} diff --git a/core/modules/help_topics/templates/help-topic.html.twig b/core/modules/help_topics/templates/help-topic.html.twig new file mode 100644 index 0000000000..cde8333355 --- /dev/null +++ b/core/modules/help_topics/templates/help-topic.html.twig @@ -0,0 +1,16 @@ +{# +/** + * @file + * Default theme implementation to display a help topic. + * + * Available variables: + * - body: The body of the topic. + * - related: List of related topic links. + * + * @ingroup themeable + */ +#} +
+ {{ body }} + {{ related }} +
diff --git a/core/modules/help_topics/tests/modules/help_topics_test/help_topics/help-test-additional.html.twig b/core/modules/help_topics/tests/modules/help_topics_test/help_topics/help-test-additional.html.twig new file mode 100644 index 0000000000..f10e221ac3 --- /dev/null +++ b/core/modules/help_topics/tests/modules/help_topics_test/help_topics/help-test-additional.html.twig @@ -0,0 +1,3 @@ + + +

{% trans %}This topic should get listed automatically on the Help test topic.{% endtrans %}

diff --git a/core/modules/help_topics/tests/modules/help_topics_test/help_topics/help-test-linked.html.twig b/core/modules/help_topics/tests/modules/help_topics_test/help_topics/help-test-linked.html.twig new file mode 100644 index 0000000000..686df2b007 --- /dev/null +++ b/core/modules/help_topics/tests/modules/help_topics_test/help_topics/help-test-linked.html.twig @@ -0,0 +1,2 @@ + +

{% trans %}This topic is not supposed to be top-level.{% endtrans %}

diff --git a/core/modules/help_topics/tests/modules/help_topics_test/help_topics/help-test.html.twig b/core/modules/help_topics/tests/modules/help_topics_test/help_topics/help-test.html.twig new file mode 100644 index 0000000000..5cac9f9b69 --- /dev/null +++ b/core/modules/help_topics/tests/modules/help_topics_test/help_topics/help-test.html.twig @@ -0,0 +1,5 @@ + + + +{% set help_topic_url = render_var(url('help_topics.help_topic', {id: 'help-topic-writing'})) %} +

{% trans %}This is a test. It should link to the writing good help topic. Also there should be a related topic link below to the Help module topic page and the linked topic.{% endtrans %}

diff --git a/core/modules/help_topics/tests/modules/help_topics_test/help_topics_test.info.yml b/core/modules/help_topics/tests/modules/help_topics_test/help_topics_test.info.yml new file mode 100644 index 0000000000..155c43d565 --- /dev/null +++ b/core/modules/help_topics/tests/modules/help_topics_test/help_topics_test.info.yml @@ -0,0 +1,7 @@ +# The name of this module is deliberately different from its machine +# name to test the presented order of help topics. +name: 'ABC Help Test' +type: module +description: 'Support module for help testing.' +package: Testing +core: 8.x diff --git a/core/modules/help_topics/tests/modules/help_topics_test/help_topics_test.module b/core/modules/help_topics/tests/modules/help_topics_test/help_topics_test.module new file mode 100644 index 0000000000..a6724446cf --- /dev/null +++ b/core/modules/help_topics/tests/modules/help_topics_test/help_topics_test.module @@ -0,0 +1,18 @@ +install(['seven', 'help_topic_test_theme']); + \Drupal::service('config.factory')->getEditable('system.theme')->set('admin', 'seven')->save(); + + // Place various blocks. + $settings = [ + 'theme' => 'seven', + 'region' => 'help', + ]; + $this->placeBlock('help_block', $settings); + $this->placeBlock('local_tasks_block', $settings); + $this->placeBlock('local_actions_block', $settings); + $this->placeBlock('page_title_block', $settings); + + // Create users. + $this->adminUser = $this->createUser([ + 'access administration pages', + 'view the administration theme', + 'administer permissions', + ]); + $this->anyUser = $this->createUser([]); + } + + /** + * Tests the main help page and individual pages for topics. + */ + public function testHelp() { + // Log in the regular user. + $this->drupalLogin($this->anyUser); + $this->verifyHelp(403); + + // Log in the admin user. + $this->drupalLogin($this->adminUser); + $this->verifyHelp(); + $this->verifyHelpLinks(); + $this->testBreadCrumb(); + + // Verify that help topics text appears on admin/help. + $this->drupalGet('admin/help'); + $session = $this->assertSession(); + $session->responseContains('

Topics

'); + $session->pageTextContains('Topics can be provided by modules or themes'); + + // Verify links for for help topics and order. + $page_text = $this->getTextContent(); + $start = strpos($page_text, 'Topics can be provided'); + $pos = $start; + foreach ($this->getTopicList() as $info) { + $name = $info['name']; + $session->linkExists($name); + $new_pos = strpos($page_text, $name, $start); + $this->assertTrue($new_pos > $pos, 'Order of ' . $name . ' is correct on page'); + $pos = $new_pos; + } + + // Ensue all the expected links are present before uninstalling. + $session->linkExists('ABC Help Test module'); + $session->linkExists('ABC Help Test'); + $session->linkExists('XYZ Help Test theme'); + + // Uninstall the test module and verify the topics are gone, after + // reloading page. + $this->container->get('module_installer')->uninstall(['help_topics_test']); + $this->drupalGet('admin/help'); + $session->linkNotExists('ABC Help Test module'); + $session->linkNotExists('ABC Help Test'); + $session->linkExists('XYZ Help Test theme'); + + // Uninstall the test theme and verify the topic is gone. + $this->container->get('theme_installer')->uninstall(['help_topic_test_theme']); + $this->drupalGet('admin/help'); + $session->linkNotExists('XYZ Help Test theme'); + } + + /** + * Verifies the logged in user has access to various help links and pages. + * + * @param int $response + * (optional) The HTTP response code to test for. If it's 200 (default), + * the test verifies the user sees the help; if it's not, it verifies they + * are denied access. + */ + protected function verifyHelp($response = 200) { + // Verify access to help topic pages. + foreach ($this->getTopicList() as $topic => $info) { + // View help topic page. + $this->drupalGet('admin/help/topic/' . $topic); + $session = $this->assertSession(); + $session->statusCodeEquals($response); + if ($response == 200) { + $name = $info['name']; + $session->titleEquals($name . ' | Drupal'); + $session->responseContains('

' . $name . '

'); + } + } + } + + /** + * Verifies links on the test help topic page and other pages. + * + * Assumes an admin user is logged in. + */ + protected function verifyHelpLinks() { + $session = $this->assertSession(); + // Verify links on the test top-level page. + $page = 'admin/help/topic/help-test'; + $links = [ + 'link to the writing good help topic' => 'Writing good help', + 'Linked topic' => 'This topic is not supposed to be top-level', + 'Additional topic' => 'This topic should get listed automatically', + ]; + foreach ($links as $link_text => $page_text) { + $this->drupalGet($page); + $this->clickLink($link_text); + $session->pageTextContains($page_text); + } + + // Verify theme provided help topics work and can be related. + $this->drupalGet('admin/help/topic/help-test-theme'); + $session->pageTextContains('This is a theme provided topic.'); + $this->clickLink('Additional topic'); + $this->assertLink('XYZ Help Test theme'); + + // Verify that the non-top-level topics do not appear on the Help page. + $this->drupalGet('admin/help'); + $session->linkNotExists('Linked topic'); + $session->linkNotExists('Additional topic'); + } + + /** + * Gets a list of topic IDs to test. + * + * @return array + * A list of topics to test, in the order in which they should appear. The + * 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 [ + 'help-test' => [ + 'name' => 'ABC Help Test module', + ], + 'help-topic-writing' => [ + 'name' => 'Writing good help', + ], + ]; + } + + /** + * Tests breadcrumb on a help topic page. + */ + public function testBreadCrumb() { + // Prepare common base breadcrumb elements. + $home = ['' => 'Home']; + $admin = $home + ['admin' => t('Administration')]; + + $page = 'admin/help/topic/help-test'; + $this->drupalGet($page); + + // Verify Help Topics administration breadcrumbs. + $trail = $admin + [ + 'admin/help' => t('Help'), + ]; + $this->assertBreadcrumb('admin/help/topic/help-test', $trail); + + } + +} diff --git a/core/modules/help_topics/tests/themes/help_topic_test_theme/help_topic_test_theme.info.yml b/core/modules/help_topics/tests/themes/help_topic_test_theme/help_topic_test_theme.info.yml new file mode 100644 index 0000000000..70c6951772 --- /dev/null +++ b/core/modules/help_topics/tests/themes/help_topic_test_theme/help_topic_test_theme.info.yml @@ -0,0 +1,5 @@ +name: Test Help Topics +type: theme +description: A theme to test help topics. +version: VERSION +core: 8.x diff --git a/core/modules/help_topics/tests/themes/help_topic_test_theme/help_topics/help-test-theme.html.twig b/core/modules/help_topics/tests/themes/help_topic_test_theme/help_topics/help-test-theme.html.twig new file mode 100644 index 0000000000..b81ee58afc --- /dev/null +++ b/core/modules/help_topics/tests/themes/help_topic_test_theme/help_topics/help-test-theme.html.twig @@ -0,0 +1,4 @@ + + + +

{% trans %}This is a theme provided topic.{% endtrans %}