Display explanation or submission guidelines on a custom route

Submitted by victor.bourgade on Mon, 10/25/2021 - 10:41

If you have the "help" module activated (which is a default) in your Drupal website, you have probably noticed that you can display explanation or submission guidelines to your users for each content type.

These explanations or guidelines are displayed on the node add and edit form but, how can we display them on another route than "entity.node.edit_form" and "node.add", for example if you have implemented a custom add/edit form for your content type.

These guidelines are provided by the "Help" module and displayed in Drupal\help\Plugin\Block\HelpBlock, so make sure you have the Help block set for your custom theme in the block layout section.

Then having a look at how the node module implements it, it's quite easy to figure that we'll need to implement a hook_help as followed:

use Drupal\Component\Utility\Xss;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\Entity\NodeType;

/**
 * Implements hook_help().
 */
function my_module_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'my_module.custom_route_name':
      $type = NodeType::load('content_type_machine_name');
      $help = $type->getHelp();
      return (!empty($help) ? Xss::filterAdmin($help) : '');
  }
}

Clear the cache and check at your new route, you should see the help block displayed with the content of your submission guidelines from the content type you have entered.

About the writer

victor.bourgade

Victor is a web developer passionnated in drupal and bootstrap technologies. He likes challenges and beautiful designs.

When not behind his computer you'll find him drinking beers with friends or in the middle of nowhere hiking with his dog.