Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| NameComponentMetadataService | |
100.00% |
28 / 28 |
|
100.00% |
4 / 4 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTranslations | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
| getFormatterOutputTypes | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| getFormatterOutputOptions | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\name\Service; |
| 6 | |
| 7 | use Drupal\Core\StringTranslation\StringTranslationTrait; |
| 8 | use Drupal\Core\StringTranslation\TranslationInterface; |
| 9 | |
| 10 | /** |
| 11 | * Translated labels and formatter output options for name components. |
| 12 | * |
| 13 | * @internal |
| 14 | */ |
| 15 | class NameComponentMetadataService implements NameComponentMetadataInterface { |
| 16 | |
| 17 | use StringTranslationTrait; |
| 18 | |
| 19 | /** |
| 20 | * Cached component labels. |
| 21 | * |
| 22 | * @var array<string, \Drupal\Core\StringTranslation\TranslatableMarkup|string>|null |
| 23 | */ |
| 24 | private ?array $translations = NULL; |
| 25 | |
| 26 | /** |
| 27 | * Cached formatter output type labels. |
| 28 | * |
| 29 | * @var array<string, \Drupal\Core\StringTranslation\TranslatableMarkup|string>|null |
| 30 | */ |
| 31 | private ?array $formatterOutputTypes = NULL; |
| 32 | |
| 33 | /** |
| 34 | * Cached formatter output option labels. |
| 35 | * |
| 36 | * @var array<string, \Drupal\Core\StringTranslation\TranslatableMarkup|string>|null |
| 37 | */ |
| 38 | private ?array $formatterOptions = NULL; |
| 39 | |
| 40 | public function __construct(TranslationInterface $string_translation) { |
| 41 | $this->setStringTranslation($string_translation); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns translated labels for core name components. |
| 46 | * |
| 47 | * @param string[]|null $intersect |
| 48 | * Keys to include; empty or NULL returns all components. |
| 49 | * |
| 50 | * @return array<string, \Drupal\Core\StringTranslation\TranslatableMarkup|string> |
| 51 | * Keyed component labels. |
| 52 | */ |
| 53 | public function getTranslations(?array $intersect = NULL): array { |
| 54 | if ($this->translations === NULL) { |
| 55 | $this->translations = [ |
| 56 | 'title' => $this->t('@name_title', ['@name_title' => $this->t('Title')]), |
| 57 | 'given' => $this->t('@name_given', ['@name_given' => $this->t('Given')]), |
| 58 | 'middle' => $this->t('@name_middle', ['@name_middle' => $this->t('Middle name(s)')]), |
| 59 | 'family' => $this->t('@name_family', ['@name_family' => $this->t('Family')]), |
| 60 | 'generational' => $this->t('@name_generational', ['@name_generational' => $this->t('Generational')]), |
| 61 | 'credentials' => $this->t('@name_credentials', ['@name_credentials' => $this->t('Credentials')]), |
| 62 | ]; |
| 63 | } |
| 64 | $use_all_translations = ($intersect === NULL || $intersect === []); |
| 65 | if ($use_all_translations) { |
| 66 | return $this->translations; |
| 67 | } |
| 68 | return array_intersect_key($this->translations, $intersect); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Labels for formatter output type identifiers. |
| 73 | * |
| 74 | * @return array<string, \Drupal\Core\StringTranslation\TranslatableMarkup|string> |
| 75 | * Keys are output type machine names; values are labels. |
| 76 | */ |
| 77 | public function getFormatterOutputTypes(): array { |
| 78 | if ($this->formatterOutputTypes === NULL) { |
| 79 | $this->formatterOutputTypes = [ |
| 80 | 'default' => $this->t('Default'), |
| 81 | 'plain' => $this->t('Plain'), |
| 82 | 'raw' => $this->t('Raw'), |
| 83 | ]; |
| 84 | } |
| 85 | return $this->formatterOutputTypes; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Select-option labels for formatter output settings. |
| 90 | * |
| 91 | * @return array<string, \Drupal\Core\StringTranslation\TranslatableMarkup|string> |
| 92 | * Options suitable for #options on form elements. |
| 93 | */ |
| 94 | public function getFormatterOutputOptions(): array { |
| 95 | if ($this->formatterOptions === NULL) { |
| 96 | $this->formatterOptions = [ |
| 97 | 'default' => $this->t('Default'), |
| 98 | 'plain' => $this->t('Plain text'), |
| 99 | 'raw' => $this->t('Raw value (not recommended)'), |
| 100 | ]; |
| 101 | } |
| 102 | return $this->formatterOptions; |
| 103 | } |
| 104 | |
| 105 | } |