Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbachmann/71ad2b6f8e710b7c2b57 to your computer and use it in GitHub Desktop.
Save dbachmann/71ad2b6f8e710b7c2b57 to your computer and use it in GitHub Desktop.
Use translated labels for select-fields in TYPO3
<?php
namespace Vendor\ExtensionName\ViewHelpers;
/* *
* This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
/**
* This view helper generates a <select> dropdown list for the use with a form.
*
* = Basic usage =
*
* The most straightforward way is to supply an associative array as the "options" parameter.
* The array key is used as option key, and the value is used as human-readable name.
*
* <code title="Basic usage">
* <f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" />
* </code>
*
* = Pre-select a value =
*
* To pre-select a value, set "value" to the option key which should be selected.
* <code title="Default value">
* <f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" value="visa" />
* </code>
* Generates a dropdown box like above, except that "VISA Card" is selected.
*
* If the select box is a multi-select box (multiple="true"), then "value" can be an array as well.
*
* = Usage on domain objects =
*
* If you want to output domain objects, you can just pass them as array into the "options" parameter.
* To define what domain object value should be used as option key, use the "optionValueField" variable. Same goes for optionLabelField.
* If neither is given, the Identifier (UID/uid) and the __toString() method are tried as fallbacks.
*
* If the optionValueField variable is set, the getter named after that value is used to retrieve the option key.
* If the optionLabelField variable is set, the getter named after that value is used to retrieve the option value.
*
* If the prependOptionLabel variable is set, an option item is added in first position, bearing an empty string or -
* If provided, the value of the prependOptionValue variable as value.
*
* <code title="Domain objects">
* <ns:select name="users" options="{userArray}" optionValueField="id" optionLabelField="firstName" l10n="true" />
* </code>
* In the above example, the userArray is an array of "User" domain objects, with no array key specified.
*
* So, in the above example, the method $user->getId() is called to retrieve the key, and $user->getFirstName() to retrieve the displayed value of each entry.
*
* The "value" property now expects a domain object, and tests for object equivalence.
*
* @api
*/
class SelectViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper {
/**
* Initialize arguments.
*
* @return void
* @api
*/
public function initializeArguments() {
parent::initializeArguments();
$this->registerArgument('l10n', 'string', 'If specified, will call the correct label specified in locallang file.');
}
/**
* Translate a given key or use the tag body as default.
*
* @param string $id The locallang id
* @return string The translated key or tag body if key doesn't exist
*/
protected function renderTranslation($id) {
$request = $this->controllerContext->getRequest();
$extensionName = $this->arguments['extensionName'] === NULL ? $request->getControllerExtensionName() : $this->arguments['extensionName'];
$value = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($id, $extensionName, $this->arguments['arguments']);
if ($value === NULL) {
$value = $this->arguments['default'] !== NULL ? $this->arguments['default'] : $this->renderChildren();
if (is_array($this->arguments['arguments'])) {
$value = vsprintf($value, $this->arguments['arguments']);
}
} elseif ($this->arguments['htmlEscape']) {
$value = htmlspecialchars($value);
}
return $value;
}
/**
* Render the option tags.
*
* @param array $options the options for the form.
* @return string rendered tags.
*/
protected function renderOptionTags($options) {
$output = '';
if ($this->hasArgument('prependOptionLabel')) {
$value = $this->hasArgument('prependOptionValue') ? $this->arguments['prependOptionValue'] : '';
$label = $this->arguments['prependOptionLabel'];
$output .= $this->renderOptionTag($value, $label, FALSE) . chr(10);
}
foreach ($options as $value => $label) {
$isSelected = $this->isSelected($value);
if ($this->hasArgument('l10n')) {
$l10n = $this->arguments['l10n'];
$label = $this->renderTranslation($l10n.'.'.$value);
}
$output .= $this->renderOptionTag($value, $label, $isSelected) . chr(10);
}
return $output;
}
}
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<xliff version="1.0">
<file source-language="en" datatype="plaintext" original="messages" date="2016-02-08T07:11:19Z" product-name="extension_name">
<header/>
<body>
<trans-unit id="tx_extensionname_domain_model_model.propertyName">
<source>Property name</source>
</trans-unit>
<!-- 123, abc, xyz, 987 are the values from the database which will be translated -->
<trans-unit id="tx_extensionname_domain_model_model.propertyName.123">
<source>Property label 123</source>
</trans-unit>
<trans-unit id="tx_extensionname_domain_model_model.propertyName.abc">
<source>Property label abc</source>
</trans-unit>
<trans-unit id="tx_extensionname_domain_model_model.propertyName.xyz">
<source>Property label xyz</source>
</trans-unit>
<trans-unit id="tx_extensionname_domain_model_model.propertyName.987">
<source>Property label 987</source>
</trans-unit>
</body>
</file>
</xliff>
{namespace ns=Vendor\ExtensionName\ViewHelpers}
<ns:select
name="propertyName"
property="propertyName"
options="{optionsArray}"
optionValueField="propertyName"
optionLabelField="propertyName"
l10n="tx_extensionname_domain_model_model.propertyName"
sortByOptionLabel="true"
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment