Display explanation or submission guidelines on a custom route

Submitted by victor.bourgade on

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.

Submission guideline in a 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 taking 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.

Tags

Drupal 8 Drupal 9

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.