function LocaleHooks::formLanguageAdminOverviewFormAlter

Implements hook_form_FORM_ID_alter() for language_admin_overview_form().

Attributes

#[Hook('form_language_admin_overview_form_alter')]

File

core/modules/locale/src/Hook/LocaleHooks.php, line 273

Class

LocaleHooks
Hook implementations for locale.

Namespace

Drupal\locale\Hook

Code

public function formLanguageAdminOverviewFormAlter(&$form, FormStateInterface $form_state) : void {
  $languages = $form['languages']['#languages'];
  $total_strings = \Drupal::service('locale.storage')->countStrings();
  $stats = array_fill_keys(array_keys($languages), []);
  // If we have source strings, count translations and calculate progress.
  if (!empty($total_strings)) {
    $translations = \Drupal::service('locale.storage')->countTranslations();
    foreach ($translations as $langcode => $translated) {
      $stats[$langcode]['translated'] = $translated;
      if ($translated > 0) {
        $stats[$langcode]['ratio'] = round($translated / $total_strings * 100, 2);
      }
    }
  }
  array_splice($form['languages']['#header'], -1, 0, [
    'translation-interface' => $this->t('Interface translation'),
  ]);
  foreach ($languages as $langcode => $language) {
    $stats[$langcode] += [
      'translated' => 0,
      'ratio' => 0,
    ];
    if (!$language->isLocked() && locale_is_translatable($langcode)) {
      $form['languages'][$langcode]['locale_statistics'] = Link::fromTextAndUrl($this->t('@translated/@total (@ratio%)', [
        '@translated' => $stats[$langcode]['translated'],
        '@total' => $total_strings,
        '@ratio' => $stats[$langcode]['ratio'],
      ]), Url::fromRoute('locale.translate_page', [], [
        'query' => [
          'langcode' => $langcode,
        ],
      ]))
        ->toRenderable();
    }
    else {
      $form['languages'][$langcode]['locale_statistics'] = [
        '#markup' => $this->t('not applicable'),
      ];
    }
    // #type = link doesn't work with #weight on table.
    // reset and set it back after locale_statistics to get it at the right
    // end.
    $operations = $form['languages'][$langcode]['operations'];
    unset($form['languages'][$langcode]['operations']);
    $form['languages'][$langcode]['operations'] = $operations;
  }
}

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