首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ezplatform为当前站点访问的当前语言提取图像uri

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

Stack Overflow用户
提问于 2017-08-10 09:48:13
回答 2查看 780关注 0票数 0

是否有最佳做法来提取图像的uri在当前翻译当前网站访问的小枝?

我们有一个具有可译图像字段的对象。使用助手呈现图像:ez_render_field工作得很好。

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

尝试使用字段只会导致

代码语言:javascript
复制
{{ ez_field(content, "image_1").uri }}

在模板的呈现过程中引发了一个异常(“可捕获的致命错误:无法将类eZ\Publish\API\Repository\Values\Content\Field的对象转换为字符串”)。

content对象如下所示:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-10 20:54:26

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

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

代码语言:javascript
复制
{% set imgAlias = ez_image_alias( content.getField( "image" ), content.versionInfo, 'teaser' ) %}
{{ dump(imgAlias.url) }}

( imgAlias.url是你要找的东西。)

以下是指向文档的链接:别名

票数 2
EN

Stack Overflow用户

发布于 2017-08-10 11:26:52

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

代码语言:javascript
复制
{{ getImageUri( content.fields.image, ezpublish.siteaccess ) }}

自定义枝函数

代码语言:javascript
复制
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;
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45610204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档