You are here

public function WebformHelpManager::buildHelp in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformHelpManager.php \Drupal\webform\WebformHelpManager::buildHelp()

Build help for specific route.

Parameters

string $route_name: The route for which to find help.

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match object from which to find help.

Return value

array An render array containing help for specific route.

Overrides WebformHelpManagerInterface::buildHelp

File

src/WebformHelpManager.php, line 245

Class

WebformHelpManager
Webform help manager.

Namespace

Drupal\webform

Code

public function buildHelp($route_name, RouteMatchInterface $route_match) {
  $is_help_disabled = $this->configFactory
    ->get('webform.settings')
    ->get('ui.help_disabled');

  // Get path from route match.
  $path = preg_replace('/^' . preg_quote(base_path(), '/') . '/', '/', Url::fromRouteMatch($route_match)
    ->setAbsolute(FALSE)
    ->toString());
  $build = [];
  foreach ($this->help as $id => $help) {

    // Set default values.
    $help += [
      'routes' => [],
      'paths' => [],
      'access' => TRUE,
      'message_type' => '',
      'message_close' => FALSE,
      'message_id' => '',
      'message_storage' => '',
      'video_id' => '',
      'attached' => [],
    ];
    if (!$help['access']) {
      continue;
    }
    $is_route_match = in_array($route_name, $help['routes']);
    $is_path_match = $help['paths'] && $this->pathMatcher
      ->matchPath($path, implode(PHP_EOL, $help['paths']));
    $has_help = $is_route_match || $is_path_match;
    if (!$has_help) {
      continue;
    }

    // Check is help is disabled.  Messages are always displayed.
    if ($is_help_disabled && empty($help['message_type'])) {
      continue;
    }
    if ($help['message_type']) {
      $build[$id] = [
        '#type' => 'webform_message',
        '#message_type' => $help['message_type'],
        '#message_close' => $help['message_close'],
        '#message_id' => $help['message_id'] ? $help['message_id'] : 'webform.help.' . $help['id'],
        '#message_storage' => $help['message_storage'],
        '#message_message' => [
          '#theme' => 'webform_help',
          '#info' => $help,
        ],
        '#attached' => $help['attached'],
      ];
      if ($help['message_close']) {
        $build['#cache']['max-age'] = 0;
      }
    }
    else {
      $build[$id] = [
        '#theme' => 'webform_help',
        '#info' => $help,
      ];
    }

    // Add custom help weight.
    if (isset($help['weight'])) {
      $build[$id]['#weight'] = $help['weight'];
    }
  }

  // Disable caching when Webform editorial module is enabled.
  if ($this->moduleHandler
    ->moduleExists('webform_editorial') && $build) {
    $build['#cache']['max-age'] = 0;
  }
  return $build;
}