You are here

public function TourHelpSection::listTopics in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/tour/src/Plugin/HelpSection/TourHelpSection.php \Drupal\tour\Plugin\HelpSection\TourHelpSection::listTopics()

Returns a list of topics to show in the help section.

Return value

array A sorted list of topic links or render arrays for topic links. The links will be shown in the help section; if the returned array of links is empty, the section will be shown with some generic empty text.

Overrides HelpSectionPluginInterface::listTopics

File

core/modules/tour/src/Plugin/HelpSection/TourHelpSection.php, line 77

Class

TourHelpSection
Provides the tours list section for the help page.

Namespace

Drupal\tour\Plugin\HelpSection

Code

public function listTopics() {

  /** @var \Drupal\tour\TourInterface[] $tours */
  $tours = $this->entityTypeManager
    ->getStorage('tour')
    ->loadMultiple();

  // Sort in the manner defined by Tour.
  uasort($tours, [
    'Drupal\\tour\\Entity\\Tour',
    'sort',
  ]);

  // Make a link to each tour, using the first of its routes that can
  // be linked to by this user, if any.
  $topics = [];
  foreach ($tours as $tour) {
    $title = $tour
      ->label();
    $id = $tour
      ->id();
    $routes = $tour
      ->getRoutes();
    $made_link = FALSE;
    foreach ($routes as $route) {

      // Some tours are for routes with parameters. For instance, there is
      // currently a tour in the Language module for the language edit page,
      // which appears on all pages with URLs like:
      // /admin/config/regional/language/edit/LANGCODE.
      // There is no way to make a link to the page that displays the tour,
      // because it is a set of pages. The easiest way to detect this is to
      // use a try/catch exception -- try to make a link, and it will error
      // out with a missing parameter exception if the route leads to a set
      // of pages instead of a single page.
      try {
        $params = isset($route['route_params']) ? $route['route_params'] : [];
        $url = Url::fromRoute($route['route_name'], $params);

        // Skip this route if the current user cannot access it.
        if (!$url
          ->access()) {
          continue;
        }

        // Generate the link HTML directly, using toString(), to catch
        // missing parameter exceptions now instead of at render time.
        $topics[$id] = Link::fromTextAndUrl($title, $url)
          ->toString();

        // If the line above didn't generate an exception, we have a good
        // link that the user can access.
        $made_link = TRUE;
        break;
      } catch (\Exception $e) {

        // Exceptions are normally due to routes that need parameters. If
        // there is an exception, just try the next route and see if we can
        // find one that will work for us.
      }
    }
    if (!$made_link) {

      // None of the routes worked to make a link, so at least display the
      // tour title.
      $topics[$id] = $title;
    }
  }
  return $topics;
}