You are here

public function HelpTopicDiscovery::findAll in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/help_topics/src/HelpTopicDiscovery.php \Drupal\help_topics\HelpTopicDiscovery::findAll()

Returns an array of discoverable items.

Return value

array An array of discovered data keyed by provider.

Throws

\Drupal\Component\Discovery\DiscoveryException Exception thrown if there is a problem during discovery.

1 call to HelpTopicDiscovery::findAll()
HelpTopicDiscovery::getDefinitions in core/modules/help_topics/src/HelpTopicDiscovery.php
Gets the definition of all plugins for this type.

File

core/modules/help_topics/src/HelpTopicDiscovery.php, line 83

Class

HelpTopicDiscovery
Discovers help topic plugins from Twig files in help_topics directories.

Namespace

Drupal\help_topics

Code

public function findAll() {
  $all = [];
  $files = $this
    ->findFiles();
  $file_cache = FileCacheFactory::get('help_topic_discovery:help_topics');

  // Try to load from the file cache first.
  foreach ($file_cache
    ->getMultiple(array_keys($files)) as $file => $data) {
    $all[$files[$file]][$data['id']] = $data;
    unset($files[$file]);
  }

  // If there are files left that were not returned from the cache, load and
  // parse them now. This list was flipped above and is keyed by filename.
  if ($files) {
    foreach ($files as $file => $provider) {
      $plugin_id = substr(basename($file), 0, -10);

      // The plugin ID begins with provider.
      list($file_name_provider, ) = explode('.', $plugin_id, 2);

      // Only the Help Topics module can provide help for other extensions.
      // @todo https://www.drupal.org/project/drupal/issues/3072312 Remove
      //   help_topics special case once Help Topics is stable and core
      //   modules can provide their own help topics.
      if ($provider !== 'help_topics' && $provider !== $file_name_provider) {
        throw new DiscoveryException("{$file} file name should begin with '{$provider}'");
      }
      $data = [
        // The plugin ID is derived from the filename. The extension
        // '.html.twig' is removed.
        'id' => $plugin_id,
        'provider' => $file_name_provider,
        'class' => HelpTopicTwig::class,
        static::FILE_KEY => $file,
      ];

      // Get the rest of the plugin definition from front matter contained in
      // the help topic Twig file.
      try {
        $front_matter = FrontMatter::create(file_get_contents($file), Yaml::class)
          ->getData();
      } catch (InvalidDataTypeException $e) {
        throw new DiscoveryException(sprintf('Malformed YAML in help topic "%s": %s.', $file, $e
          ->getMessage()));
      }
      foreach ($front_matter as $key => $value) {
        switch ($key) {
          case 'related':
            if (!is_array($value)) {
              throw new DiscoveryException("{$file} contains invalid value for 'related' key, the value must be an array of strings");
            }
            $data[$key] = $value;
            break;
          case 'top_level':
            if (!is_bool($value)) {
              throw new DiscoveryException("{$file} contains invalid value for 'top_level' key, the value must be a Boolean");
            }
            $data[$key] = $value;
            break;
          case 'label':
            $data[$key] = new TranslatableMarkup($value);
            break;
          default:
            throw new DiscoveryException("{$file} contains invalid key='{$key}'");
        }
      }
      if (!isset($data['label'])) {
        throw new DiscoveryException("{$file} does not contain the required key with name='label'");
      }
      $all[$provider][$data['id']] = $data;
      $file_cache
        ->set($file, $data);
    }
  }
  return $all;
}