ezplatform 为当前站点访问的当前语言提取图像的 uri

ezplatform extracting the uri of an image for the current language of the current site access

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

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

但我现在还需要为当前站点访问提取图像的 uri,但找不到执行此操作的方法。

尝试使用 ez_field 只会导致

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

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class eZ\Publish\API\Repository\Values\Content\Field could not be converted to string").

内容对象如下所示:

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

{{ 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;
        }
    }
}

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

其中 image 是字段名称,teaser 是您定义的图像变量。默认情况下你有 originalsmalllarge 和 ....

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

( imgAlias.url 就是你要找的。)

这里是 link 文档:https://doc.ez.no/display/DEVELOPER/ez_image_alias