简体   繁体   中英

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

Is there a best practice to extract the uri of an image in the current translation of the current site access in twig?

We have an object with a translatable image field. Rendering the image with the helper: ez_render_field works fine.

But I now need to also extract the uri of the image for the current siteaccess but cannot find a way of doing this.

Trying to use the ez_field just results in

{{ 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").

The content object looks like: 在此处输入图片说明

here is the standard way to achieve this.

where image is the field's name and teaser is the image variante you defined. by default you have original , small , large and ....

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

( imgAlias.url is what you are looking for. )

here is the link to Documentation: https://doc.ez.no/display/DEVELOPER/ez_image_alias

I don't know if this is the best method, but this is what i came up with:

{{ getImageUri( content.fields.image, ezpublish.siteaccess ) }}

The custom twig functions

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM