Change "srcset" to "data-srcset" in Drupal 8 - drupal

I want to change the attribute "srcset" to "data-srcset" and "srt" to "data-srt" in Drupal 8 responsive images and i didn't find a solution in hours.
Any ideas?
Thanks.
:-) AK

Copy responsive-image.html.twig file to your theme
Then use the without twig filter over the {{ source_attributes }}. Documentation.
Add data-srcset="{{ source_attributes.srcset }} to render the srcset in the attribute you choose, data-srcset in this case
Your modifications must look like this <source{{ source_attributes|without('srcset') }} data-srcset="{{ source_attributes.srcset }}"/>

I ended up with this solution in THEME.theme
for normal images:
function THEME_preprocess_image(&$variables) {
$variables['attributes']['data-src'] = $variables['attributes']['src'];
$variables['attributes']['src'] = '';
}
and for responsive images:
function THEME_preprocess_responsive_image(&$variables) {
$variables['img_element']['#attributes']['data-srcset'] = $variables['img_element']['#attributes']['srcset'];
$variables['img_element']['#attributes']['srcset'] = '';
}

The preprocess function in THEME.theme is the right way forward. Here's a refinement of the code posted so far:
/**
* 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);
}
}

Related

Get CSS Parameter from Typescript Angular

Is there a way to Get a variable that is set in the global scss file from a ts file in Angular (8)
I'm looking to use some of the defined variables dynamically in a canvas element defined in the ts code.
I have a way to do this using a styles service based on
https://en.programqa.com/question/52907585/
Within Global.SCSS
#mixin ExportVariables($map, $prefix: null) {
$mapPrefix: "--#{$prefix}";
#if ($prefix){
$mapPrefix: "#{$mapPrefix}-";
}
body {
#each $name, $value in $map {
#{$mapPrefix}#{$name}: $value;
}
}
}
--idle-state: #29ABE2;
// Import each of these in the theme service
$stateSCSS:(
idle: var(--idle-state),
);
#include ExportVariables($stateSCSS, 'stateSCSS');
In the Service
const bodyStyles = window.getComputedStyle(document.body);
this.stateSCSS = {
idle: bodyStyles.getPropertyValue('--stateSCSS-idle'),
};
I think this answers your questions: access SASS values ($colors from variables.scss) in Typescript (Angular2 ionic2)
TLDR:
Unfortunately, there is no way to access SASS variable directly from typescript/javascript code. However, we can make a workaround to access those variables.
You can view the workaround in the post mentioned above

Modify HTML tag from dangerouslySetInnerHTML in React

I am building a Gatsby Blog using React/Gatsby & the Wordpress API.
I render an excerpt of the latest articles on the landing page like so:
<span
className="mb-0"
id="excerpt-wrapper"
dangerouslySetInnerHTML={{ __html: this.props.post.node.excerpt}
/>
The problem is, my this.props.post.node.excerpt comes with an unwanted wrapping <p> tag. This tag inherit from Bootstrap CSS as I am using Bootstrap 4 in my whole project, and from the user agent stylesheet.
Hence I need to find a way either to :
get rid of the wrapping p tag
modify the CSS once the excerpt is mounted
I tried the following solution:
componentDidMount() {
this.removePTagMargin();
}
removePTagMargin = () => {
const excerptWrapper = document.querySelector("#excerpt-wrapper");
excerptWrapper.firstChild.style.marginBottom = "0px !important"
excerptWrapper.firstChild.style.marginBlockEnd = "0px !important"
}
but it does not work (maybe because it executes before the WP API call is done ?).
How can I solve my problem ?
This is assuming the excerpt comes from gatsby-transformer-remark.
You can choose the format of your excerpt in your GraphQL query for the post, it looks like the format you're using is HTML, you want PLAIN:
https://www.gatsbyjs.org/packages/gatsby-transformer-remark/#format
Try modifying your query by putting the format parameter on the excerpt field:
{
allMarkdownRemark {
edges {
node {
excerpt(format: PLAIN)
}
}
}
}
Edit: Hacky way of removing the <p> tags due to the inefficiencies in this gatsby-source-wordpress plugin.
Add a helper called removeParagraphTags this will simply trim the first three chars from the string and the last 4 chars from the string.
removeParagraphTags (excerpt) {
return excerpt.substr(3, excerpt.length - 7)
}
Then you can use this helper when setting the excerpt HTML.
dangerouslySetInnerHTML={{
__html: this.removeParagraphTags(this.props.post.node.excerpt)
}}

SVG sprites in helper function

I use svg sprites like this:
<svg><use href="combined.svg#filter_sent"></use></svg>
In a Meteor/Blaze template helper, svg sprites are not rendered:
<div class="colorset">
{{{color}}}
</div>
color: function () {
if (this.color && !this.validate) {
return '<svg><use href="combined.svg#green"></use></svg>';
}
if (this.color && this.certain) {
return '<svg><use href="combined.svg#orange"></use></svg>';
} else {
return '<svg><use href="combined.svg#red"></use></svg>';
}
}
=> Inside the use > shadow-root, nothing is inserted.
There are a number of error possibilities, best if you post all your code.
One possibility: Not matching template name to helper function declaration. e.g.
In html file:
<template name="foo">
...
In js file:
Template.bar.helpers({ ....
(this bites me more often than I care to admit)
I do not know what you mean by
=> Inside the use > shadow-root, nothing is inserted.
Using the chrome inspector you can choose the element you want to examine and see if it is has been rendered in the page really easily.

Create multiple background using SASS mixin

I'm trying to create a mixin in Sass to generate multiple background, problem is the number of background is unknow, it's can be 3, 4 or even 5. Here what I try and fail.
#mixin multiBg($page: index, $sec: sec01,$from: 1, $to: 3, $type: jpg){
$url: (); // i'm try to create a empty array first
$newUrl: null; // and an empty variable
#for $i from $from through $to {
$newUrl: append($url, url(../img/#{$page}/#{$sec}_bg0#{$i}.#{$type})); // then append value to variable;
}
background: $newUrl;
}
#sec05 {
#include multiBg(index,sec05);
}
current output:
background: url(../img/index/sec05_bg03.jpg);
expected output:
background: url(../img/sec05_bg01.jpg),url(../img/sec05_bg02.jpg), url(../img/sec05_bg03.jpg);
I don't know how to fix this problem since i'm still learing SASS. Can someone enlighten me please.
You're on the right track! But your syntax and logic are slightly off. Here's what I came up with:
#mixin multiBg($page: index, $sec: sec01, $from: 1, $to: 5, $type: jpg) {
$url_list: ();
#for $i from $from through $to {
// I broke constructing the url and adding it to the set into two steps here.
// We could do this all at once, but separating it can make it easier to read.
// First, generate the url.
$url_string: url(../img/#{$page}/#{$sec}_bg0#{$i}.#{$type});
// Then add it to the list (the 'comma' part is important)
$url_list: append($url_list, $url_string, comma);
}
// Done looping? Output the final list!
background-image: $url_list;
}
That seems to return what you're looking for. Here are the official docs on list functions - I always forget one or two, may be useful for you too.
Also, since you mentioned you're new to sass - check out Sassmeister if you haven't already. It's a handy little sandbox for quickly prototyping and trying things out in sass; similar to Codepen but a bit more specialized. It's what I used to experiment with this question.
This is a cleaner answer, I believe.
#mixin image-resolve($image) {
$images: ();
#each $i in $image {
$path: ();
$images: append($images, $path, comma);
}
background-image: $images;
}

Duplicate an element?

I'm trying to duplicate an element in a page. I do not exactly how, is it possible with CSS or I need to use a script?
I imagine something like this:
#yan:before {
content: Get The Value Of(AN.element.inPage) !important;
font-size: 200% !important;
}
E.G.:
Let's say I want to get the title of a topic.
#yan:before {
content: Get The Value Of(h1.subject) !important;
font-size: 200% !important;
}
Then the topic title would appear 2 times in the page. Is it possible?
NOW This is my RIGHT code...
// ==UserScript==
// #name DUPLICATING - CLONE elements
// #namespace http://userscripts.org/scripts/show/109262
// #description EXPLAIN ME PLEASE
// #include http*://*answers.yahoo.com*
// #version 1
// ==/UserScript==
var yan = document.getElementById('yan'),
h1s = document.querySelectorAll('h1.subject');
[].forEach.call(h1s, function(node) {
// insert before
yan.parentNode.insertBefore(node.cloneNode(true), yan);
});
►►►► BUT now how do I set a position relative to the "#yan-related" element??
►►►► I want the newly created element to follow the #yan-related.
This is only possible with script, e.g.
var yan = document.getElementById('yan'),
h1s = document.querySelectorAll('h1.subject');
[].forEach.call(h1s, function(node) {
// insert before
yan.parentNode.insertBefore(node.cloneNode(true), yan);
});
Demo
You will have to use jquery. Check clone()
var a = $("#dashnav").clone
$("#dashnav").after(a)
No, CSS is for layout purposes, and cannot clone HTML elements. You will have to use JavaScript/jQuery for this purpose.
See related question on how to use .clone():
Is it possible to clone html element objects in JavaScript / JQuery?

Resources