ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
UserLanguageDefinition.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
31 use Closure;
32 
34 {
39  public function __construct(
40  private readonly UI $ui,
41  private readonly array $installed_languages,
42  private readonly Closure $required
43  ) {
44  }
45 
46  public function formGroup(array $arguments = []): Group
47  {
48  $languages = array_combine($this->installed_languages, array_map($this->translatedLanguage(...), $this->installed_languages));
49  $default = key($languages);
50 
51  return $this->ui->create()->input()->field()->group([
52  'lng' => $this->radio(
53  $this->ui->txt('language'),
54  $languages,
55  $arguments['lng'] ?? $default
56  )->withRequired(true, ($this->required)($languages))
57  ], $this->ui->txt('crit_type_usr_language'), $this->ui->txt('crit_type_usr_language_info'));
58  }
59 
60  public function withCriterion(CriterionContent $criterion): Condition
61  {
62  return new UserLanguage($criterion, $this, $this->ui);
63  }
64 
65  public function translatedType(): string
66  {
67  return $this->ui->txt('crit_type_usr_language');
68  }
69 
70  public function translatedLanguage(string $language): string
71  {
72  return $this->ui->txt('meta_l_' . $language);
73  }
74 
79  private function radio(string $lang_key, array $options, $value): Radio
80  {
81  $field = $this->ui->create()->input()->field()->radio($lang_key);
82  foreach ($options as $key => $label) {
83  $field = $field->withOption((string) $key, $label);
84  }
85 
86  return $value !== null && isset($options[(string) $value]) ? $field->withValue((string) $value) : $field;
87  }
88 }
This is what a radio-input looks like.
Definition: Radio.php:28
__construct(private readonly UI $ui, private readonly array $installed_languages, private readonly Closure $required)
string $key
Consumer key/client ID value.
Definition: System.php:193