class LocaleThemeHooks

Hook implementations for locale.

Hierarchy

Expanded class hierarchy of LocaleThemeHooks

1 file declares its use of LocaleThemeHooks
locale.module in core/modules/locale/locale.module

File

core/modules/locale/src/Hook/LocaleThemeHooks.php, line 17

Namespace

Drupal\locale\Hook
View source
class LocaleThemeHooks {
  use StringTranslationTrait;
  public function __construct(protected readonly LanguageManagerInterface $languageManager, protected readonly DateFormatterInterface $dateFormatter, protected readonly RedirectDestinationInterface $redirectDestination) {
  }
  
  /**
   * Implements hook_theme().
   */
  public function theme() : array {
    return [
      'locale_translation_last_check' => [
        'variables' => [
          'last' => NULL,
        ],
        'initial preprocess' => static::class . ':preprocessLocaleTranslationLastCheck',
      ],
      'locale_translation_update_info' => [
        'variables' => [
          'updates' => [],
          'not_found' => [],
        ],
        'initial preprocess' => static::class . ':preprocessLocaleTranslationUpdateInfo',
      ],
    ];
  }
  
  /**
   * Prepares variables for translation status information templates.
   *
   * Translation status information is displayed per language.
   *
   * Default template: locale-translate-edit-form-strings.html.twig.
   *
   * @param array $variables
   *   An associative array containing:
   *   - updates: The projects which have updates.
   *   - not_found: The projects which updates are not found.
   *
   * @see \Drupal\locale\Form\TranslationStatusForm
   */
  public function preprocessLocaleTranslationUpdateInfo(array &$variables) : void {
    foreach ($variables['updates'] as $update) {
      $variables['modules'][] = $update['name'];
    }
  }
  
  /**
   * Prepares variables for most recent translation update templates.
   *
   * Displays the last time we checked for locale update data. In addition to
   * properly formatting the given timestamp, this function also provides a
   * "Check manually" link that refreshes the available update and redirects
   * back to the same page.
   *
   * Default template: locale-translation-last-check.html.twig.
   *
   * @param array $variables
   *   An associative array containing:
   *   - last: The timestamp when the site last checked for available updates.
   *
   * @see \Drupal\locale\Form\TranslationStatusForm
   */
  public function preprocessLocaleTranslationLastCheck(array &$variables) : void {
    $last = $variables['last'];
    $variables['last_checked'] = $last != NULL;
    $variables['time'] = $variables['last_checked'] ? $this->dateFormatter
      ->formatTimeDiffSince($last) : NULL;
    $variables['link'] = Link::fromTextAndUrl($this->t('Check manually'), Url::fromRoute('locale.check_translation', [], [
      'query' => $this->redirectDestination
        ->getAsArray(),
    ]))
      ->toString();
  }
  
  /**
   * Implements hook_preprocess_HOOK() for node templates.
   */
  public function preprocessNode(&$variables) : void {
    /** @var \Drupal\node\NodeInterface $node */
    $node = $variables['node'];
    if ($node->language()
      ->getId() != LanguageInterface::LANGCODE_NOT_SPECIFIED) {
      $interface_language = $this->languageManager
        ->getCurrentLanguage();
      $node_language = $node->language();
      if ($node_language->getId() != $interface_language->getId()) {
        // If the node language was different from the page language, we should
        // add markup to identify the language. Otherwise the page language is
        // inherited.
        $variables['attributes']['lang'] = $node_language->getId();
        if ($node_language->getDirection() != $interface_language->getDirection()) {
          // If text direction is different form the page's text direction, add
          // direction information as well.
          $variables['attributes']['dir'] = $node_language->getDirection();
        }
      }
    }
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
LocaleThemeHooks::preprocessLocaleTranslationLastCheck public function Prepares variables for most recent translation update templates.
LocaleThemeHooks::preprocessLocaleTranslationUpdateInfo public function Prepares variables for translation status information templates.
LocaleThemeHooks::preprocessNode public function Implements hook_preprocess_HOOK() for node templates.
LocaleThemeHooks::theme public function Implements hook_theme().
LocaleThemeHooks::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
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. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.