Verified Commit c57ce025 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3420993 by Amber Himes Matz, andypost, sakthi_dev, smustgrave,...

Issue #3420993 by Amber Himes Matz, andypost, sakthi_dev, smustgrave, larowlan, catch, quietone: Convert HelpSection plugin discovery to attributes

(cherry picked from commit b9dfd470)
parent 890e58d7
Loading
Loading
Loading
Loading
Loading
+57 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\help\Attribute;

use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines a HelpSection attribute object for plugin discovery.
 *
 * Plugin Namespace: Plugin\HelpSection
 *
 * For a working example, see \Drupal\help\Plugin\HelpSection\HookHelpSection.
 *
 * @see \Drupal\help\HelpSectionPluginInterface
 * @see \Drupal\help\Plugin\HelpSection\HelpSectionPluginBase
 * @see \Drupal\help\HelpSectionManager
 * @see hook_help_section_info_alter()
 * @see plugin_api
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class HelpSection extends Plugin {

  /**
   * Constructs a HelpSection attribute.
   *
   * @param string $id
   *   The plugin ID.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $title
   *   The text to use as the title of the help page section.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description
   *   (optional) The description of the help page section.
   * @param string|null $permission
   *   (optional) The permission required to access the help page section.
   *
   *   Only set if this section needs its own permission, beyond the generic
   *   'access help pages' permission needed to see the /admin/help
   *   page itself.
   * @param int|null $weight
   *   (optional) The weight of the help page section.
   * @param string|null $deriver
   *   (optional) The deriver class.
   *
   *   The sections will be ordered by this weight on the help page.
   */
  public function __construct(
    public readonly string $id,
    public readonly TranslatableMarkup $title,
    public readonly ?TranslatableMarkup $description = NULL,
    public readonly ?string $permission = NULL,
    public readonly ?int $weight = NULL,
    public readonly ?string $deriver = NULL,
  ) {}

}
+2 −1
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\help;

use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\help\Attribute\HelpSection;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
@@ -36,7 +37,7 @@ class HelpSectionManager extends DefaultPluginManager {
   *   The module handler for the alter hook.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/HelpSection', $namespaces, $module_handler, 'Drupal\help\HelpSectionPluginInterface', 'Drupal\help\Annotation\HelpSection');
    parent::__construct('Plugin/HelpSection', $namespaces, $module_handler, 'Drupal\help\HelpSectionPluginInterface', HelpSection::class, 'Drupal\help\Annotation\HelpSection');

    $this->alterInfo('help_section_info');
    $this->setCacheBackend($cache_backend, 'help_section_plugins');
+8 −8
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@
namespace Drupal\help\Plugin\HelpSection;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\help\Attribute\HelpSection;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\help\SearchableHelpInterface;
use Drupal\help\HelpTopicPluginInterface;
use Drupal\help\HelpTopicPluginManagerInterface;
@@ -18,17 +20,15 @@
/**
 * Provides the help topics list section for the help page.
 *
 * @HelpSection(
 *   id = "help_topics",
 *   title = @Translation("Topics"),
 *   weight = -10,
 *   description = @Translation("Topics can be provided by modules or themes. Top-level help topics on your site:"),
 *   permission = "access help pages"
 * )
 *
 * @internal
 *   Plugin classes are internal.
 */
#[HelpSection(
  id: 'help_topics',
  title: new TranslatableMarkup('Topics'),
  description: new TranslatableMarkup('Topics can be provided by modules or themes. Top-level help topics on your site:'),
  weight: -10
)]
class HelpTopicSection extends HelpSectionPluginBase implements ContainerFactoryPluginInterface, SearchableHelpInterface {

  /**
+7 −6
Original line number Diff line number Diff line
@@ -3,19 +3,20 @@
namespace Drupal\help\Plugin\HelpSection;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Link;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\help\Attribute\HelpSection;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides the module topics list section for the help page.
 *
 * @HelpSection(
 *   id = "hook_help",
 *   title = @Translation("Module overviews"),
 *   description = @Translation("Module overviews are provided by modules. Overviews available for your installed modules:"),
 * )
 */
#[HelpSection(
  id: 'hook_help',
  title: new TranslatableMarkup('Module overviews'),
  description: new TranslatableMarkup('Module overviews are provided by modules. Overviews available for your installed modules:')
)]
class HookHelpSection extends HelpSectionPluginBase implements ContainerFactoryPluginInterface {

  /**
+7 −6
Original line number Diff line number Diff line
@@ -2,17 +2,18 @@

namespace Drupal\help_page_test\Plugin\HelpSection;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\help\Plugin\HelpSection\HelpSectionPluginBase;
use Drupal\help\Attribute\HelpSection;

/**
 * Provides an empty section for the help page, for testing.
 *
 * @HelpSection(
 *   id = "empty_section",
 *   title = @Translation("Empty section"),
 *   description = @Translation("This description should appear."),
 * )
 */
#[HelpSection(
  id: 'empty_section',
  title: new TranslatableMarkup('Empty section'),
  description: new TranslatableMarkup('This description should appear.')
)]
class EmptyHelpSection extends HelpSectionPluginBase {

  /**
Loading