‪TYPO3CMS  ‪main
CountrySelectViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
25 
83 {
87  protected ‪$tagName = 'select';
88 
89  public function ‪initializeArguments(): void
90  {
91  parent::initializeArguments();
92  $this->registerUniversalTagAttributes();
93  $this->registerTagAttribute('size', 'string', 'Size of select field, a numeric value to show the amount of items to be visible at the same time - equivalent to HTML <select> site attribute');
94  $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
95  $this->registerArgument('excludeCountries', 'array', 'Array with country codes that should not be shown.', false, []);
96  $this->registerArgument('onlyCountries', 'array', 'If set, only the country codes in the list are rendered.', false, []);
97  $this->registerArgument('optionLabelField', 'string', 'If specified, will call the appropriate getter on each object to determine the label. Use "name", "localizedName", "officialName" or "localizedOfficialName"', false, 'localizedName');
98  $this->registerArgument('sortByOptionLabel', 'boolean', 'If true, List will be sorted by label.', false, false);
99  $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this ViewHelper', false, 'f3-form-error');
100  $this->registerArgument('prependOptionLabel', 'string', 'If specified, will provide an option at first position with the specified label.');
101  $this->registerArgument('prependOptionValue', 'string', 'If specified, will provide an option at first position with the specified value.');
102  $this->registerArgument('multiple', 'boolean', 'If set multiple options may be selected.', false, false);
103  $this->registerArgument('required', 'boolean', 'If set no empty value is allowed.', false, false);
104  $this->registerArgument('prioritizedCountries', 'array', 'A list of country codes which should be listed on top of the list.', false, []);
105  $this->registerArgument('alternativeLanguage', 'string', 'If specified, the country list will be shown in the given language.');
106  }
107 
108  public function ‪render(): string
109  {
110  if ($this->arguments['required']) {
111  $this->tag->addAttribute('required', 'required');
112  }
113  $name = $this->‪getName();
114  if ($this->arguments['multiple']) {
115  $this->tag->addAttribute('multiple', 'multiple');
116  $name .= '[]';
117  }
119  $this->‪setErrorClassAttribute();
121  $this->‪setRespectSubmittedDataValue(true);
122 
123  $this->tag->addAttribute('name', $name);
124 
125  $validCountries = $this->getCountryList();
126  ‪$options = $this->createOptions($validCountries);
127  $selectedValue = $this->‪getValueAttribute();
128 
129  $tagContent = $this->renderPrependOptionTag();
130  foreach (‪$options as $value => $label) {
131  $tagContent .= $this->renderOptionTag($value, $label, $value === $selectedValue);
132  }
133 
134  $this->tag->forceClosingTag(true);
135  $this->tag->setContent($tagContent);
136  return $this->tag->render();
137  }
138 
143  protected function createOptions(array ‪$countries): array
144  {
145  ‪$options = [];
146  foreach (‪$countries as $code => $country) {
147  switch ($this->arguments['optionLabelField']) {
148  case 'localizedName':
149  ‪$options[$code] = $this->‪translate($country->getLocalizedNameLabel());
150  break;
151  case 'name':
152  ‪$options[$code] = $country->getName();
153  break;
154  case 'officialName':
155  ‪$options[$code] = $country->getOfficialName();
156  break;
157  case 'localizedOfficialName':
158  $name = $this->‪translate($country->getLocalizedOfficialNameLabel());
159  if (!$name) {
160  $name = $this->‪translate($country->getLocalizedNameLabel());
161  }
162  ‪$options[$code] = $name;
163  break;
164  default:
165  throw new \TYPO3Fluid\Fluid\Core\ViewHelper\Exception('Argument "optionLabelField" of <f:form.countrySelect> must either be set to "localizedName", "name", "officialName", or "localizedOfficialName".', 1674076708);
166  }
167  }
168  if ($this->arguments['sortByOptionLabel']) {
169  asort(‪$options, SORT_LOCALE_STRING);
170  } else {
171  ksort(‪$options, SORT_NATURAL);
172  }
173  if (($this->arguments['prioritizedCountries'] ?? []) !== []) {
174  $finalOptions = [];
175  foreach ($this->arguments['prioritizedCountries'] as $countryCode) {
176  if (isset(‪$options[$countryCode])) {
177  $label = ‪$options[$countryCode];
178  $finalOptions[$countryCode] = $label;
179  unset(‪$options[$countryCode]);
180  }
181  }
182  foreach (‪$options as $countryCode => $label) {
183  $finalOptions[$countryCode] = $label;
184  }
185  ‪$options = $finalOptions;
186  }
187  return ‪$options;
188  }
189 
190  protected function ‪translate(string $label): string
191  {
192  if ($this->arguments['alternativeLanguage']) {
193  return (string)‪LocalizationUtility::translate($label, languageKey: $this->arguments['alternativeLanguage']);
194  }
195  return (string)‪LocalizationUtility::translate($label);
196  }
197 
201  protected function renderPrependOptionTag(): string
202  {
203  if ($this->hasArgument('prependOptionLabel')) {
204  $value = $this->hasArgument('prependOptionValue') ? $this->arguments['prependOptionValue'] : '';
205  $label = $this->arguments['prependOptionLabel'];
206  return $this->renderOptionTag((string)$value, (string)$label, false) . LF;
207  }
208  return '';
209  }
210 
219  protected function renderOptionTag(string $value, string $label, bool $isSelected): string
220  {
221  ‪$output = '<option value="' . htmlspecialchars($value) . '"';
222  if ($isSelected) {
223  ‪$output .= ' selected="selected"';
224  }
225  ‪$output .= '>' . htmlspecialchars($label) . '</option>';
226  return ‪$output;
227  }
228 
232  protected function getCountryList(): array
233  {
234  $filter = new CountryFilter();
235  $filter
236  ->setOnlyCountries($this->arguments['onlyCountries'] ?? [])
237  ->setExcludeCountries($this->arguments['excludeCountries'] ?? []);
238 
239  return GeneralUtility::makeInstance(CountryProvider::class)->getFiltered($filter);
240  }
241 }
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\setErrorClassAttribute
‪setErrorClassAttribute()
Definition: AbstractFormFieldViewHelper.php:331
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CountrySelectViewHelper\render
‪render()
Definition: CountrySelectViewHelper.php:107
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CountrySelectViewHelper\translate
‪array< string, createOptions(array $countries):array { $options=[];foreach( $countries as $code=> $country) { switch( $this->arguments[ 'optionLabelField']) { case 'localizedName':$options[ $code]=$this-> translate($country->getLocalizedNameLabel())
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:35
‪TYPO3\CMS\Fluid\ViewHelpers\Form
Definition: AbstractFormFieldViewHelper.php:18
‪$countries
‪$countries
Definition: updateIsoDatabase.php:172
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CountrySelectViewHelper\initializeArguments
‪initializeArguments()
Definition: CountrySelectViewHelper.php:88
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CountrySelectViewHelper\$options
‪if(! $name) $options[$code]
Definition: CountrySelectViewHelper.php:161
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\registerFieldNameForFormTokenGeneration
‪registerFieldNameForFormTokenGeneration(string $fieldName)
Definition: AbstractFormViewHelper.php:100
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\setRespectSubmittedDataValue
‪setRespectSubmittedDataValue(bool $respectSubmittedDataValue)
Definition: AbstractFormFieldViewHelper.php:69
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getName
‪getName()
Definition: AbstractFormFieldViewHelper.php:79
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, Locale|string $languageKey=null)
Definition: LocalizationUtility.php:47
‪TYPO3\CMS\Core\Country\CountryFilter
Definition: CountryFilter.php:24
‪$output
‪$output
Definition: annotationChecker.php:114
‪TYPO3\CMS\Core\Country\Country
Definition: Country.php:25
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
Definition: AbstractFormFieldViewHelper.php:37
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CountrySelectViewHelper
Definition: CountrySelectViewHelper.php:83
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\addAdditionalIdentityPropertiesIfNeeded
‪addAdditionalIdentityPropertiesIfNeeded()
Definition: AbstractFormFieldViewHelper.php:241
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Country\CountryProvider
Definition: CountryProvider.php:26
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getValueAttribute
‪mixed getValueAttribute()
Definition: AbstractFormFieldViewHelper.php:147
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CountrySelectViewHelper\$tagName
‪string $tagName
Definition: CountrySelectViewHelper.php:86