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

getTranslatedByMethod() public method

For generic use, expects method exposing translated property as-is on value object, typically $object->$method($language) Languages will consist of either forced language or current languages list, in addition helper will append null to list of languages so method may fallback to main/initial language if supported by domain.
public getTranslatedByMethod ( eZ\Publish\API\Repository\Values\ValueObject $object, string $method, string $forcedLanguage = null ) : string | null
$object eZ\Publish\API\Repository\Values\ValueObject Can be any kind of Value object which directly holds the methods that provides translated value.
$method string Method name, example 'getName', 'description'
$forcedLanguage string Locale we want the content name translation in (e.g. "fre-FR"). Null by default (takes current locale)
return string | null
    public function getTranslatedByMethod(ValueObject $object, $method, $forcedLanguage = null)
    {
        if (!method_exists($object, $method)) {
            throw new InvalidArgumentException('$method', "Method '{$method}' not found on " . get_class($object));
        }
        foreach ($this->getLanguages($forcedLanguage) as $lang) {
            if ($value = $object->{$method}($lang)) {
                return $value;
            }
        }
    }

Usage Example

 /**
  * Returns tag keyword for provided tag ID or tag object.
  *
  * @param mixed|\Netgen\TagsBundle\API\Repository\Values\Tags\Tag $tag
  *
  * @return string
  */
 public function getTagKeyword($tag)
 {
     if (!$tag instanceof Tag) {
         try {
             $tag = $this->tagsService->loadTag($tag);
         } catch (NotFoundException $e) {
             return '';
         }
     }
     return $this->translationHelper->getTranslatedByMethod($tag, 'getKeyword');
 }
All Usage Examples Of eZ\Publish\Core\Helper\TranslationHelper::getTranslatedByMethod