我正尝试使用我在drupal 8主题中创建的图像样式来打印图像。如何在Drupal 8树枝模板中使用图像样式?
我可以做一个模板打印图像{{content.banner}}但我怎么应用映像风格的形象?我试图寻找文件,但似乎没有任何出现。
我正尝试使用我在drupal 8主题中创建的图像样式来打印图像。如何在Drupal 8树枝模板中使用图像样式?
我可以做一个模板打印图像{{content.banner}}但我怎么应用映像风格的形象?我试图寻找文件,但似乎没有任何出现。
目前的Drupal 8没有对应用的影像风格特殊的过滤器。相反,你可以为你的形象的新属性是这样的:
{% set image = image|merge({'#image_style': 'thumbnail'}) %}
然后简单地输出更新后的图像:
{{ image }}
P.S。您可以通过{{ dump(image) }}
来查看可以更新的属性
我通过创建自己的解决方案树枝筛选器。
您可以通过创建自己的模块暴露这个筛选这样做。
随时重新使用它。
代码
namespace Drupal\twig_extender\TwigExtension;
use Drupal\node\Entity\Node;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
class Images extends \Twig_Extension {
/**
* Generates a list of all Twig functions that this extension defines.
*/
public function getFunctions(){
return array(
new \Twig_SimpleFunction('image_style', array($this, 'imageStyle'), array('is_safe' => array('html'))),
);
}
/**
* Gets a unique identifier for this Twig extension.
*/
public function getName() {
return 'twig_extender.twig.images';
}
/**
* Generate images styles for given image
*/
public static function imageStyle($file_id, $styles) {
$file = File::load($file_id);
$transform = array();
if (isset($file->uri->value)) {
$transform['source'] = $file->url();
foreach ($styles as $style) {
$transform[$style] = ImageStyle::load($style)->buildUrl($file->uri->value);
}
}
return $transform;
}
}
使用
{% set transform = image_style(image.entity.fid.value, ['thumbnail', 'large']) %}
然后,你必须访问源图像&风格
{{ transform.source }}
{{ transform.thumbnail }}
{{ transform.large }}
希望这将有助于哟你的家伙!
我试图上述方法。我把上面的代码放在'/ modules/custom_module/src/TwigExtension/Images.php'中,但仍然出现错误'Twig_Error_Syntax:Unknown“image_style”函数。在Twig_ExpressionParser - > ..' – ARUN
从现在开始,我使用Bamboo Twig(Drupal 8模块)。在Bamboo Twig Loader扩展中,它展示了两个分支函数来渲染图像。这里的竹枝文件:https://www.drupal.org/docs/8/modules/bamboo-twig/usage。这些函数是'bamboo_render_image'&'bamboo_render_image_style'。 –
我也确实在内容类型设置中,可以显示全部图像进行该内容类型是图片样式X.,所以我可以从我的答案还是你做到这一点,但你更好海事组织。 – Kato
我应该为“图像”使用什么变量? content.field_bloc_1_image.entity不起作用,例如:s –
我无法让这个工作。它不使用图像样式?我有{%组图像= content.field_global_image |合并({ '#image_style': '缩略图'})%} {{图像}} – paulcap1