You are here

class TourHelpSection in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/tour/src/Plugin/HelpSection/TourHelpSection.php \Drupal\tour\Plugin\HelpSection\TourHelpSection

Provides the tours list section for the help page.

Plugin annotation


@HelpSection(
  id = "tour",
  title = @Translation("Tours"),
  weight = 10,
  description = @Translation("Tours guide you through workflows or explain concepts on various user interface pages. The tours with links in this list are on user interface landing pages; the tours without links will show on individual pages (such as when editing a View using the Views UI module). Available tours:"),
  permission = "access tour"
)

Hierarchy

Expanded class hierarchy of TourHelpSection

File

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

Namespace

Drupal\tour\Plugin\HelpSection
View source
class TourHelpSection extends HelpSectionPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a TourHelpSection object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {

    // The calculation of which URL (if any) gets put on which tour depends
    // on a route access check. This can have a lot of inputs, including user
    // permissions and other factors. Rather than doing a complicated
    // accounting of the cache metadata for all of these possible factors, set
    // the max age of the cache to zero to prevent using incorrect cached
    // information.
    return 0;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
HelpSectionPluginBase::getDescription public function Returns the description text for the help section. Overrides HelpSectionPluginInterface::getDescription
HelpSectionPluginBase::getTitle public function Returns the title of the help section. Overrides HelpSectionPluginInterface::getTitle
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TourHelpSection::$entityTypeManager protected property The entity type manager.
TourHelpSection::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
TourHelpSection::getCacheMaxAge public function Overrides UnchangingCacheableDependencyTrait::getCacheMaxAge
TourHelpSection::listTopics public function Returns a list of topics to show in the help section. Overrides HelpSectionPluginInterface::listTopics
TourHelpSection::__construct public function Constructs a TourHelpSection object. Overrides PluginBase::__construct
UnchangingCacheableDependencyTrait::getCacheContexts public function 1
UnchangingCacheableDependencyTrait::getCacheTags public function 1