首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Drupal 8中的"srcset“更改为"data-srcset”。

将Drupal 8中的"srcset“更改为"data-srcset”。
EN

Stack Overflow用户
提问于 2017-09-19 23:02:55
回答 3查看 541关注 0票数 0

我想将Drupal 8响应图像中的属性"srcset“更改为"data-srcset”,将"srt“更改为"data-srt”,而且我在几个小时内没有找到解决方案。

有什么想法吗?

谢谢。

-) AK

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-09-29 18:29:24

  1. responsive-image.html.twig文件复制到主题
  2. 然后在without上使用{{ source_attributes }}小枝过滤器。文档
  3. 添加data-srcset="{{ source_attributes.srcset }}以在所选属性中呈现srcset,在本例中为data-srcset
  4. 您的修改必须类似于这个<source{{ source_attributes|without('srcset') }} data-srcset="{{ source_attributes.srcset }}"/>
票数 0
EN

Stack Overflow用户

发布于 2018-06-29 14:59:14

我在THEME.theme中得到了这个解决方案

对于正常图像:

代码语言:javascript
复制
  function THEME_preprocess_image(&$variables) {
    $variables['attributes']['data-src'] = $variables['attributes']['src'];
    $variables['attributes']['src'] = '';
  }

对于有反应的图像:

代码语言:javascript
复制
  function THEME_preprocess_responsive_image(&$variables) {
    $variables['img_element']['#attributes']['data-srcset'] = $variables['img_element']['#attributes']['srcset'];
    $variables['img_element']['#attributes']['srcset'] = '';
  }
票数 0
EN

Stack Overflow用户

发布于 2021-01-14 18:09:05

THEME.theme中的预处理功能是正确的发展方向。下面是到目前为止发布的代码的改进:

代码语言:javascript
复制
/**
 * Implements template_preprocess_responsive_image().
 * @see core/themes/stable/templates/field/responsive-image.html.twig
 */

function THEME_preprocess_responsive_image(&$variables) {
  // Loop through the sources in case there's more than one source for this image
  foreach ($variables['sources'] as $source) {
    // Get the original srcset for each source
    $original_srcset = $source->offsetGet('srcset')->value();
    // Assign it to a new attribute called 'data-srcset'
    $source->offsetSet('data-srcset', $original_srcset);
    // Unset the original srcset
    $source->offsetSet('srcset', NULL);
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46310909

复制
相关文章

相似问题

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