我正嘗試使用我在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