0

在树枝中当前站点访问的当前翻译中提取图像的 uri 是否有最佳实践?

我们有一个具有可翻译图像场的对象。使用助手渲染图像:ez_render_field工作正常。

但我现在还需要为当前站点访问提取图像的 uri,但找不到这样做的方法。

尝试使用ez_field只会导致

{{ ez_field(content, "image_1").uri }}

在渲染模板期间引发了异常(“可捕获的致命错误:类 eZ\Publish\API\Repository\Values\Content\Field 的对象无法转换为字符串”)。

内容对象如下所示: 在此处输入图像描述

4

2 回答 2

2

这是实现这一目标的标准方法。

whereimage是字段的名称,并且teaser是您定义的图像变体。默认情况下,您有,original和....smalllarge

{% set imgAlias = ez_image_alias( content.getField( "image" ), content.versionInfo, 'teaser' ) %}
{{ dump(imgAlias.url) }}

imgAlias.url就是你要找的。)

这是文档的链接:https ://doc.ez.no/display/DEVELOPER/ez_image_alias

于 2017-08-10T20:54:26.290 回答
0

我不知道这是否是最好的方法,但这就是我想出的:

{{ 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;
        }
    }
}
于 2017-08-10T11:26:52.277 回答