Get rid of hard-coded markup in hook_help() implementations

Created on 4 February 2014, almost 11 years ago
Updated 18 July 2023, over 1 year ago

Updated: Comment #N

Problem/Motivation

There are a couple of problems with how we are doing hook_help() implementations currently in Drupal 8 and all previous versions. Here is a typical implementation:

/**
 * Implements hook_help().
 */
function book_help($path, $arg) {
  switch ($path) {
    case 'admin/help#book':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Book module is used for creating structured, multi-page content, such as site resource guides, manuals, and wikis. It allows you to create content that has chapters, sections, subsections, or any similarly-tiered structure. For more information, see the <a href="!book">online documentation for the Book module</a>.', array('!book' => 'https://drupal.org/documentation/modules/book')) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Adding and managing book content') . '</dt>';
      $output .= '<dd>' . t('You can assign separate permissions for <em>creating new books</em> as well as <em>creating</em>, <em>editing</em> and <em>deleting</em> book content. Users with the <em>Administer book outlines</em> permission can add <em>any</em> type of content to a book by selecting the appropriate book outline while editing the content. They can also view a list of all books, and edit and rearrange section titles on the <a href="!admin-book">Book administration page</a>.', array('!admin-book' => \Drupal::url('book.admin'))) . '</dd>';
      $output .= '<dt>' . t('Book navigation') . '</dt>';
 ...

Here are some problems with the current hook_help() standard, which this issue may be able to fix, and some additional concerns:

  1. Generally, returning HTML markup and text from a PHP function like this is frowned upon. Markup should be in Twig templates instead.
  2. The current syntax is difficult for Documentation writers, who may not be PHP programmers.
  3. Ideally, it would be good to make it easy for people to follow the standard format of "About" / "Uses" (and individual uses items). Note that in some cases, the individual Uses items have more than one paragraph. Currently this is just done in an ad-hoc way (we publish a page that has the standards for how to write hook_help() and hope people follow it).
  4. We need the help text to be translated (which it is currently -- can't lose that).
  5. When translating the individual strings in the current syntax, the context can get lost. However, we also don't want the markup, or any other stuff like Twig directives, to be in the translation strings, and we don't want translated strings to be very long (because fixing a small typo or grammatical error would mean a completely new string for translators to tackle, as opposed to only a small chunk to re-translate). See #1918856-45: Put each module's help into a separate Twig file β†’ for details on this subject.
  6. As a note, the help has a lot of links in it, in general. Some go to external URLs and some to within-site pages.

Proposed resolution

There are several possible ways to solve this problem:

0. Since we want to keep translation text chunks relatively small and without markup, would it be possible to use translation context to help group them? [This is probably independent of how the rest of the issues are solved.]

1. We could make a single "standard help" Twig template, which would output the standard About/Uses markup. Then hook_help() could return a renderable array something like this:

return array(
  '#theme' => 'help-standard',
  '#about' => t('The about text goes here'),
  '#uses' => array(
      array(
        '#header' => t('Using this module'),
        '#text' => t('Description here'),
     ),
     array(
       '#header' => t('Doing something else'),
       '#text' => array(t('First paragraph'), t('Second paragraph')),
    ),
);

Presumably if a hook_help() wanted to return something completely different, or add something after the standard About/Uses, they could do something different in their render array.

2. We could put the About/Uses text into some kind of a YML file. Docs writers are already using YML for Tours and it's much easier to deal with than PHP concatenation. Question: If it is a YML config file, what happens when Core has an update and a site already has its help config stored -- would it get updated? And if it is some other type of YML file (not part of the config system), would it be automatically translated and what would that buy us?

3. There's a proposal on #1918856: Put each module's help into a separate Twig file β†’ to make each individual hook_help() implementation have its own Twig template.

4. Some kind of README file with Markdown format.

5. There may be other ideas?

Remaining tasks

a) Figure out which idea is the best for documentation writers, module developers, and translators.

b) Implement it in a patch.

User interface changes

None.

API changes

hook_help() would be changed in some way (yet to be determined).

πŸ“Œ Task
Status

Postponed: needs info

Version

9.5

Component
HelpΒ  β†’

Last updated 12 days ago

No maintainer
Created by

πŸ‡ΊπŸ‡ΈUnited States jhodgdon Spokane, WA, USA

Live updates comments and jobs are added and updated live.
  • API change

    Changes an existing API or subsystem. Not backportable to earlier major versions, unless absolutely required to fix a critical bug.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024