eZ\Publish\Core\Helper\TranslationHelper::getTranslatedFieldDefinitionProperty PHP Method

getTranslatedFieldDefinitionProperty() public method

By default, this method will return the field definition name in current language if translation is present. If not, main language will be used. If $forcedLanguage is provided, will return the field definition name in this language, if translation is present.
public getTranslatedFieldDefinitionProperty ( eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType, string $fieldDefIdentifier, string $property = 'name', string $forcedLanguage = null ) : string | null
$contentType eZ\Publish\API\Repository\Values\ContentType\ContentType
$fieldDefIdentifier string Field Definition identifier
$property string Specifies if 'name' or 'description' should be used
$forcedLanguage string Locale we want the field definition name translated in in (e.g. "fre-FR"). Null by default (takes current locale)
return string | null
    public function getTranslatedFieldDefinitionProperty(ContentType $contentType, $fieldDefIdentifier, $property = 'name', $forcedLanguage = null)
    {
        $fieldDefinition = $contentType->getFieldDefinition($fieldDefIdentifier);
        if (!$fieldDefinition instanceof FieldDefinition) {
            throw new InvalidArgumentException('$fieldDefIdentifier', "Field '{$fieldDefIdentifier}' not found on {$contentType->identifier}");
        }
        $method = 'get' . $property;
        if (!method_exists($fieldDefinition, $method)) {
            throw new InvalidArgumentException('$property', "Method get'{$property}'() not found on FieldDefinition");
        }
        // Loop over prioritized languages to get the appropriate translated field definition name
        // Should ideally have used array_unique, but in that case the loop should ideally never reach last item
        foreach ($this->getLanguages($forcedLanguage, $contentType->mainLanguageCode) as $lang) {
            if ($name = $fieldDefinition->{$method}($lang)) {
                return $name;
            }
        }
    }

Usage Example

 /**
  * Gets name of a FieldDefinition description by loading ContentType based on Content/ContentInfo object.
  *
  * @param \eZ\Publish\API\Repository\Values\ValueObject $content Must be Content or ContentInfo object
  * @param string $fieldDefIdentifier Identifier for the field we want to get the name from
  * @param string $forcedLanguage Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale)
  *
  * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType When $content is not a valid Content object.
  *
  * @return string|null
  */
 public function getTranslatedFieldDefinitionDescription(ValueObject $content, $fieldDefIdentifier, $forcedLanguage = null)
 {
     if ($contentType = $this->getContentType($content)) {
         return $this->translationHelper->getTranslatedFieldDefinitionProperty($contentType, $fieldDefIdentifier, 'description', $forcedLanguage);
     }
     throw new InvalidArgumentType('$content', 'Content|ContentInfo', $content);
 }