Verified Commit 41437f5b authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3421017 by mohit_aghera, smustgrave: Convert LanguageNegotiation plugin...

Issue #3421017 by mohit_aghera, smustgrave: Convert LanguageNegotiation plugin discovery to attributes

(cherry picked from commit 2001751f)
parent 17b3cf71
Loading
Loading
Loading
Loading
Loading
+54 −0
Changes for core/modules/language/src/Attribute/LanguageNegotiation.php: 54 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\language\Attribute;

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

/**
 * Defines a language negotiation attribute object.
 *
 * Plugin Namespace: Plugin\LanguageNegotiation
 *
 * For a working example, see
 * \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser.
 *
 * @see \Drupal\language\LanguageNegotiator
 * @see \Drupal\language\LanguageNegotiationMethodManager
 * @see \Drupal\language\LanguageNegotiationMethodInterface
 * @see hook_language_negotiation_info_alter()
 * @see plugin_api
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class LanguageNegotiation extends Plugin {

  /**
   * Constructs an LanguageNegotiation attribute.
   *
   * @param string $id
   *   The language negotiation plugin ID.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $name
   *   The human-readable name of the language negotiation plugin.
   * @param string[]|null $types
   *   An array of language types, such as the
   *    \Drupal\Core\Language\LanguageInterface::TYPE_* constants.
   *   If a language negotiation plugin does not specify which language types it
   *   should be used with, it will be available for all the configurable
   *   language types.
   * @param int $weight
   *   The default weight of the language negotiation plugin.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $description
   *   The description of the language negotiation plugin.
   * @param string|null $config_route_name
   *   (optional) The route pointing to the plugin's configuration page.
   */
  public function __construct(
      public readonly string $id,
      public readonly TranslatableMarkup $name,
      public readonly ?array $types = NULL,
      public readonly int $weight = 0,
      public readonly ?TranslatableMarkup $description = NULL,
      public readonly ?string $config_route_name = NULL,
  ) {}

}
+2 −1
Changes for core/modules/language/src/LanguageNegotiationMethodManager.php: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\language\Attribute\LanguageNegotiation;

/**
 * Manages language negotiation methods.
@@ -23,7 +24,7 @@ class LanguageNegotiationMethodManager extends DefaultPluginManager {
   *   An object that implements ModuleHandlerInterface
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/LanguageNegotiation', $namespaces, $module_handler, 'Drupal\language\LanguageNegotiationMethodInterface', 'Drupal\language\Annotation\LanguageNegotiation');
    parent::__construct('Plugin/LanguageNegotiation', $namespaces, $module_handler, 'Drupal\language\LanguageNegotiationMethodInterface', LanguageNegotiation::class, 'Drupal\language\Annotation\LanguageNegotiation');
    $this->cacheBackend = $cache_backend;
    $this->setCacheBackend($cache_backend, 'language_negotiation_plugins');
    $this->alterInfo('language_negotiation_info');
+9 −8
Changes for core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php: 9 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -4,21 +4,22 @@

use Drupal\Component\Utility\UserAgent;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\language\Attribute\LanguageNegotiation;
use Drupal\language\LanguageNegotiationMethodBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class for identifying language from the browser Accept-language HTTP header.
 *
 * @LanguageNegotiation(
 *   id = \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser::METHOD_ID,
 *   weight = -2,
 *   name = @Translation("Browser"),
 *   description = @Translation("Language from the browser's language settings."),
 *   config_route_name = "language.negotiation_browser"
 * )
 */
#[LanguageNegotiation(
  id: LanguageNegotiationBrowser::METHOD_ID,
  name: new TranslatableMarkup('Browser'),
  weight: -2,
  description: new TranslatableMarkup("Language from the browser's language settings."),
  config_route_name: 'language.negotiation_browser'
)]
class LanguageNegotiationBrowser extends LanguageNegotiationMethodBase implements ContainerFactoryPluginInterface {

  /**
+10 −8
Changes for core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationContentEntity.php: 10 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -4,10 +4,13 @@

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\language\Attribute\LanguageNegotiation;
use Drupal\language\LanguageNegotiationMethodBase;
use Drupal\language\LanguageSwitcherInterface;
use Drupal\Core\Routing\RouteObjectInterface;
@@ -17,15 +20,14 @@

/**
 * Class for identifying the content translation language.
 *
 * @LanguageNegotiation(
 *   id = Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationContentEntity::METHOD_ID,
 *   types = {Drupal\Core\Language\LanguageInterface::TYPE_CONTENT},
 *   weight = -9,
 *   name = @Translation("Content language"),
 *   description = @Translation("Determines the content language from the request parameter named 'language_content_entity'."),
 * )
 */
#[LanguageNegotiation(
  id: LanguageNegotiationContentEntity::METHOD_ID,
  name: new TranslatableMarkup('Content language'),
  types: [LanguageInterface::TYPE_CONTENT],
  weight: -9,
  description: new TranslatableMarkup("Determines the content language from the request parameter named 'language_content_entity'.")
)]
class LanguageNegotiationContentEntity extends LanguageNegotiationMethodBase implements OutboundPathProcessorInterface, LanguageSwitcherInterface, ContainerFactoryPluginInterface {

  /**
+9 −8
Changes for core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSelected.php: 9 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -2,20 +2,21 @@

namespace Drupal\language\Plugin\LanguageNegotiation;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\language\Attribute\LanguageNegotiation;
use Drupal\language\LanguageNegotiationMethodBase;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class for identifying language from a selected language.
 *
 * @LanguageNegotiation(
 *   id = Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected::METHOD_ID,
 *   weight = 12,
 *   name = @Translation("Selected language"),
 *   description = @Translation("Language based on a selected language."),
 *   config_route_name = "language.negotiation_selected"
 * )
 */
#[LanguageNegotiation(
  id: LanguageNegotiationSelected::METHOD_ID,
  name: new TranslatableMarkup('Selected language'),
  weight: 12,
  description: new TranslatableMarkup("Language based on a selected language."),
  config_route_name: 'language.negotiation_selected'
)]
class LanguageNegotiationSelected extends LanguageNegotiationMethodBase {

  /**
Loading