是否有最佳做法来提取图像的uri在当前翻译当前网站访问的小枝?
我们有一个具有可译图像字段的对象。使用助手呈现图像:ez_render_field工作得很好。
但我现在也需要为当前的站点访问提取映像的uri,但无法找到这样做的方法。
尝试使用字段只会导致
{{ ez_field(content, "image_1").uri }}在模板的呈现过程中引发了一个异常(“可捕获的致命错误:无法将类eZ\Publish\API\Repository\Values\Content\Field的对象转换为字符串”)。
content对象如下所示:

发布于 2017-08-10 20:54:26
以下是实现这一目标的标准方法。
其中image是字段的名称,teaser是您定义的图像变量。默认情况下,您有original、small、large和..。
{% set imgAlias = ez_image_alias( content.getField( "image" ), content.versionInfo, 'teaser' ) %}
{{ dump(imgAlias.url) }}( imgAlias.url是你要找的东西。)
以下是指向文档的链接:别名
发布于 2017-08-10 11:26:52
我不知道这是不是最好的方法,但这是我想出来的:
{{ getImageUri( content.fields.image, ezpublish.siteaccess ) }}自定义枝函数
use Symfony\Component\Yaml\Yaml;
/**
* Class AppExtension
*
* @package AppBundle\Twig
*/
class AppExtension extends \Twig_Extension
{
public function getFilters()
{
return [
];
}
public function getFunctions()
{
return [
new \Twig_SimpleFunction('getYml', [$this, 'getYml']),
new \Twig_SimpleFunction('getImageUri', [$this, 'getImageUri']),
];
}
/**
* Pass an image object and return the original image uri in the current site access
*
* @param $imageField
* @param $siteAccess
*
* @return mixed
*/
public function getImageUri( $imageField, $siteAccess ){
$languages = $this->getYml('ezplatform', 'ezpublish:system:'.$siteAccess->name.':languages');
if($languages){
foreach($languages as $language){
if( array_key_exists( $language, $imageField ) ){
return $imageField[$language]->uri;
}
}
}
return $imageField[array_keys($imageField)[0]]->uri;
}
/**
* Internal cache of the yml files already fetched
*
* @var array
*/
private $yamlCache = [];
/**
* Return content from a app/config/*.yml file. Pass in a : separated path to fetch what you need. Empty returns the whole yml as an array
*
* @param $fileMinusExt
* @param bool $path
* @param string $delimiter
*
* @return bool|mixed
*/
public function getYml($fileMinusExt, $path = false, $delimiter = ':')
{
if (in_array($fileMinusExt, $this->yamlCache)) {
$value = $this->yamlCache[$fileMinusExt];
} else {
$value = Yaml::parse(file_get_contents('../app/config/' . $fileMinusExt . '.yml'));
}
if ($path === false) {
return $value;
}
return $this->_getYmlPath($value, explode($delimiter, $path));
}
/**
* Extract value from array
*
* @param $values
* @param $parts
*
* @return bool
*/
private function _getYmlPath($values, $parts)
{
try {
$subVal = $values[array_shift($parts)];
if (count($parts) > 0) {
return $this->_getYmlPath($subVal, $parts);
}
return $subVal;
} catch (\Exception $e) {
return false;
}
}
}https://stackoverflow.com/questions/45610204
复制相似问题